Skip to content

Commit

Permalink
docs: improve README examples of math/iter/sequences
Browse files Browse the repository at this point in the history
PR-URL: #1779
Ref: #1577

Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com>
Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
  • Loading branch information
ShivamAhir and Planeshifter authored Oct 30, 2024
1 parent 43ac1e7 commit e469715
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
41 changes: 37 additions & 4 deletions lib/node_modules/@stdlib/math/iter/sequences/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,48 @@ The namespace contains the following functions for creating iterator protocol-co

## Examples

<!-- TODO: better examples -->

<!-- eslint no-undef: "error" -->

```javascript
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( '@stdlib/math/iter/sequences' );

console.log( objectKeys( ns ) );
// Create iterators for generating square and cube numbers:
var squares = ns.iterSquaresSeq();
var cubes = ns.iterCubesSeq();

// Iterate over both sequences and log the first five pairs:
var square;
var cube;
var i;
for ( i = 0; i < 5; i++ ) {
square = squares.next().value;
cube = cubes.next().value;
console.log( 'Square: %d, Cube: %d', square, cube );
}

// Calculate the sum of the first ten Fibonacci numbers:
var fibonacci = ns.iterFibonacciSeq({
'iter': 10
});
var sum = 0;
var v = fibonacci.next();
while ( v.done === false ) {
sum += v.value;
v = fibonacci.next();
}
console.log( 'Sum of first ten Fibonacci numbers: %d', sum );

// Generate prime numbers:
var primes = ns.iterPrimesSeq({
'iter': 10
});

console.log( 'First ten prime numbers:' );
v = primes.next();
while ( v.done === false ) {
console.log( v.value );
v = primes.next();
}
```

</section>
Expand Down
41 changes: 38 additions & 3 deletions lib/node_modules/@stdlib/math/iter/sequences/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,42 @@

'use strict';

var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( './../lib' );
var ns = require('./../lib');

Check failure on line 21 in lib/node_modules/@stdlib/math/iter/sequences/examples/index.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

There should be exactly one space after the opening parenthesis in require calls

Check failure on line 21 in lib/node_modules/@stdlib/math/iter/sequences/examples/index.js

View workflow job for this annotation

GitHub Actions / Lint Changed Files

There should be exactly one space before the closing parenthesis in require calls

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 31, 2024

Member

@Planeshifter Missing spaces in the require statement.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Nov 23, 2024

Author Member

Fixed.


console.log( objectKeys( ns ) );
// Create iterators for generating square and cube numbers:
var squares = ns.iterSquaresSeq();
var cubes = ns.iterCubesSeq();

// Iterate over both sequences and log the first five pairs:
var square;

This comment has been minimized.

Copy link
@kgryte

kgryte Oct 31, 2024

Member

@Planeshifter In the interest of future refactoring, I suggest inlining L32 and L33 in L34 so we can avoid needing square and cube temporary variables.

This comment has been minimized.

Copy link
@Planeshifter

Planeshifter Nov 23, 2024

Author Member

Done.

var cube;
var i;
for ( i = 0; i < 5; i++ ) {
square = squares.next().value;
cube = cubes.next().value;
console.log( 'Square: %d, Cube: %d', square, cube );
}

// Calculate the sum of the first 10 Fibonacci numbers:
var fibonacci = ns.iterFibonacciSeq({
'iter': 10
});
var sum = 0;
var v = fibonacci.next();
while ( v.done === false ) {
sum += v.value;
v = fibonacci.next();
}
console.log( 'Sum of first 10 Fibonacci numbers: %d', sum );

// Generate prime numbers:
var primes = ns.iterPrimesSeq({
'iter': 10
});

console.log( 'First ten prime numbers:' );
v = primes.next();
while ( v.done === false ) {
console.log( v.value );
v = primes.next();
}

1 comment on commit e469715

@stdlib-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage Report

Package Statements Branches Functions Lines
math/iter/sequences $\color{green}303/303$
$\color{green}+100.00\%$
$\color{green}1/1$
$\color{green}+100.00\%$
$\color{green}0/0$
$\color{green}+100.00\%$
$\color{green}303/303$
$\color{green}+100.00\%$

The above coverage report was generated for the changes in this push.

Please # to comment.