sequence.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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.sequence"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.functional.sequence"] = true;
  8. dojo.provide("dojox.lang.functional.sequence");
  9. dojo.require("dojox.lang.functional.lambda");
  10. // This module adds high-level functions and related constructs:
  11. // - sequence generators
  12. // If you want more general sequence builders check out listcomp.js and
  13. // unfold() (in fold.js).
  14. // Defined methods:
  15. // - take any valid lambda argument as the functional argument
  16. (function(){
  17. var d = dojo, df = dojox.lang.functional;
  18. d.mixin(df, {
  19. // sequence generators
  20. repeat: function(/*Number*/ n, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){
  21. // summary: builds an array by repeatedly applying a unary function N times
  22. // with a seed value Z. N should be greater than 0.
  23. o = o || d.global; f = df.lambda(f);
  24. var t = new Array(n), i = 1;
  25. t[0] = z;
  26. for(; i < n; t[i] = z = f.call(o, z), ++i);
  27. return t; // Array
  28. },
  29. until: function(/*Function|String|Array*/ pr, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){
  30. // summary: builds an array by repeatedly applying a unary function with
  31. // a seed value Z until the predicate is satisfied.
  32. o = o || d.global; f = df.lambda(f); pr = df.lambda(pr);
  33. var t = [];
  34. for(; !pr.call(o, z); t.push(z), z = f.call(o, z));
  35. return t; // Array
  36. }
  37. });
  38. })();
  39. }