diff --git a/Array.js b/Array.js index 5d868dc..364258c 100644 --- a/Array.js +++ b/Array.js @@ -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() diff --git a/Promise.js b/Promise.js index 9d2e7ab..6346de7 100644 --- a/Promise.js +++ b/Promise.js @@ -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){ diff --git a/generator.js b/generator.js index d9593ca..169c28e 100644 --- a/generator.js +++ b/generator.js @@ -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 .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 diff --git a/package.json b/package.json index 2df51fc..2fc09a2 100644 --- a/package.json +++ b/package.json @@ -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": {