linrec.js 4.7 KB

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