Debian Codegraph @

dojox/lang/functional/listcomp.js

raw symbols SCIP
1 dojo.provide("dojox.lang.functional.listcomp");
2
3 // This module adds high-level functions and related constructs:
4 // - list comprehensions similar to JavaScript 1.7
5
6 // Notes:
7 // - listcomp() produces functions, which after the compilation step are
8 // as fast as regular JS functions (at least theoretically).
9
10 (function(){
11 var g_re = /\bfor\b|\bif\b/gm;
12
13 var listcomp = function(/*String*/ s){
14 var frag = s.split(g_re), act = s.match(g_re),
15 head = ["var r = [];"], tail = [], i = 0, l = act.length;
16 while(i < l){
17 var a = act[i], f = frag[++i];
18 if(a == "for" && !/^\s*\(\s*(;|var)/.test(f)){
19 f = f.replace(/^\s*\(/, "(var ");
20 }
21 head.push(a, f, "{");
22 tail.push("}");
23 }
24 return head.join("") + "r.push(" + frag[0] + ");" + tail.join("") + "return r;"; // String
25 };
26
27 dojo.mixin(dojox.lang.functional, {
28 buildListcomp: function(/*String*/ s){
29 // summary:
30 // builds a function from a text snippet, which represents a valid
31 // JS 1.7 list comprehension, returns a string, which represents the function.
32 // description:
33 // This method returns a textual representation of a function
34 // built from the list comprehension text snippet (conformant to JS 1.7).
35 // It is meant to be evaled in the proper context, so local variable can be
36 // pulled from the environment.
37 return "function(){" + listcomp(s) + "}"; // String
38 },
39 compileListcomp: function(/*String*/ s){
40 // summary:
41 // builds a function from a text snippet, which represents a valid
42 // JS 1.7 list comprehension, returns a function object.
43 // description:
44 // This method returns a function built from the list
45 // comprehension text snippet (conformant to JS 1.7). It is meant to be
46 // reused several times.
47 return new Function([], listcomp(s)); // Function
48 },
49 listcomp: function(/*String*/ s){
50 // summary:
51 // executes the list comprehension building an array.
52 return (new Function([], listcomp(s)))(); // Array
53 }
54 });
55 })();