multirec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.multirec"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.functional.multirec"] = true;
  8. dojo.provide("dojox.lang.functional.multirec");
  9. dojo.require("dojox.lang.functional.lambda");
  10. dojo.require("dojox.lang.functional.util");
  11. // This module provides recursion combinators:
  12. // - a multi-way 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", _y_r_y_o = ["_y.r", "_y.o"];
  23. df.multirec = 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 multi-way 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. Each member of the array should be
  45. // an array of parameters. The length of it defines how many times
  46. // the generated function is called recursively.
  47. // above:
  48. // The lambda expression, which is called after the recursive step.
  49. // It accepts two parameters: the array of returned values from recursive steps,
  50. // and the original array of parameters used with all other functions.
  51. // The returned value will be returned as the value of the generated function.
  52. var c, t, b, a, cs, ts, bs, as, dict1 = {}, dict2 = {},
  53. add2dict = function(x){ dict1[x] = 1; };
  54. if(typeof cond == "string"){
  55. cs = inline(cond, _x, add2dict);
  56. }else{
  57. c = df.lambda(cond);
  58. cs = "_c.apply(this, _x)";
  59. dict2["_c=_t.c"] = 1;
  60. }
  61. if(typeof then == "string"){
  62. ts = inline(then, _x, add2dict);
  63. }else{
  64. t = df.lambda(then);
  65. ts = "_t.apply(this, _x)";
  66. }
  67. if(typeof before == "string"){
  68. bs = inline(before, _x, add2dict);
  69. }else{
  70. b = df.lambda(before);
  71. bs = "_b.apply(this, _x)";
  72. dict2["_b=_t.b"] = 1;
  73. }
  74. if(typeof after == "string"){
  75. as = inline(after, _y_r_y_o, add2dict);
  76. }else{
  77. a = df.lambda(after);
  78. as = "_a.call(this, _y.r, _y.o)";
  79. dict2["_a=_t.a"] = 1;
  80. }
  81. var locals1 = df.keys(dict1), locals2 = df.keys(dict2),
  82. f = new Function([], "var _y={a:arguments},_x,_r,_z,_i".concat( // Function
  83. locals1.length ? "," + locals1.join(",") : "",
  84. locals2.length ? ",_t=arguments.callee," + locals2.join(",") : "",
  85. t ? (locals2.length ? ",_t=_t.t" : "_t=arguments.callee.t") : "",
  86. ";for(;;){for(;;){if(_y.o){_r=",
  87. as,
  88. ";break}_x=_y.a;if(",
  89. cs,
  90. "){_r=",
  91. ts,
  92. ";break}_y.o=_x;_x=",
  93. bs,
  94. ";_y.r=[];_z=_y;for(_i=_x.length-1;_i>=0;--_i){_y={p:_y,a:_x[_i],z:_z}}}if(!(_z=_y.z)){return _r}_z.r.push(_r);_y=_y.p}"
  95. ));
  96. if(c){ f.c = c; }
  97. if(t){ f.t = t; }
  98. if(b){ f.b = b; }
  99. if(a){ f.a = a; }
  100. return f;
  101. };
  102. })();
  103. /*
  104. For documentation only:
  105. 1) The original recursive version:
  106. var multirec1 = function(cond, then, before, after){
  107. var cond = df.lambda(cond),
  108. then = df.lambda(then),
  109. before = df.lambda(before),
  110. after = df.lambda(after);
  111. return function(){
  112. if(cond.apply(this, arguments)){
  113. return then.apply(this, arguments);
  114. }
  115. var args = before.apply(this, arguments),
  116. ret = new Array(args.length);
  117. for(var i = 0; i < args.length; ++i){
  118. ret[i] = arguments.callee.apply(this, args[i]);
  119. }
  120. return after.call(this, ret, arguments);
  121. };
  122. };
  123. 2) The original iterative version (before minification and inlining):
  124. var multirec2 = function(cond, then, before, after){
  125. var cond = df.lambda(cond),
  126. then = df.lambda(then),
  127. before = df.lambda(before),
  128. after = df.lambda(after);
  129. return function(){
  130. var top = {args: arguments}, args, ret, parent, i;
  131. for(;;){
  132. for(;;){
  133. if(top.old){
  134. ret = after.call(this, top.ret, top.old);
  135. break;
  136. }
  137. args = top.args;
  138. if(cond.apply(this, args)){
  139. ret = then.apply(this, args);
  140. break;
  141. }
  142. top.old = args;
  143. args = before.apply(this, args);
  144. top.ret = [];
  145. parent = top;
  146. for(i = args.length - 1; i >= 0; --i){
  147. top = {prev: top, args: args[i], parent: parent};
  148. }
  149. }
  150. if(!(parent = top.parent)){
  151. return ret;
  152. }
  153. parent.ret.push(ret);
  154. top = top.prev;
  155. }
  156. };
  157. };
  158. */
  159. }