1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/*
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
Available via Academic Free License >= 2.1 OR the modified BSD license.
see: http://dojotoolkit.org/license for details
*/
if(!dojo._hasResource["dojox.lang.functional.fold"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.lang.functional.fold"] = true;
dojo.provide("dojox.lang.functional.fold");
dojo.require("dojox.lang.functional.lambda");
// This module adds high-level functions and related constructs:
// - "fold" family of functions
// Notes:
// - missing high-level functions are provided with the compatible API:
// foldl, foldl1, foldr, foldr1
// - missing JS standard functions are provided with the compatible API:
// reduce, reduceRight
// - the fold's counterpart: unfold
// 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 foldl, foldl1, and reduce)
(function(){
var d = dojo, df = dojox.lang.functional, empty = {};
d.mixin(df, {
// classic reduce-class functions
foldl: function(/*Array|String|Object*/ a, /*Function*/ 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 the final
// value.
if(typeof a == "string"){ a = a.split(""); }
o = o || d.global; f = df.lambda(f);
var i, n;
if(d.isArray(a)){
// array
for(i = 0, n = a.length; i < n; z = f.call(o, z, a[i], i, a), ++i);
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){
// iterator
for(i = 0; a.hasNext(); z = f.call(o, z, a.next(), i++, a));
}else{
// object/dictionary
for(i in a){
if(!(i in empty)){
z = f.call(o, z, a[i], i, a);
}
}
}
return z; // Object
},
foldl1: 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 the final value.
if(typeof a == "string"){ a = a.split(""); }
o = o || d.global; f = df.lambda(f);
var z, i, n;
if(d.isArray(a)){
// array
z = a[0];
for(i = 1, n = a.length; i < n; z = f.call(o, z, a[i], i, a), ++i);
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){
// iterator
if(a.hasNext()){
z = a.next();
for(i = 1; a.hasNext(); z = f.call(o, z, a.next(), i++, a));
}
}else{
// object/dictionary
var first = true;
for(i in a){
if(!(i in empty)){
if(first){
z = a[i];
first = false;
}else{
z = f.call(o, z, a[i], i, a);
}
}
}
}
return z; // Object
},
foldr: 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 the final
// value.
if(typeof a == "string"){ a = a.split(""); }
o = o || d.global; f = df.lambda(f);
for(var i = a.length; i > 0; --i, z = f.call(o, z, a[i], i, a));
return z; // Object
},
foldr1: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
// summary: repeatedly applies a binary function to an array from right
// to left; returns the final value.
if(typeof a == "string"){ a = a.split(""); }
o = o || d.global; f = df.lambda(f);
var n = a.length, z = a[n - 1], i = n - 1;
for(; i > 0; --i, z = f.call(o, z, a[i], i, a));
return z; // Object
},
// JS 1.8 standard array functions, which can take a lambda as a parameter.
reduce: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ z){
// summary: apply a function simultaneously against two values of the array
// (from left-to-right) as to reduce it to a single value.
return arguments.length < 3 ? df.foldl1(a, f) : df.foldl(a, f, z); // Object
},
reduceRight: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ z){
// summary: apply a function simultaneously against two values of the array
// (from right-to-left) as to reduce it to a single value.
return arguments.length < 3 ? df.foldr1(a, f) : df.foldr(a, f, z); // Object
},
// the fold's counterpart: unfold
unfold: function(/*Function|String|Array*/ pr, /*Function|String|Array*/ f,
/*Function|String|Array*/ g, /*Object*/ z, /*Object?*/ o){
// summary: builds an array by unfolding a value
o = o || d.global; f = df.lambda(f); g = df.lambda(g); pr = df.lambda(pr);
var t = [];
for(; !pr.call(o, z); t.push(f.call(o, z)), z = g.call(o, z));
return t; // Array
}
});
})();
}
|