multirec.js 5.3 KB

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