1 | define(["dojo/_base/kernel", "dojo/_base/lang", "./lambda"], function(d, darray, df){ |
---|
2 | |
---|
3 | // This module adds high-level functions and related constructs: |
---|
4 | // - "scan" family of functions |
---|
5 | |
---|
6 | // Notes: |
---|
7 | // - missing high-level functions are provided with the compatible API: |
---|
8 | // scanl, scanl1, scanr, scanr1 |
---|
9 | |
---|
10 | // Defined methods: |
---|
11 | // - take any valid lambda argument as the functional argument |
---|
12 | // - operate on dense arrays |
---|
13 | // - take a string as the array argument |
---|
14 | // - take an iterator objects as the array argument (only scanl, and scanl1) |
---|
15 | |
---|
16 | var empty = {}; |
---|
17 | |
---|
18 | d.mixin(df, { |
---|
19 | // classic reduce-class functions |
---|
20 | scanl: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){ |
---|
21 | // summary: repeatedly applies a binary function to an array from left |
---|
22 | // to right using a seed value as a starting point; returns an array |
---|
23 | // of values produced by foldl() at that point. |
---|
24 | if(typeof a == "string"){ a = a.split(""); } |
---|
25 | o = o || d.global; f = df.lambda(f); |
---|
26 | var t, n, i; |
---|
27 | if(d.isArray(a)){ |
---|
28 | // array |
---|
29 | t = new Array((n = a.length) + 1); |
---|
30 | t[0] = z; |
---|
31 | for(i = 0; i < n; z = f.call(o, z, a[i], i, a), t[++i] = z); |
---|
32 | }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
---|
33 | // iterator |
---|
34 | t = [z]; |
---|
35 | for(i = 0; a.hasNext(); t.push(z = f.call(o, z, a.next(), i++, a))); |
---|
36 | }else{ |
---|
37 | // object/dictionary |
---|
38 | t = [z]; |
---|
39 | for(i in a){ |
---|
40 | if(!(i in empty)){ |
---|
41 | t.push(z = f.call(o, z, a[i], i, a)); |
---|
42 | } |
---|
43 | } |
---|
44 | } |
---|
45 | return t; // Array |
---|
46 | }, |
---|
47 | scanl1: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
48 | // summary: repeatedly applies a binary function to an array from left |
---|
49 | // to right; returns an array of values produced by foldl1() at that |
---|
50 | // point. |
---|
51 | if(typeof a == "string"){ a = a.split(""); } |
---|
52 | o = o || d.global; f = df.lambda(f); |
---|
53 | var t, n, z, first = true; |
---|
54 | if(d.isArray(a)){ |
---|
55 | // array |
---|
56 | t = new Array(n = a.length); |
---|
57 | t[0] = z = a[0]; |
---|
58 | for(var i = 1; i < n; t[i] = z = f.call(o, z, a[i], i, a), ++i); |
---|
59 | }else if(typeof a.hasNext == "function" && typeof a.next == "function"){ |
---|
60 | // iterator |
---|
61 | if(a.hasNext()){ |
---|
62 | t = [z = a.next()]; |
---|
63 | for(i = 1; a.hasNext(); t.push(z = f.call(o, z, a.next(), i++, a))); |
---|
64 | } |
---|
65 | }else{ |
---|
66 | // object/dictionary |
---|
67 | for(i in a){ |
---|
68 | if(!(i in empty)){ |
---|
69 | if(first){ |
---|
70 | t = [z = a[i]]; |
---|
71 | first = false; |
---|
72 | }else{ |
---|
73 | t.push(z = f.call(o, z, a[i], i, a)); |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | } |
---|
78 | return t; // Array |
---|
79 | }, |
---|
80 | scanr: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){ |
---|
81 | // summary: repeatedly applies a binary function to an array from right |
---|
82 | // to left using a seed value as a starting point; returns an array |
---|
83 | // of values produced by foldr() at that point. |
---|
84 | if(typeof a == "string"){ a = a.split(""); } |
---|
85 | o = o || d.global; f = df.lambda(f); |
---|
86 | var n = a.length, t = new Array(n + 1), i = n; |
---|
87 | t[n] = z; |
---|
88 | for(; i > 0; --i, z = f.call(o, z, a[i], i, a), t[i] = z); |
---|
89 | return t; // Array |
---|
90 | }, |
---|
91 | scanr1: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
92 | // summary: repeatedly applies a binary function to an array from right |
---|
93 | // to left; returns an array of values produced by foldr1() at that |
---|
94 | // point. |
---|
95 | if(typeof a == "string"){ a = a.split(""); } |
---|
96 | o = o || d.global; f = df.lambda(f); |
---|
97 | var n = a.length, t = new Array(n), z = a[n - 1], i = n - 1; |
---|
98 | t[i] = z; |
---|
99 | for(; i > 0; --i, z = f.call(o, z, a[i], i, a), t[i] = z); |
---|
100 | return t; // Array |
---|
101 | } |
---|
102 | }); |
---|
103 | }); |
---|