added .froEach(..) to sync generators...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-08-08 21:51:06 +03:00
parent 72909c35b2
commit 934e3dd216
3 changed files with 25 additions and 1 deletions

View File

@ -100,6 +100,7 @@ Library of JavaScript type extensions, types and utilities.
- [`<generator>.iter(..)`](#generatoriter-1)
- [`<generator>.map(..)` / `<generator>.filter(..)`](#generatormap--generatorfilter)
- [`<generator>.reduce(..)` / `<generator>.greduce(..)`](#generatorreduce--generatorgreduce)
- [`<generator>.forEach(..)`](#generatorforeach)
- [`<generator>.slice(..)`](#generatorslice)
- [`<generator>.at(..)` / `<generator>.gat(..)`](#generatorat--generatorgat)
- [`<generator>.flat(..)`](#generatorflat)
@ -2135,6 +2136,17 @@ XXX .reduce(..) can return a non-iterable -- test and document this case...
...compare with Array.prototype.reduce(..)
-->
#### `<generator>.forEach(..)`
```bnf
<generator>.forEach(<func>)
-> <array>
```
This is different from the above in that this will unwind the `<generator>`.
Note that this differs from `<array>.forEach(..)` in that his will return the resulting array, essentially behaving like `.map(..)`.
#### `<generator>.slice(..)`

View File

@ -223,6 +223,12 @@ object.Mixin('GeneratorMixin', 'soft', {
reduce: makeGenerator('reduce'),
reduceRight: makeGenerator('reduceRight'),
// XXX add .toString(..) ???
forEach: function(func){
var that = this
return function(){
return that(...arguments).forEach(func) } },
// non-generators...
//
toArray: function(){
@ -397,6 +403,12 @@ object.Mixin('GeneratorProtoMixin', 'soft', {
greduce: function*(func, res){
yield this.reduce(...arguments) },
// NOTE: this is a special case in that it will unwind the generator...
// NOTE: this is different from <array>.forEach(..) in that this will
// return the resulting array.
forEach: function(func){
return [...this].map(func) },
pop: function(){
return [...this].pop() },
// XXX this needs the value to be iterable...

View File

@ -1,6 +1,6 @@
{
"name": "ig-types",
"version": "6.16.3",
"version": "6.16.4",
"description": "Generic JavaScript types and type extensions...",
"main": "main.js",
"scripts": {