zip.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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.zip"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.lang.functional.zip"] = true;
  8. dojo.provide("dojox.lang.functional.zip");
  9. // This module adds high-level functions and related constructs:
  10. // - zip combiners
  11. // Defined methods:
  12. // - operate on dense arrays
  13. (function(){
  14. var df = dojox.lang.functional;
  15. dojo.mixin(df, {
  16. // combiners
  17. zip: function(){
  18. // summary: returns an array of arrays, where the i-th array
  19. // contains the i-th element from each of the argument arrays.
  20. // description: This is the venerable zip combiner (for example,
  21. // see Python documentation for general details). The returned
  22. // array is truncated to match the length of the shortest input
  23. // array.
  24. var n = arguments[0].length, m = arguments.length, i = 1, t = new Array(n), j, p;
  25. for(; i < m; n = Math.min(n, arguments[i++].length));
  26. for(i = 0; i < n; ++i){
  27. p = new Array(m);
  28. for(j = 0; j < m; p[j] = arguments[j][i], ++j);
  29. t[i] = p;
  30. }
  31. return t; // Array
  32. },
  33. unzip: function(/*Array*/ a){
  34. // summary: similar to dojox.lang.functional.zip(), but takes
  35. // a single array of arrays as the input.
  36. // description: This function is similar to dojox.lang.functional.zip()
  37. // and can be used to unzip objects packed by
  38. // dojox.lang.functional.zip(). It is here mostly to provide
  39. // a short-cut for the different method signature.
  40. return df.zip.apply(null, a); // Array
  41. }
  42. });
  43. })();
  44. }