Project

General

Profile

Statistics
| Revision:

root / trunk / web / dojo / dojox / lang / functional / fold.js @ 12

History | View | Annotate | Download (1.96 KB)

1 9 andrej.cim
/*
2
        Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3
        Available via Academic Free License >= 2.1 OR the modified BSD license.
4
        see: http://dojotoolkit.org/license for details
5
*/
6
7
8
if(!dojo._hasResource["dojox.lang.functional.fold"]){
9
dojo._hasResource["dojox.lang.functional.fold"]=true;
10
dojo.provide("dojox.lang.functional.fold");
11
dojo.require("dojox.lang.functional.lambda");
12
(function(){
13
var d=dojo,df=dojox.lang.functional,_1={};
14
d.mixin(df,{foldl:function(a,f,z,o){
15
if(typeof a=="string"){
16
a=a.split("");
17
}
18
o=o||d.global;
19
f=df.lambda(f);
20
var i,n;
21
if(d.isArray(a)){
22
for(i=0,n=a.length;i<n;z=f.call(o,z,a[i],i,a),++i){
23
}
24
}else{
25
if(typeof a.hasNext=="function"&&typeof a.next=="function"){
26
for(i=0;a.hasNext();z=f.call(o,z,a.next(),i++,a)){
27
}
28
}else{
29
for(i in a){
30
if(!(i in _1)){
31
z=f.call(o,z,a[i],i,a);
32
}
33
}
34
}
35
}
36
return z;
37
},foldl1:function(a,f,o){
38
if(typeof a=="string"){
39
a=a.split("");
40
}
41
o=o||d.global;
42
f=df.lambda(f);
43
var z,i,n;
44
if(d.isArray(a)){
45
z=a[0];
46
for(i=1,n=a.length;i<n;z=f.call(o,z,a[i],i,a),++i){
47
}
48
}else{
49
if(typeof a.hasNext=="function"&&typeof a.next=="function"){
50
if(a.hasNext()){
51
z=a.next();
52
for(i=1;a.hasNext();z=f.call(o,z,a.next(),i++,a)){
53
}
54
}
55
}else{
56
var _2=true;
57
for(i in a){
58
if(!(i in _1)){
59
if(_2){
60
z=a[i];
61
_2=false;
62
}else{
63
z=f.call(o,z,a[i],i,a);
64
}
65
}
66
}
67
}
68
}
69
return z;
70
},foldr:function(a,f,z,o){
71
if(typeof a=="string"){
72
a=a.split("");
73
}
74
o=o||d.global;
75
f=df.lambda(f);
76
for(var i=a.length;i>0;--i,z=f.call(o,z,a[i],i,a)){
77
}
78
return z;
79
},foldr1:function(a,f,o){
80
if(typeof a=="string"){
81
a=a.split("");
82
}
83
o=o||d.global;
84
f=df.lambda(f);
85
var n=a.length,z=a[n-1],i=n-1;
86
for(;i>0;--i,z=f.call(o,z,a[i],i,a)){
87
}
88
return z;
89
},reduce:function(a,f,z){
90
return arguments.length<3?df.foldl1(a,f):df.foldl(a,f,z);
91
},reduceRight:function(a,f,z){
92
return arguments.length<3?df.foldr1(a,f):df.foldr(a,f,z);
93
},unfold:function(pr,f,g,z,o){
94
o=o||d.global;
95
f=df.lambda(f);
96
g=df.lambda(g);
97
pr=df.lambda(pr);
98
var t=[];
99
for(;!pr.call(o,z);t.push(f.call(o,z)),z=g.call(o,z)){
100
}
101
return t;
102
}});
103
})();
104
}