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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
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.array"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.lang.functional.array"] = true;
dojo.provide("dojox.lang.functional.array");
dojo.require("dojox.lang.functional.lambda");
// This module adds high-level functions and related constructs:
// - array-processing functions similar to standard JS functions
// Notes:
// - this module provides JS standard methods similar to high-level functions in dojo/_base/array.js:
// forEach, map, filter, every, some
// 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
(function(){
var d = dojo, df = dojox.lang.functional, empty = {};
d.mixin(df, {
// JS 1.6 standard array functions, which can take a lambda as a parameter.
// Consider using dojo._base.array functions, if you don't need the lambda support.
filter: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
// summary: creates a new array with all elements that pass the test
// implemented by the provided function.
if(typeof a == "string"){ a = a.split(""); }
o = o || d.global; f = df.lambda(f);
var t = [], v, i, n;
if(d.isArray(a)){
// array
for(i = 0, n = a.length; i < n; ++i){
v = a[i];
if(f.call(o, v, i, a)){ t.push(v); }
}
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){
// iterator
for(i = 0; a.hasNext();){
v = a.next();
if(f.call(o, v, i++, a)){ t.push(v); }
}
}else{
// object/dictionary
for(i in a){
if(!(i in empty)){
v = a[i];
if(f.call(o, v, i, a)){ t.push(v); }
}
}
}
return t; // Array
},
forEach: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
// summary: executes a provided function once per array element.
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; f.call(o, a[i], i, a), ++i);
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){
// iterator
for(i = 0; a.hasNext(); f.call(o, a.next(), i++, a));
}else{
// object/dictionary
for(i in a){
if(!(i in empty)){
f.call(o, a[i], i, a);
}
}
}
return o; // Object
},
map: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
// summary: creates a new array with the results of calling
// a provided function on every element in this array.
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);
for(i = 0; i < n; t[i] = f.call(o, a[i], i, a), ++i);
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){
// iterator
t = [];
for(i = 0; a.hasNext(); t.push(f.call(o, a.next(), i++, a)));
}else{
// object/dictionary
t = [];
for(i in a){
if(!(i in empty)){
t.push(f.call(o, a[i], i, a));
}
}
}
return t; // Array
},
every: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
// summary: tests whether all elements in the array pass the test
// implemented by the provided function.
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; ++i){
if(!f.call(o, a[i], i, a)){
return false; // Boolean
}
}
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){
// iterator
for(i = 0; a.hasNext();){
if(!f.call(o, a.next(), i++, a)){
return false; // Boolean
}
}
}else{
// object/dictionary
for(i in a){
if(!(i in empty)){
if(!f.call(o, a[i], i, a)){
return false; // Boolean
}
}
}
}
return true; // Boolean
},
some: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){
// summary: tests whether some element in the array passes the test
// implemented by the provided function.
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; ++i){
if(f.call(o, a[i], i, a)){
return true; // Boolean
}
}
}else if(typeof a.hasNext == "function" && typeof a.next == "function"){
// iterator
for(i = 0; a.hasNext();){
if(f.call(o, a.next(), i++, a)){
return true; // Boolean
}
}
}else{
// object/dictionary
for(i in a){
if(!(i in empty)){
if(f.call(o, a[i], i, a)){
return true; // Boolean
}
}
}
}
return false; // Boolean
}
});
})();
}
|