From fe1f39cba6cf80236fc370c708aaf28b6b20b523 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Tue, 25 May 2021 18:21:30 +0300 Subject: [PATCH] refactoring and bugfixes... Signed-off-by: Alex A. Naanou --- Array.js | 27 +++++++++++++++++++-------- README.md | 10 +++++++--- generator.js | 42 +++++++++++++++++++++++++++--------------- package.json | 2 +- 4 files changed, 54 insertions(+), 27 deletions(-) diff --git a/Array.js b/Array.js index dd94f9f..d467228 100644 --- a/Array.js +++ b/Array.js @@ -29,14 +29,14 @@ module.STOP = //--------------------------------------------------------------------- // Mixins... -// Equivalent to .map(..) / .filter(..) / .reduce(..) / .. with support for -// STOP... +// Wrap .map(..) / .filter(..) / .reduce(..) / .. to support STOP... // // NOTE: these add almost no overhead to the iteration. // NOTE: these will not return a partial result if stopped. // -// XXX should these return a partial result on STOP? -var wrapIterFunc = function(iter){ +/*/ XXX should these return a partial result on STOP? +// ...use generators to do this... +var stoppable = function(iter){ return function(func){ try { return this[iter](...arguments) @@ -46,6 +46,17 @@ var wrapIterFunc = function(iter){ } else if(err instanceof STOP){ return err.value } throw err } } } +/*/ +var stoppableList = function(iter){ + return function(func){ + return [...this.iter()[iter](...arguments)] } } +var stoppableValue = function(iter, no_return=false){ + return function(func){ + var res = this.iter()[iter](...arguments) + return no_return ? + undefined + : res } } +//*/ // Equivalent to .map(..) / .filter(..) / .reduce(..) that process the @@ -493,10 +504,10 @@ object.Mixin('ArrayProtoMixin', 'soft', { // Stoppable iteration... // - smap: wrapIterFunc('map'), - sfilter: wrapIterFunc('filter'), - sreduce: wrapIterFunc('reduce'), - sforEach: wrapIterFunc('forEach'), + smap: stoppableList('map'), + sfilter: stoppableList('filter'), + sreduce: stoppableValue('reduce'), + sforEach: stoppableValue('map', true), // Chunk iteration... // diff --git a/README.md b/README.md index f7aa64b..afaf597 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,8 @@ A library of JavaScript type extensions, types and type utilities. - [`generator.iter(..)`](#generatoriter) - [`generator.STOP`](#generatorstop) - [Generator instance iteration](#generator-instance-iteration) - - [`.map(..)` / `.filter(..)` / `.reduce(..)`](#generatormap--generatorfilter--generatorreduce) + - [`.map(..)` / `.filter(..)`](#generatormap--generatorfilter) + - [`.reduce(..)` / `.greduce(..)`](#generatorreduce--generatorgreduce) - [`.slice(..)`](#generatorslice) - [`.at(..)` / `.gat(..)`](#generatorat--generatorgat) - [`.flat(..)`](#generatorflat) @@ -1650,7 +1651,7 @@ Chained generators handle items depth-first, i.e. the items are passed as they are yielded down the generator chain. -#### `.map(..)` / `.filter(..)` / `.reduce(..)` +#### `.map(..)` / `.filter(..)` Equivalents to `Array`'s `.map(..)`, `.filter(..)` and `.reduce(..)` but return generators that yield the handler return values. @@ -1683,6 +1684,9 @@ var L = [1,2,3,4,5] .toArray() ``` + +#### `.reduce(..)` / `.greduce(..)` +