extra-array
Advanced tools
Comparing version 2.0.29 to 2.0.30
76
index.js
@@ -65,78 +65,2 @@ const exports0 = Array.isArray; | ||
} | ||
function by(arr, val, fn, ths, bgn, end) { | ||
while(bgn<end) { | ||
var m = (bgn+end)>>>1, c = fn.call(ths, arr[m], val, m, arr); | ||
if(c<0) bgn = m+1; | ||
else if(c>0) end = m; | ||
else return m; | ||
} | ||
return ~bgn; | ||
} | ||
function binarySearch(arr, val, fn, ths, bgn=0, end=arr.length) { | ||
if(fn!=null) return by(arr, val, fn, ths, bgn, end); | ||
while(bgn<end) { | ||
var m = (bgn+end)>>>1; | ||
if(arr[m]<val) bgn = m+1; | ||
else if(arr[m]>val) end = m; | ||
else return m; | ||
} | ||
return ~bgn; | ||
} | ||
function by17(arr, val, fn, ths, bgn, end) { | ||
while(bgn<end) { | ||
var m = (bgn+end)>>>1, c = fn.call(ths, arr[m], val, m, arr); | ||
if(c<0) bgn = m+1; | ||
else if(c>0) end = m; | ||
else return m; | ||
} | ||
return bgn; | ||
} | ||
function binarySearch17(arr, val, fn, ths, bgn=0, end=arr.length) { | ||
if(fn!=null) return by17(arr, val, fn, ths, bgn, end); | ||
while(bgn<end) { | ||
var m = (bgn+end)>>>1; | ||
if(arr[m]<val) bgn = m+1; | ||
else if(arr[m]>val) end = m; | ||
else return m; | ||
} | ||
return bgn; | ||
} | ||
function by18(arr, val, fn, ths, bgn, end) { | ||
var end0 = end; | ||
while(bgn<end) { | ||
var m = (bgn+end)>>>1, c = fn.call(ths, arr[m], val, m, arr); | ||
if(c<0) bgn = m+1; | ||
else end = m; | ||
} | ||
return bgn>=end0 || arr[bgn]!==val? ~bgn:bgn; | ||
} | ||
function binarySearch18(arr, val, fn, ths, bgn=0, end=arr.length) { | ||
if(fn!=null) return by18(arr, val, fn, ths, bgn, end); | ||
var end0 = end; | ||
while(bgn<end) { | ||
var m = (bgn+end)>>>1; | ||
if(arr[m]<val) bgn = m+1; | ||
else end = m; | ||
} | ||
return bgn>=end0 || arr[bgn]!==val? ~bgn:bgn; | ||
} | ||
function by19(arr, val, fn, ths, bgn, end) { | ||
var bgn0 = bgn; | ||
while(bgn<end) { | ||
var m = (bgn+end)>>>1, c = fn.call(ths, arr[m], val, m, arr); | ||
if(c<=0) bgn = m+1; | ||
else end = m; | ||
} | ||
return bgn<=bgn0 || arr[bgn-1]!==val? ~bgn:bgn-1; | ||
} | ||
function binarySearch19(arr, val, fn, ths, bgn=0, end=arr.length) { | ||
if(fn!=null) return by19(arr, val, fn, ths, bgn, end); | ||
var bgn0 = bgn; | ||
while(bgn<end) { | ||
var m = (bgn+end)>>>1; | ||
if(arr[m]<=val) bgn = m+1; | ||
else end = m; | ||
} | ||
return bgn<=bgn0 || arr[bgn-1]!==val? ~bgn:bgn-1; | ||
} | ||
function containsIt(a, b, a0=0, a1=a.length, b0=0, b1=b.length) { | ||
@@ -143,0 +67,0 @@ for(i=a0, L=b1-b0, I=a1-L+1; i<I; i++) |
{ | ||
"name": "extra-array", | ||
"version": "2.0.29", | ||
"version": "2.0.30", | ||
"description": "Standard utility methods for Array.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -217,2 +217,105 @@ /** | ||
/** | ||
* Places value between each element in source. | ||
* @param {Array} x source | ||
* @param {*} v value to place | ||
* @returns {Array} [x0 v x1 v ...] | ||
*/ | ||
function intersperse(x, v) { | ||
var a = []; | ||
for(var w of x) | ||
a.push(w, v); | ||
a.pop(); | ||
return a; | ||
} | ||
/** | ||
* Places separator between each array in source. | ||
* @param {Array<Array>} xs source arrays | ||
* @param {Array} y separator | ||
* @returns {Array} [x0 y x1 y ...] | ||
*/ | ||
function intercalate(xs, y) { | ||
var a = [], Y = y.length; | ||
for(var x of xs) | ||
concat$(a, x, y); | ||
a.splice(a.length-Y, Y); | ||
return a; | ||
} | ||
/** | ||
* Applies a function against accumulator, and each value in array | ||
* (from left-right) to reduce it to a single value. | ||
* @param {Array} x array | ||
* @param {function} fn reduce fn (acc, val, idx, arr) | ||
* @param {*?} v inital value of accumulator | leftmost value in array | ||
* @returns {*} reduced value | ||
*/ | ||
function foldl(x, fn, v) { | ||
return arguments.length>=3? x.reduce(fn, v):x.reduce(fn); | ||
} | ||
/** | ||
* Applies a function against accumulator, and each value in array | ||
* (from right-left) to reduce it to a single value. | ||
* @param {Array} x array | ||
* @param {function} fn reduce fn (acc, val, idx, arr) | ||
* @param {*?} v inital value of accumulator | rightmost value in array | ||
* @returns {*} reduced value | ||
*/ | ||
function foldr(x, fn, v) { | ||
return arguments.length>=3? x.reduceRight(fn, v):x.reduceRight(fn); | ||
} | ||
/** | ||
* Applies a function against accumulator, and each value in array | ||
* (from left-right) to return successively reduced values. | ||
* @param {Array} x array | ||
* @param {function} fn reduce fn (acc, val, idx, arr) | ||
* @param {*?} v inital value of accumulator | leftmost value in array | ||
* @returns {Array} reduced values | ||
*/ | ||
function scanl(x, fn, v) { | ||
var V = arguments.length>=3; | ||
var a = V? []:[head(x)]; | ||
for(var i=V? 0:1, I=x.length; i<I; i++) | ||
a.push(v = fn(v, x[i], i, x)); | ||
return a; | ||
} | ||
/** | ||
* Applies a function against accumulator, and each value in array | ||
* (from right-left) to return successively reduced values. | ||
* @param {Array} x array | ||
* @param {function} fn reduce fn (acc, val, idx, arr) | ||
* @param {*?} v inital value of accumulator | rightmost value in array | ||
* @returns {Array} reduced values | ||
*/ | ||
function scanr(x, fn, v) { | ||
var V = arguments.length>=3; | ||
var a = V? []:[last(x)]; | ||
for(var i=x.length-(V? 1:2); i>=0; i--) | ||
a.push(v = fn(v, x[i], i, x)); | ||
return a; | ||
} | ||
/** | ||
* Builds list from a seed value (dual to foldr). | ||
* @param {function} fn unreduce fn (val, idx, arr) | ||
* @param {*} v initial value of accumucator | ||
*/ | ||
function unfoldr(fn, v) { | ||
var a = [], i = -1; | ||
while(true) { | ||
var r = fn(v, ++i, a); | ||
if(!r) return a; | ||
var [w, v] = r; | ||
a.push(w); | ||
} | ||
} | ||
/** | ||
* Combines values from n arrays, with a function. | ||
@@ -219,0 +322,0 @@ * @param {Array<Array>} xs n arrays |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35923
1162