mirror of
https://github.com/flynx/types.js.git
synced 2025-10-28 10:00:08 +00:00
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:
parent
0feb5c4339
commit
c735bc84d1
4
Map.js
4
Map.js
@ -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
|
||||
|
||||
@ -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:
|
||||
|
||||
20
README.md
20
README.md
@ -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
4
Set.js
@ -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]) },
|
||||
|
||||
@ -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???
|
||||
|
||||
@ -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": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user