summaryrefslogtreecommitdiff
path: root/js/dojo/dojox/lang/functional/scan.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/dojo/dojox/lang/functional/scan.js')
-rw-r--r--js/dojo/dojox/lang/functional/scan.js104
1 files changed, 104 insertions, 0 deletions
diff --git a/js/dojo/dojox/lang/functional/scan.js b/js/dojo/dojox/lang/functional/scan.js
new file mode 100644
index 0000000..8abbe47
--- /dev/null
+++ b/js/dojo/dojox/lang/functional/scan.js
@@ -0,0 +1,104 @@
+//>>built
+define("dojox/lang/functional/scan", ["dojo/_base/kernel", "dojo/_base/lang", "./lambda"], function(d, darray, df){
+
+// This module adds high-level functions and related constructs:
+// - "scan" family of functions
+
+// Notes:
+// - missing high-level functions are provided with the compatible API:
+// scanl, scanl1, scanr, scanr1
+
+// Defined methods:
+// - take any valid lambda argument as the functional argument
+// - operate on dense arrays
+// - take a string as the array argument
+// - take an iterator objects as the array argument (only scanl, and scanl1)
+
+ var empty = {};
+
+ d.mixin(df, {
+ // classic reduce-class functions
+ scanl: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){
+ // summary: repeatedly applies a binary function to an array from left
+ // to right using a seed value as a starting point; returns an array
+ // of values produced by foldl() at that point.
+ if(typeof a == "string"){ a = a.split(""); }
+ o = o || d.global; f = df.lambda(f);
+ var t, n, i;
+ if(d.isArray(a)){
+ // array
+ t = new Array((n = a.length) + 1);
+ t[0] = z;
+ for(i = 0; i < n; z = f.call(o, z, a[i], i, a), t[++i] = z);
+ }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
+ // iterator
+ t = [z];
+ for(i = 0; a.hasNext(); t.push(z = f.call(o, z, a.next(), i++, a)));
+ }else{
+ // object/dictionary
+ t = [z];
+ for(i in a){
+ if(!(i in empty)){
+ t.push(z = f.call(o, z, a[i], i, a));
+ }
+ }
+ }
+ return t; // Array
+ },
+ scanl1: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
+ // summary: repeatedly applies a binary function to an array from left
+ // to right; returns an array of values produced by foldl1() at that
+ // point.
+ if(typeof a == "string"){ a = a.split(""); }
+ o = o || d.global; f = df.lambda(f);
+ var t, n, z, first = true;
+ if(d.isArray(a)){
+ // array
+ t = new Array(n = a.length);
+ t[0] = z = a[0];
+ for(var i = 1; i < n; t[i] = z = f.call(o, z, a[i], i, a), ++i);
+ }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
+ // iterator
+ if(a.hasNext()){
+ t = [z = a.next()];
+ for(i = 1; a.hasNext(); t.push(z = f.call(o, z, a.next(), i++, a)));
+ }
+ }else{
+ // object/dictionary
+ for(i in a){
+ if(!(i in empty)){
+ if(first){
+ t = [z = a[i]];
+ first = false;
+ }else{
+ t.push(z = f.call(o, z, a[i], i, a));
+ }
+ }
+ }
+ }
+ return t; // Array
+ },
+ scanr: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){
+ // summary: repeatedly applies a binary function to an array from right
+ // to left using a seed value as a starting point; returns an array
+ // of values produced by foldr() at that point.
+ if(typeof a == "string"){ a = a.split(""); }
+ o = o || d.global; f = df.lambda(f);
+ var n = a.length, t = new Array(n + 1), i = n;
+ t[n] = z;
+ for(; i > 0; --i, z = f.call(o, z, a[i], i, a), t[i] = z);
+ return t; // Array
+ },
+ scanr1: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
+ // summary: repeatedly applies a binary function to an array from right
+ // to left; returns an array of values produced by foldr1() at that
+ // point.
+ if(typeof a == "string"){ a = a.split(""); }
+ o = o || d.global; f = df.lambda(f);
+ var n = a.length, t = new Array(n), z = a[n - 1], i = n - 1;
+ t[i] = z;
+ for(; i > 0; --i, z = f.call(o, z, a[i], i, a), t[i] = z);
+ return t; // Array
+ }
+ });
+});