From f1a2b39c039a0c5676e743bfbf6cd128d41becec Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Fri, 26 Mar 2021 02:12:55 +0300 Subject: [PATCH] docs + minor tweak... Signed-off-by: Alex A. Naanou --- README.md | 35 +++++++++++++++++++++++++++++++---- generator.js | 7 ++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a99238c..8e9cc7d 100644 --- a/README.md +++ b/README.md @@ -1115,6 +1115,10 @@ XXX .reduce(..) can return a non-iterable -- test and document this case... -> ``` +Equivalent to `Array`'s `.slice(..)` but will return a generator instead of an +array, for more info see: +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice + #### `.at(..)` @@ -1134,13 +1138,29 @@ available. -> ``` +Equivalent to `Array`'s `.flat(..)` but will return a generator instead of an +array, for more info see: +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat + #### `.shift()` / `.pop()` -Return a generator yielding the first/last sequence item. +Return the first/last item in generator. +```bnf +.pop() + -> + -> undefined -Note that there are no equivalents to `.push(..)` or `.unshift(..)` as they -need break item processing order. +.shift() + -> + -> undefined +``` + +Note that there are no equivalents to `.push(..)` and `.unshift(..)` as they +would require breaking item processing order. + +Note that `.shift()` may not yield the actual first item if the generator is +partially depleted at time of call. #### `.promise()` @@ -1168,19 +1188,26 @@ Note that this will deplete the generator. ``` Shorthands to `.promise().then(..)` / `.promise().catch(..)` / `.promise().finally(..)` +These are the same as equivalent `Promise` methods, for more info see: +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise + #### `.toArray()` +Unwind a generator into an array ```bnf .toArray() -> ``` -This is equivalent to `[...]`. +This is equivalent to `[...]` but more suited for the concatenative style. ### Generator constructor iteration +This API is essentially the same as [generator iteration](#generator-instance-iteration) +but will return a reusable generator _pipeline_ instead of a generator + ```javascript var sumOdds = generator.iter .filter(function(e){ diff --git a/generator.js b/generator.js index 14cbb1a..b7d853c 100644 --- a/generator.js +++ b/generator.js @@ -235,8 +235,13 @@ object.Mixin('GeneratorProtoMixin', 'soft', { return [...this] }, pop: function(){ return [...this].pop() }, + // XXX should this unwind the whole generator??? + // XXX this will not get the first item if the generator is already + // partially depleted... + // ...should we remove this??? shift: function(){ - return [...this].shift() }, + return this.next().value }, + //return [...this].shift() }, // promises... //