zip.js 1.4 KB

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