root / trunk / web / dojo / dojox / lang / functional / scan.js @ 12
History | View | Annotate | Download (1.8 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.scan"]){ |
||
9 | dojo._hasResource["dojox.lang.functional.scan"]=true; |
||
10 | dojo.provide("dojox.lang.functional.scan");
|
||
11 | dojo.require("dojox.lang.functional.lambda");
|
||
12 | (function(){
|
||
13 | var d=dojo,df=dojox.lang.functional,_1={};
|
||
14 | d.mixin(df,{scanl: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 t,n,i;
|
||
21 | if(d.isArray(a)){
|
||
22 | t=new Array((n=a.length)+1); |
||
23 | t[0]=z;
|
||
24 | for(i=0;i<n;z=f.call(o,z,a[i],i,a),t[++i]=z){ |
||
25 | } |
||
26 | }else{
|
||
27 | if(typeof a.hasNext=="function"&&typeof a.next=="function"){ |
||
28 | t=[z]; |
||
29 | for(i=0;a.hasNext();t.push(z=f.call(o,z,a.next(),i++,a))){ |
||
30 | } |
||
31 | }else{
|
||
32 | t=[z]; |
||
33 | for(i in a){ |
||
34 | if(!(i in _1)){ |
||
35 | t.push(z=f.call(o,z,a[i],i,a)); |
||
36 | } |
||
37 | } |
||
38 | } |
||
39 | } |
||
40 | return t;
|
||
41 | },scanl1:function(a,f,o){ |
||
42 | if(typeof a=="string"){ |
||
43 | a=a.split("");
|
||
44 | } |
||
45 | o=o||d.global; |
||
46 | f=df.lambda(f); |
||
47 | var t,n,z,_2=true; |
||
48 | if(d.isArray(a)){
|
||
49 | t=new Array(n=a.length);
|
||
50 | t[0]=z=a[0]; |
||
51 | for(var i=1;i<n;t[i]=z=f.call(o,z,a[i],i,a),++i){ |
||
52 | } |
||
53 | }else{
|
||
54 | if(typeof a.hasNext=="function"&&typeof a.next=="function"){ |
||
55 | if(a.hasNext()){
|
||
56 | t=[z=a.next()]; |
||
57 | for(var i=1;a.hasNext();t.push(z=f.call(o,z,a.next(),i++,a))){ |
||
58 | } |
||
59 | } |
||
60 | }else{
|
||
61 | for(var i in a){ |
||
62 | if(!(i in _1)){ |
||
63 | if(_2){
|
||
64 | t=[z=a[i]]; |
||
65 | _2=false;
|
||
66 | }else{
|
||
67 | t.push(z=f.call(o,z,a[i],i,a)); |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | return t;
|
||
74 | },scanr:function(a,f,z,o){ |
||
75 | if(typeof a=="string"){ |
||
76 | a=a.split("");
|
||
77 | } |
||
78 | o=o||d.global; |
||
79 | f=df.lambda(f); |
||
80 | var n=a.length,t=new Array(n+1),i=n; |
||
81 | t[n]=z; |
||
82 | for(;i>0;--i,z=f.call(o,z,a[i],i,a),t[i]=z){ |
||
83 | } |
||
84 | return t;
|
||
85 | },scanr1:function(a,f,o){ |
||
86 | if(typeof a=="string"){ |
||
87 | a=a.split("");
|
||
88 | } |
||
89 | o=o||d.global; |
||
90 | f=df.lambda(f); |
||
91 | var n=a.length,t=new Array(n),z=a[n-1],i=n-1; |
||
92 | t[i]=z; |
||
93 | for(;i>0;--i,z=f.call(o,z,a[i],i,a),t[i]=z){ |
||
94 | } |
||
95 | return t;
|
||
96 | }}); |
||
97 | })(); |
||
98 | } |