linrec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // wrapped by build app
  2. define("dojox/lang/functional/linrec", ["dijit","dojo","dojox","dojo/require!dojox/lang/functional/lambda,dojox/lang/functional/util"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.lang.functional.linrec");
  4. dojo.require("dojox.lang.functional.lambda");
  5. dojo.require("dojox.lang.functional.util");
  6. // This module provides recursion combinators:
  7. // - a linear recursion combinator.
  8. // Acknoledgements:
  9. // - recursion combinators are inspired by Manfred von Thun's article
  10. // "Recursion Theory and Joy"
  11. // (http://www.latrobe.edu.au/philosophy/phimvt/joy/j05cmp.html)
  12. // Notes:
  13. // - recursion combinators produce a function, which implements
  14. // their respective recusion patterns. String lambdas are inlined, if possible.
  15. (function(){
  16. var df = dojox.lang.functional, inline = df.inlineLambda,
  17. _x ="_x", _r_y_a = ["_r", "_y.a"];
  18. df.linrec = function(
  19. /*Function|String|Array*/ cond,
  20. /*Function|String|Array*/ then,
  21. /*Function|String|Array*/ before,
  22. /*Function|String|Array*/ after){
  23. // summary:
  24. // Generates a function for the linear recursion pattern.
  25. // All parameter functions are called in the context of "this" object.
  26. // cond:
  27. // The lambda expression, which is used to detect the termination of recursion.
  28. // It accepts the same parameter as the generated recursive function itself.
  29. // This function should return "true", if the recursion should be stopped,
  30. // and the "then" part should be executed. Otherwise the recursion will proceed.
  31. // then:
  32. // The lambda expression, which is called upon termination of the recursion.
  33. // It accepts the same parameters as the generated recursive function itself.
  34. // The returned value will be returned as the value of the generated function.
  35. // before:
  36. // The lambda expression, which is called before the recursive step.
  37. // It accepts the same parameter as the generated recursive function itself.
  38. // The returned value should be an array, which is used to call
  39. // the generated function recursively.
  40. // above:
  41. // The lambda expression, which is called after the recursive step.
  42. // It accepts two parameters: the returned value from the recursive step, and
  43. // the original array of parameters used with all other functions.
  44. // The returned value will be returned as the value of the generated function.
  45. var c, t, b, a, cs, ts, bs, as, dict1 = {}, dict2 = {},
  46. add2dict = function(x){ dict1[x] = 1; };
  47. if(typeof cond == "string"){
  48. cs = inline(cond, _x, add2dict);
  49. }else{
  50. c = df.lambda(cond);
  51. cs = "_c.apply(this, _x)";
  52. dict2["_c=_t.c"] = 1;
  53. }
  54. if(typeof then == "string"){
  55. ts = inline(then, _x, add2dict);
  56. }else{
  57. t = df.lambda(then);
  58. ts = "_t.t.apply(this, _x)";
  59. }
  60. if(typeof before == "string"){
  61. bs = inline(before, _x, add2dict);
  62. }else{
  63. b = df.lambda(before);
  64. bs = "_b.apply(this, _x)";
  65. dict2["_b=_t.b"] = 1;
  66. }
  67. if(typeof after == "string"){
  68. as = inline(after, _r_y_a, add2dict);
  69. }else{
  70. a = df.lambda(after);
  71. as = "_a.call(this, _r, _y.a)";
  72. dict2["_a=_t.a"] = 1;
  73. }
  74. var locals1 = df.keys(dict1), locals2 = df.keys(dict2),
  75. f = new Function([], "var _x=arguments,_y,_r".concat( // Function
  76. locals1.length ? "," + locals1.join(",") : "",
  77. locals2.length ? ",_t=_x.callee," + locals2.join(",") : t ? ",_t=_x.callee" : "",
  78. ";for(;!",
  79. cs,
  80. ";_x=",
  81. bs,
  82. "){_y={p:_y,a:_x}}_r=",
  83. ts,
  84. ";for(;_y;_y=_y.p){_r=",
  85. as,
  86. "}return _r"
  87. ));
  88. if(c){ f.c = c; }
  89. if(t){ f.t = t; }
  90. if(b){ f.b = b; }
  91. if(a){ f.a = a; }
  92. return f;
  93. };
  94. })();
  95. /*
  96. For documentation only:
  97. 1) The original recursive version:
  98. var linrec1 = function(cond, then, before, after){
  99. var cond = df.lambda(cond),
  100. then = df.lambda(then),
  101. before = df.lambda(before),
  102. after = df.lambda(after);
  103. return function(){
  104. if(cond.apply(this, arguments)){
  105. return then.apply(this, arguments);
  106. }
  107. var args = before.apply(this, arguments);
  108. var ret = arguments.callee.apply(this, args);
  109. return after.call(this, ret, arguments);
  110. };
  111. };
  112. 2) The original iterative version (before minification and inlining):
  113. var linrec2 = function(cond, then, before, after){
  114. var cond = df.lambda(cond),
  115. then = df.lambda(then),
  116. before = df.lambda(before),
  117. after = df.lambda(after);
  118. return function(){
  119. var args = arguments, top, ret;
  120. // 1st part
  121. for(; !cond.apply(this, args); args = before.apply(this, args)){
  122. top = {prev: top, args: args};
  123. }
  124. ret = then.apply(this, args);
  125. //2nd part
  126. for(; top; top = top.prev){
  127. ret = after.call(this, ret, top.args);
  128. }
  129. return ret;
  130. };
  131. };
  132. */
  133. });