From 98c0de1b4f28c610c6d2fa8726ca58f09c54c568 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Wed, 18 Nov 2020 06:59:56 +0300 Subject: [PATCH] minor tweaks... Signed-off-by: Alex A. Naanou --- Array.js | 18 +++++++++++++++--- README.md | 12 ++++++++---- package.json | 2 +- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Array.js b/Array.js index ebe595f..15c87d0 100644 --- a/Array.js +++ b/Array.js @@ -13,6 +13,8 @@ var object = require('ig-object') +var generator = require('./generator') + /*********************************************************************/ @@ -476,15 +478,25 @@ function(func, ...arrays){ : this.constructor.zip(func, this, ...arrays) } +// +// Array.iter() +// Array.iter([ .. ]) +// -> iterator // // array.iter() // -> iterator // // // XXX should this take an argument and be like map?? -Array.prototype.iter = function*(){ - for(var e of this){ - yield e } } +Array.prototype.iter = + function*(){ + for(var e of this){ + yield e } } +Array.iter = + function*(lst=[]){ + yield* lst.iter() } + + diff --git a/README.md b/README.md index 1731669..7afe2f9 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ A library of JavaScript type extensions, types and type utilities. - [`.toKeys(..)`](#arraytokeys) - [`.toMap(..)`](#arraytomap) - [`Array.zip(..)` / `.zip(..)`](#arrayzip--arrayzip) - - [`.iter()`](#arrayiter) + - [`Array.iter(..)` / `.iter()`](#arrayiter--arrayiter) - [Abortable `Array` iteration](#abortable-array-iteration) - [`array.StopIteration`](#arraystopiteration) - [`.smap(..)` / `.sfilter(..)` / `.sreduce(..)` / `.sforEach(..)`](#arraysmap--arraysfilter--arraysreduce--arraysforeach) @@ -77,7 +77,7 @@ A library of JavaScript type extensions, types and type utilities. - [`.promise()`](#generatorpromise) - [`.then(..)` / `.catch(..)` / `.finally(..)`](#generatorthen--generatorcatch--generatorfinally) - [`.toArray()`](#generatortoarray) - - [Generator constructor iteration workflow](#generator-constructor-iteration-workflow) + - [Generator constructor iteration](#generator-constructor-iteration) - [`Generator.at(..)`](#generatorat) - [`Generator.shift()` / `Generator.pop()`](#generatorshift--generatorpop) - [`Generator.slice(..)`](#generatorslice) @@ -407,7 +407,7 @@ This will return `true` if: ### `Array.zip(..)` / `.zip(..)` -### `.iter()` +### `Array.iter(..)` / `.iter()` Return an iterator/generator from the current array. @@ -660,6 +660,10 @@ promise, and it is similar to a _generator_ in that allows iteration over the contained values and chaining of operations but unlike `Promise.all(..)` this iteration occurs depth first instead of breadth first. +Essentially one can think about _promise iterators_ vs. _generators_ as the former +being internally controlled and asynchronous while the later being externally +controlled and synchronous. + Here is a traditional example using `Promise.all(..)`: ```javascript var p = Promise.all([ .. ]) @@ -783,7 +787,7 @@ var generator = require('ig-types/generator') #### `.toArray()` -### Generator constructor iteration workflow +### Generator constructor iteration ```javascript var sumOdds = generator.iter diff --git a/package.json b/package.json index 6dab3be..7a67512 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "3.7.9", + "version": "3.7.10", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {