Skip to content

Commit 43ac1e7

Browse files
docs: improve math/iter/ops examples
PR-URL: #2008 Closes: #1575 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 80e8e4a commit 43ac1e7

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

lib/node_modules/@stdlib/math/iter/ops/README.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,52 @@ The namespace contains the following functions for creating iterator protocol-co
6363

6464
## Examples
6565

66-
<!-- TODO: better examples -->
67-
6866
<!-- eslint no-undef: "error" -->
6967

7068
```javascript
71-
var objectKeys = require( '@stdlib/utils/keys' );
69+
var array2iterator = require( '@stdlib/array/to-iterator' );
7270
var ns = require( '@stdlib/math/iter/ops' );
7371

74-
console.log( objectKeys( ns ) );
72+
// Demonstrate operations with two iterators:
73+
var arr1 = [ 2.0, 3.0 ];
74+
var arr2 = [ 1.0, 4.0 ];
75+
var itAdd = ns.iterAdd( array2iterator( arr1 ), array2iterator( arr2 ) );
76+
var itDiv = ns.iterDivide( array2iterator( arr1 ), array2iterator( arr2 ) );
77+
var itMul = ns.iterMultiply( array2iterator( arr1 ), array2iterator( arr2 ) );
78+
var itSub = ns.iterSubtract( array2iterator( arr1 ), array2iterator( arr2 ) );
79+
80+
// Addition: 2+1=3, 3+4=7
81+
console.log( itAdd.next().value );
82+
// => 3.0
83+
console.log( itAdd.next().value );
84+
// => 7.0
85+
86+
// Division: 2/1=2, 3/4=0.75
87+
console.log( itDiv.next().value );
88+
// => 2.0
89+
console.log( itDiv.next().value );
90+
// => 0.75
91+
92+
// Multiplication: 2*1=2, 3*4=12
93+
console.log( itMul.next().value );
94+
// => 2.0
95+
console.log( itMul.next().value );
96+
// => 12.0
97+
98+
// Subtraction: 2-1=1, 3-4=-1
99+
console.log( itSub.next().value );
100+
// => 1.0
101+
console.log( itSub.next().value );
102+
// => -1.0
103+
104+
// Demonstrate operation with iterator and constant
105+
var it3 = array2iterator( [ 1.0, 2.0 ] );
106+
var itWithConstant = ns.iterAdd( it3, 3.0 );
107+
108+
console.log( itWithConstant.next().value );
109+
// => 4.0
110+
console.log( itWithConstant.next().value );
111+
// => 5.0
75112
```
76113

77114
</section>

lib/node_modules/@stdlib/math/iter/ops/examples/index.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,46 @@
1818

1919
'use strict';
2020

21-
var objectKeys = require( '@stdlib/utils/keys' );
21+
var array2iterator = require( '@stdlib/array/to-iterator' );
2222
var ns = require( './../lib' );
2323

24-
console.log( objectKeys( ns ) );
24+
// Demonstrate operations with two iterators:
25+
var arr1 = [ 2.0, 3.0 ];
26+
var arr2 = [ 1.0, 4.0 ];
27+
var itAdd = ns.iterAdd( array2iterator( arr1 ), array2iterator( arr2 ) );
28+
var itDiv = ns.iterDivide( array2iterator( arr1 ), array2iterator( arr2 ) );
29+
var itMul = ns.iterMultiply( array2iterator( arr1 ), array2iterator( arr2 ) );
30+
var itSub = ns.iterSubtract( array2iterator( arr1 ), array2iterator( arr2 ) );
31+
32+
// Addition: 2+1=3, 3+4=7
33+
console.log( itAdd.next().value );
34+
// => 3.0
35+
console.log( itAdd.next().value );
36+
// => 7.0
37+
38+
// Division: 2/1=2, 3/4=0.75
39+
console.log( itDiv.next().value );
40+
// => 2.0
41+
console.log( itDiv.next().value );
42+
// => 0.75
43+
44+
// Multiplication: 2*1=2, 3*4=12
45+
console.log( itMul.next().value );
46+
// => 2.0
47+
console.log( itMul.next().value );
48+
// => 12.0
49+
50+
// Subtraction: 2-1=1, 3-4=-1
51+
console.log( itSub.next().value );
52+
// => 1.0
53+
console.log( itSub.next().value );
54+
// => -1.0
55+
56+
// Demonstrate operation with iterator and constant
57+
var it3 = array2iterator( [ 1.0, 2.0 ] );
58+
var itWithConstant = ns.iterAdd( it3, 3.0 );
59+
60+
console.log( itWithConstant.next().value );
61+
// => 4.0
62+
console.log( itWithConstant.next().value );
63+
// => 5.0

0 commit comments

Comments
 (0)