binrec.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.binrec"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.functional.binrec"] = true;
  8. dojo.provide("dojox.lang.functional.binrec");
  9. dojo.require("dojox.lang.functional.lambda");
  10. dojo.require("dojox.lang.functional.util");
  11. // This module provides recursion combinators:
  12. // - a binary 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", _z_r_r_z_a = ["_z.r", "_r", "_z.a"];
  23. df.binrec = 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 binary 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 of two variable, which are used to call
  44. // the generated function recursively twice in row starting from the first item.
  45. // above:
  46. // The lambda expression, which is called after the recursive step.
  47. // It accepts three parameters: two returned values from recursive steps, 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.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, _z_r_r_z_a, add2dict);
  74. }else{
  75. a = df.lambda(after);
  76. as = "_a.call(this, _z.r, _r, _z.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,_z,_r".concat( // Function
  81. locals1.length ? "," + locals1.join(",") : "",
  82. locals2.length ? ",_t=_x.callee," + locals2.join(",") : "",
  83. t ? (locals2.length ? ",_t=_t.t" : "_t=_x.callee.t") : "",
  84. ";while(!",
  85. cs,
  86. "){_r=",
  87. bs,
  88. ";_y={p:_y,a:_r[1]};_z={p:_z,a:_x};_x=_r[0]}for(;;){do{_r=",
  89. ts,
  90. ";if(!_z)return _r;while(\"r\" in _z){_r=",
  91. as,
  92. ";if(!(_z=_z.p))return _r}_z.r=_r;_x=_y.a;_y=_y.p}while(",
  93. cs,
  94. ");do{_r=",
  95. bs,
  96. ";_y={p:_y,a:_r[1]};_z={p:_z,a:_x};_x=_r[0]}while(!",
  97. cs,
  98. ")}"
  99. ));
  100. if(c){ f.c = c; }
  101. if(t){ f.t = t; }
  102. if(b){ f.b = b; }
  103. if(a){ f.a = a; }
  104. return f;
  105. };
  106. })();
  107. /*
  108. For documentation only:
  109. 1) The original recursive version:
  110. var binrec1 = function(cond, then, before, after){
  111. var cond = df.lambda(cond),
  112. then = df.lambda(then),
  113. before = df.lambda(before),
  114. after = df.lambda(after);
  115. return function(){
  116. if(cond.apply(this, arguments)){
  117. return then.apply(this, arguments);
  118. }
  119. var args = before.apply(this, arguments);
  120. var ret1 = arguments.callee.apply(this, args[0]);
  121. var ret2 = arguments.callee.apply(this, args[1]);
  122. return after.call(this, ret1, ret2, arguments);
  123. };
  124. };
  125. 2) The original iterative version (before minification and inlining):
  126. var binrec2 = function(cond, then, before, after){
  127. var cond = df.lambda(cond),
  128. then = df.lambda(then),
  129. before = df.lambda(before),
  130. after = df.lambda(after);
  131. return function(){
  132. var top1, top2, ret, args = arguments;
  133. // first part: start the pump
  134. while(!cond.apply(this, args)){
  135. ret = before.apply(this, args);
  136. top1 = {prev: top1, args: ret[1]};
  137. top2 = {prev: top2, args: args};
  138. args = ret[0];
  139. }
  140. for(;;){
  141. // second part: mop up
  142. do{
  143. ret = then.apply(this, args);
  144. if(!top2){
  145. return ret;
  146. }
  147. while("ret" in top2){
  148. ret = after.call(this, top2.ret, ret, top2.args);
  149. if(!(top2 = top2.prev)){
  150. return ret;
  151. }
  152. }
  153. top2.ret = ret;
  154. args = top1.args;
  155. top1 = top1.prev;
  156. }while(cond.apply(this, args));
  157. // first part (encore)
  158. do{
  159. ret = before.apply(this, args);
  160. top1 = {prev: top1, args: ret[1]};
  161. top2 = {prev: top2, args: args};
  162. args = ret[0];
  163. }while(!cond.apply(this, args));
  164. }
  165. };
  166. };
  167. */
  168. }