tailrec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // wrapped by build app
  2. define("dojox/lang/functional/tailrec", ["dijit","dojo","dojox","dojo/require!dojox/lang/functional/lambda,dojox/lang/functional/util"], function(dijit,dojo,dojox){
  3. dojo.provide("dojox.lang.functional.tailrec");
  4. dojo.require("dojox.lang.functional.lambda");
  5. dojo.require("dojox.lang.functional.util");
  6. // This module provides recursion combinators:
  7. // - a tail 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, _x ="_x";
  17. df.tailrec = function(
  18. /*Function|String|Array*/ cond,
  19. /*Function|String|Array*/ then,
  20. /*Function|String|Array*/ before){
  21. // summary:
  22. // Generates a function for the tail recursion pattern. This is the simplified
  23. // version of the linear recursive combinator without the "after" function,
  24. // and with the modified "before" function. All parameter functions are called
  25. // 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. // and returns an array of arguments for the next recursive call of
  39. // the generated function.
  40. var c, t, b, cs, ts, bs, dict1 = {}, dict2 = {},
  41. add2dict = function(x){ dict1[x] = 1; };
  42. if(typeof cond == "string"){
  43. cs = inline(cond, _x, add2dict);
  44. }else{
  45. c = df.lambda(cond);
  46. cs = "_c.apply(this, _x)";
  47. dict2["_c=_t.c"] = 1;
  48. }
  49. if(typeof then == "string"){
  50. ts = inline(then, _x, add2dict);
  51. }else{
  52. t = df.lambda(then);
  53. ts = "_t.t.apply(this, _x)";
  54. }
  55. if(typeof before == "string"){
  56. bs = inline(before, _x, add2dict);
  57. }else{
  58. b = df.lambda(before);
  59. bs = "_b.apply(this, _x)";
  60. dict2["_b=_t.b"] = 1;
  61. }
  62. var locals1 = df.keys(dict1), locals2 = df.keys(dict2),
  63. f = new Function([], "var _x=arguments,_t=_x.callee,_c=_t.c,_b=_t.b".concat( // Function
  64. locals1.length ? "," + locals1.join(",") : "",
  65. locals2.length ? ",_t=_x.callee," + locals2.join(",") : t ? ",_t=_x.callee" : "",
  66. ";for(;!",
  67. cs,
  68. ";_x=",
  69. bs,
  70. ");return ",
  71. ts
  72. ));
  73. if(c){ f.c = c; }
  74. if(t){ f.t = t; }
  75. if(b){ f.b = b; }
  76. return f;
  77. };
  78. })();
  79. /*
  80. For documentation only:
  81. 1) The original recursive version:
  82. var tailrec1 = function(cond, then, before){
  83. var cond = df.lambda(cond),
  84. then = df.lambda(then),
  85. before = df.lambda(before);
  86. return function(){
  87. if(cond.apply(this, arguments)){
  88. return then.apply(this, arguments);
  89. }
  90. var args = before.apply(this, arguments);
  91. return arguments.callee.apply(this, args);
  92. };
  93. };
  94. 2) The original iterative version (before minification and inlining):
  95. var tailrec2 = function(cond, then, before){
  96. var cond = df.lambda(cond),
  97. then = df.lambda(then),
  98. before = df.lambda(before);
  99. return function(){
  100. var args = arguments;
  101. for(; !cond.apply(this, args); args = before.apply(this, args));
  102. return then.apply(this, args);
  103. };
  104. };
  105. */
  106. });