addes stoppable iterator functions to Array...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-11-06 17:25:25 +03:00
parent 4fd5fdc993
commit bf8c2c8cdc
2 changed files with 26 additions and 1 deletions

View File

@ -187,6 +187,30 @@ Array.prototype.inplaceSortAs = function(other){
return this }
// Equivalent to .map(..) / .filter(..) / .reduce(..) / .. with support for
// StopIteration...
//
// NOTE: these add almost no overhead to the iteration.
//
// XXX should these return a partial result on StopIteration?
var wrapIterFunc = function(iter){
return function(func){
try {
return this[iter](...arguments)
// handle StopIteration...
} catch(err){
if(err === StopIteration){
return
} else if( err instanceof StopIteration){
return err.msg }
throw err } } }
Array.prototype.smap = wrapIterFunc('map')
Array.prototype.sfilter = wrapIterFunc('filter')
Array.prototype.sreduce = wrapIterFunc('reduce')
Array.prototype.sforEach = wrapIterFunc('forEach')
// Equivalent to .map(..) / .filter(..) / .reduce(..) that process the
// contents in chunks asynchronously...
//
@ -225,6 +249,7 @@ Array.prototype.inplaceSortAs = function(other){
// The main goal of this is to not block the runtime while processing a
// very long array by interrupting the processing with a timeout...
//
// XXX should these return a partial result on StopIteration?
var makeChunkIter = function(iter, wrapper){
wrapper = wrapper
|| function(res, func, array, e){

View File

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