source: Dev/trunk/src/client/dojox/lang/functional/scan.js @ 483

Last change on this file since 483 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

  • Property svn:executable set to *
File size: 3.5 KB
Line 
1define(["dojo/_base/kernel", "dojo/_base/lang", "./lambda"], function(kernel, lang, 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        lang.mixin(df, {
19                // classic reduce-class functions
20                scanl: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){
21                        // summary:
22                        //              repeatedly applies a binary function to an array from left
23                        //              to right using a seed value as a starting point; returns an array
24                        //              of values produced by foldl() at that point.
25                        if(typeof a == "string"){ a = a.split(""); }
26                        o = o || kernel.global; f = df.lambda(f);
27                        var t, n, i;
28                        if(lang.isArray(a)){
29                                // array
30                                t = new Array((n = a.length) + 1);
31                                t[0] = z;
32                                for(i = 0; i < n; z = f.call(o, z, a[i], i, a), t[++i] = z);
33                        }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
34                                // iterator
35                                t = [z];
36                                for(i = 0; a.hasNext(); t.push(z = f.call(o, z, a.next(), i++, a)));
37                        }else{
38                                // object/dictionary
39                                t = [z];
40                                for(i in a){
41                                        if(!(i in empty)){
42                                                t.push(z = f.call(o, z, a[i], i, a));
43                                        }
44                                }
45                        }
46                        return t;       // Array
47                },
48                scanl1: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
49                        // summary:
50                        //              repeatedly applies a binary function to an array from left
51                        //              to right; returns an array of values produced by foldl1() at that
52                        //              point.
53                        if(typeof a == "string"){ a = a.split(""); }
54                        o = o || kernel.global; f = df.lambda(f);
55                        var t, n, z, first = true;
56                        if(lang.isArray(a)){
57                                // array
58                                t = new Array(n = a.length);
59                                t[0] = z = a[0];
60                                for(var i = 1; i < n; t[i] = z = f.call(o, z, a[i], i, a), ++i);
61                        }else if(typeof a.hasNext == "function" && typeof a.next == "function"){
62                                // iterator
63                                if(a.hasNext()){
64                                        t = [z = a.next()];
65                                        for(i = 1; a.hasNext(); t.push(z = f.call(o, z, a.next(), i++, a)));
66                                }
67                        }else{
68                                // object/dictionary
69                                for(i in a){
70                                        if(!(i in empty)){
71                                                if(first){
72                                                        t = [z = a[i]];
73                                                        first = false;
74                                                }else{
75                                                        t.push(z = f.call(o, z, a[i], i, a));
76                                                }
77                                        }
78                                }
79                        }
80                        return t;       // Array
81                },
82                scanr: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){
83                        // summary:
84                        //              repeatedly applies a binary function to an array from right
85                        //              to left using a seed value as a starting point; returns an array
86                        //              of values produced by foldr() at that point.
87                        if(typeof a == "string"){ a = a.split(""); }
88                        o = o || kernel.global; f = df.lambda(f);
89                        var n = a.length, t = new Array(n + 1), i = n;
90                        t[n] = z;
91                        for(; i > 0; --i, z = f.call(o, z, a[i], i, a), t[i] = z);
92                        return t;       // Array
93                },
94                scanr1: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
95                        // summary:
96                        //              repeatedly applies a binary function to an array from right
97                        //              to left; returns an array of values produced by foldr1() at that
98                        //              point.
99                        if(typeof a == "string"){ a = a.split(""); }
100                        o = o || kernel.global; f = df.lambda(f);
101                        var n = a.length, t = new Array(n), z = a[n - 1], i = n - 1;
102                        t[i] = z;
103                        for(; i > 0; --i, z = f.call(o, z, a[i], i, a), t[i] = z);
104                        return t;       // Array
105                }
106        });
107});
Note: See TracBrowser for help on using the repository browser.