binrec.js 5.4 KB

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