root / trunk / web / dojo / dojox / lang / functional / lambda.js @ 10
History | View | Annotate | Download (2.03 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.lambda"]){ |
||
9 | dojo._hasResource["dojox.lang.functional.lambda"]=true; |
||
10 | dojo.provide("dojox.lang.functional.lambda");
|
||
11 | (function(){
|
||
12 | var df=dojox.lang.functional,_1={};
|
||
13 | var _2="ab".split(/a*/).length>1?String.prototype.split:function(_3){ |
||
14 | var r=this.split.call(this,_3),m=_3.exec(this); |
||
15 | if(m&&m.index==0){ |
||
16 | r.unshift("");
|
||
17 | } |
||
18 | return r;
|
||
19 | }; |
||
20 | var _4=function(s){ |
||
21 | var _5=[],_6=_2.call(s,/\s*->\s*/m); |
||
22 | if(_6.length>1){ |
||
23 | while(_6.length){
|
||
24 | s=_6.pop(); |
||
25 | _5=_6.pop().split(/\s*,\s*|\s+/m);
|
||
26 | if(_6.length){
|
||
27 | _6.push("(function("+_5+"){return ("+s+")})"); |
||
28 | } |
||
29 | } |
||
30 | }else{
|
||
31 | if(s.match(/\b_\b/)){ |
||
32 | _5=["_"];
|
||
33 | }else{
|
||
34 | var l=s.match(/^\s*(?:[+*\/%&|\^\.=<>]|!=)/m),r=s.match(/[+\-*\/%&|\^\.=<>!]\s*$/m); |
||
35 | if(l||r){
|
||
36 | if(l){
|
||
37 | _5.push("$1");
|
||
38 | s="$1"+s;
|
||
39 | } |
||
40 | if(r){
|
||
41 | _5.push("$2");
|
||
42 | s=s+"$2";
|
||
43 | } |
||
44 | }else{
|
||
45 | var _7=s.replace(/(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|this|true|false|null|undefined|typeof|instanceof|in|delete|new|void|arguments|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|escape|eval|isFinite|isNaN|parseFloat|parseInt|unescape|dojo|dijit|dojox|window|document|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/g,"").match(/([a-z_$][a-z_$\d]*)/gi)||[],t={}; |
||
46 | dojo.forEach(_7,function(v){
|
||
47 | if(!(v in t)){ |
||
48 | _5.push(v); |
||
49 | t[v]=1;
|
||
50 | } |
||
51 | }); |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | return {args:_5,body:s}; |
||
56 | }; |
||
57 | var _8=function(a){ |
||
58 | return a.length?function(){ |
||
59 | var i=a.length-1,x=df.lambda(a[i]).apply(this,arguments); |
||
60 | for(--i;i>=0;--i){ |
||
61 | x=df.lambda(a[i]).call(this,x);
|
||
62 | } |
||
63 | return x;
|
||
64 | }:function(x){
|
||
65 | return x;
|
||
66 | }; |
||
67 | }; |
||
68 | dojo.mixin(df,{rawLambda:function(s){ |
||
69 | return _4(s);
|
||
70 | },buildLambda:function(s){ |
||
71 | s=_4(s); |
||
72 | return "function("+s.args.join(",")+"){return ("+s.body+");}"; |
||
73 | },lambda:function(s){ |
||
74 | if(typeof s=="function"){ |
||
75 | return s;
|
||
76 | } |
||
77 | if(s instanceof Array){ |
||
78 | return _8(s);
|
||
79 | } |
||
80 | if(s in _1){ |
||
81 | return _1[s];
|
||
82 | } |
||
83 | s=_4(s); |
||
84 | return _1[s]=new Function(s.args,"return ("+s.body+");"); |
||
85 | },clearLambdaCache:function(){ |
||
86 | _1={}; |
||
87 | }}); |
||
88 | })(); |
||
89 | } |