adding .between(..) to arrays and generators...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-08-14 11:15:54 +03:00
parent 934e3dd216
commit 62bfed34aa
4 changed files with 58 additions and 1 deletions

View File

@ -482,6 +482,35 @@ object.Mixin('ArrayProtoMixin', 'soft', {
this.constructor.zip(this, func, ...arrays)
: this.constructor.zip(func, this, ...arrays) },
// Insert new values between elements of an array
//
// .between(value)
// -> array
//
// .between(func)
// -> array
//
// func([a, b], from_index, to_index, array)
// -> elem
//
between: function(func){
var res = []
// NOTE: we skip the last element...
for(var i=0; i < this.length; i+=1){
var pair = new Array(2)
i in this ?
res.push(pair[0] = this[i])
: (res.length += 1)
if(i+1 >= this.length){
break }
i+1 in this
&& (pair[1] = this[i+1])
res.push(
typeof(func) == 'function' ?
func.call(this, pair, i, res.length, this)
: func) }
return res },
// get iterator over array...
//
// Array.iter()

View File

@ -294,6 +294,11 @@ object.Constructor('IterablePromise', Promise, {
.then(function(){
return res }) },
// XXX BETWEEN...
between: function(func){
// XXX
},
// XXX .chain(..) -- see generator.chain(..)
flat: function(depth=1){

View File

@ -223,6 +223,8 @@ object.Mixin('GeneratorMixin', 'soft', {
reduce: makeGenerator('reduce'),
reduceRight: makeGenerator('reduceRight'),
between: makeGenerator('between'),
// XXX add .toString(..) ???
forEach: function(func){
var that = this
@ -403,6 +405,19 @@ object.Mixin('GeneratorProtoMixin', 'soft', {
greduce: function*(func, res){
yield this.reduce(...arguments) },
between: stoppable(function*(func){
var i = 0
var j = 0
var prev
for(var e of this){
if(i > 0){
yield typeof(func) == 'function' ?
func.call(this, [prev, e], i-1, i + j++, this)
: func }
prev = e
yield e
i++ } }),
// NOTE: this is a special case in that it will unwind the generator...
// NOTE: this is different from <array>.forEach(..) in that this will
// return the resulting array.
@ -502,6 +517,9 @@ object.Mixin('AsyncGeneratorMixin', 'soft', {
map: makeGenerator('async', 'map'),
filter: makeGenerator('async', 'filter'),
reduce: makeGenerator('async', 'reduce'),
// XXX TEST...
between: makeGenerator('async', 'between'),
})
var AsyncGeneratorProtoMixin =
@ -559,6 +577,11 @@ object.Mixin('AsyncGeneratorProtoMixin', 'soft', {
return [] })
return state },
// XXX BETWEEN...
between: async function(func){
// XXX
},
// XXX TEST...
chain: async function*(...next){
yield* next

View File

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