added .iter() to generators, sets and maps (compatibility with arrays)...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2021-06-23 09:05:19 +03:00
parent 0feb5c4339
commit c735bc84d1
6 changed files with 40 additions and 1 deletions

4
Map.js
View File

@ -16,6 +16,10 @@ var object = require('ig-object')
var MapProtoMixin =
module.MapProtoMixin =
object.Mixin('MapProtoMixin', 'soft', {
iter: function*(){
for(var e of this){
yield e } },
// NOTE: we do not touch .__keys here as no renaming is ever done...
//
// XXX this essentially rewrites the whole map, is there a faster/better

View File

@ -117,6 +117,9 @@ object.Constructor('IterablePromise', Promise, {
return (e && e.flat) ?
e.flat(depth)
: e }) },
// XXX do we need this???
//iter: function(){
// return this },
// XXX do we need:

View File

@ -80,6 +80,7 @@ Library of JavaScript type extensions, types and utilities.
- [`generator.iter(..)`](#generatoriter)
- [`generator.STOP`](#generatorstop)
- [Generator instance iteration](#generator-instance-iteration)
- [`<generator>.iter()`](#generatoriter-1)
- [`<generator>.map(..)` / `<generator>.filter(..)`](#generatormap--generatorfilter)
- [`<generator>.reduce(..)` / `<generator>.greduce(..)`](#generatorreduce--generatorgreduce)
- [`<generator>.slice(..)`](#generatorslice)
@ -91,6 +92,7 @@ Library of JavaScript type extensions, types and utilities.
- [`<generator>.toArray()`](#generatortoarray)
- [Treating iterators the same as generators](#treating-iterators-the-same-as-generators)
- [Generator constructor iteration](#generator-constructor-iteration)
- [`<Generator>.iter(..)`](#generatoriter-2)
- [`<Generator>.at(..)` / `<Generator>.gat(..)`](#generatorat--generatorgat-1)
- [`<Generator>.shift()` / `<Generator>.pop()` / `<Generator>.gshift()` / `<Generator>.gpop()`](#generatorshift--generatorpop--generatorgshift--generatorgpop-1)
- [`<Generator>.slice(..)`](#generatorslice-1)
@ -1739,6 +1741,17 @@ Chained generators handle items depth-first, i.e. the items are passed as they
are yielded down the generator chain.
#### `<generator>.iter()`
Iterate over the generator.
```bnf
<generator>.iter()
-> <generator>
```
This is here mainly for compatibility with [`<array>`'s `.iter()`](#arrayiter--arrayiter).
#### `<generator>.map(..)` / `<generator>.filter(..)`
Equivalents to `Array`'s `.map(..)`, `.filter(..)` and `.reduce(..)` but return
@ -1969,6 +1982,13 @@ XXX list the differences...
-->
#### `<Generator>.iter(..)`
This is a shorthand to [`iter(..)`](#generatoriter).
This is here mainly for compatibility with
[`Array`'s `.iter(..)`](#arrayiter--arrayiter).
#### `<Generator>.at(..)` / `<Generator>.gat(..)`
```bnf

4
Set.js
View File

@ -16,6 +16,10 @@ var object = require('ig-object')
var SetProtoMixin =
module.SetProtoMixin =
object.Mixin('SetMixin', 'soft', {
iter: function*(){
for(var e of this){
yield e } },
// Set set operation shorthands...
unite: function(other=[]){
return new Set([...this, ...other]) },

View File

@ -190,6 +190,10 @@ module.GeneratorMixin =
object.Mixin('GeneratorMixin', 'soft', {
STOP: object.STOP,
// NOTE: this is here for compatibility with Array.iter(..)
iter: function*(lst=[]){
yield* module.iter(lst) },
gat: makeGenerator('gat'),
at: function(i){
var that = this
@ -263,6 +267,10 @@ object.Mixin('GeneratorMixin', 'soft', {
var GeneratorProtoMixin =
module.GeneratorProtoMixin =
object.Mixin('GeneratorProtoMixin', 'soft', {
// NOTE: this is here for compatibility with [..].iter()
iter: function*(){
yield* this },
at: function(i){
return this.gat(i).next().value },
// XXX this needs the value to be iterable... why???

View File

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