Debian Codegraph @

dojox/lang/functional/numrec.js

raw symbols SCIP
1 dojo.provide("dojox.lang.functional.numrec");
2
3 dojo.require("dojox.lang.functional.lambda");
4 dojo.require("dojox.lang.functional.util");
5
6 // This module provides recursion combinators:
7 // - a simplified numeric linear recursion combinator.
8
9 // Acknowledgements:
10 // - recursion combinators are inspired by Manfred von Thun's article
11 // "Recursion Theory and Joy"
12 // (http://www.latrobe.edu.au/philosophy/phimvt/joy/j05cmp.html)
13
14 // Notes:
15 // - recursion combinators produce a function, which implements
16 // their respective recusion patterns. String lambdas are inlined, if possible.
17
18 (function(){
19 var df = dojox.lang.functional, inline = df.inlineLambda,
20 _r_i = ["_r", "_i"];
21
22 df.numrec = function(/*Object*/ then, /*Function|String|Array*/ after){
23 // summary:
24 // Generates a function for the simplified numeric linear recursion pattern.
25 // All parameter functions are called in the context of "this" object.
26 // description:
27 // This is a simplification of the linear recursion combinator:
28 // - the generated function takes one numeric parameter "x",
29 // - the "cond" is fixed and checks for 0.
30 // - the "before" is fixed and the generated function is called with "x - 1".
31 // - the "above is called with two parameters: the return from the generated
32 // function, and with "x".
33 // - as you can see the recursion is done by decreasing the parameter,
34 // and calling itself until it reaches 0.
35 // then:
36 // The value, which is used upon termination of the recursion.
37 // It will be returned as the value of the generated function.
38 // above:
39 // The lambda expression, which is called after the recursive step.
40 // It accepts two parameters: the returned value from the recursive step, and
41 // the original parameter. The returned value will be returned as the value of
42 // the generated function.
43
44 var a, as, dict = {},
45 add2dict = function(x){ dict[x] = 1; };
46 if(typeof after == "string"){
47 as = inline(after, _r_i, add2dict);
48 }else{
49 a = df.lambda(after);
50 as = "_a.call(this, _r, _i)";
51 }
52 var locals = df.keys(dict),
53 f = new Function(["_x"], "var _t=arguments.callee,_r=_t.t,_i".concat( // Function
54 locals.length ? "," + locals.join(",") : "",
55 a ? ",_a=_t.a" : "",
56 ";for(_i=1;_i<=_x;++_i){_r=",
57 as,
58 "}return _r"
59 ));
60 f.t = then;
61 if(a){ f.a = a; }
62 return f;
63 };
64 })();
65
66 /*
67 For documentation only:
68
69 1) The original recursive version:
70
71 var numrec1 = function(then, after){
72 var after = df.lambda(after);
73 return function(x){
74 return x ? after.call(this, arguments.callee.call(this, x - 1), x) : then;
75 };
76 };
77
78 2) The original iterative version (before minification and inlining):
79
80 var numrec2 = function(then, after){
81 var after = df.lambda(after);
82 return function(x){
83 var ret = then, i;
84 for(i = 1; i <= x; ++i){
85 ret = after.call(this, ret, i);
86 }
87 return ret;
88 };
89 };
90
91 */