[483] | 1 | define(["dojo/_base/lang", "dojo/_base/kernel" ,"./lambda"], |
---|
| 2 | function(lang, kernel, df){ |
---|
| 3 | // This module adds high-level functions and related constructs: |
---|
| 4 | // - reversed versions of array-processing functions similar to standard JS functions |
---|
| 5 | |
---|
| 6 | // Notes: |
---|
| 7 | // - this module provides reversed versions of standard array-processing functions: |
---|
| 8 | // forEachRev, mapRev, filterRev |
---|
| 9 | |
---|
| 10 | // Defined methods: |
---|
| 11 | // - take any valid lambda argument as the functional argument |
---|
| 12 | // - operate on dense arrays |
---|
| 13 | // - take a string as the array argument |
---|
| 14 | |
---|
| 15 | lang.mixin(df, { |
---|
| 16 | // JS 1.6 standard array functions, which can take a lambda as a parameter. |
---|
| 17 | // Consider using dojo._base.array functions, if you don't need the lambda support. |
---|
| 18 | filterRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 19 | // summary: |
---|
| 20 | // creates a new array with all elements that pass the test |
---|
| 21 | // implemented by the provided function. |
---|
| 22 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 23 | o = o || kernel.global; f = df.lambda(f); |
---|
| 24 | var t = [], v, i = a.length - 1; |
---|
| 25 | for(; i >= 0; --i){ |
---|
| 26 | v = a[i]; |
---|
| 27 | if(f.call(o, v, i, a)){ t.push(v); } |
---|
| 28 | } |
---|
| 29 | return t; // Array |
---|
| 30 | }, |
---|
| 31 | forEachRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 32 | // summary: |
---|
| 33 | // executes a provided function once per array element. |
---|
| 34 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 35 | o = o || kernel.global; f = df.lambda(f); |
---|
| 36 | for(var i = a.length - 1; i >= 0; f.call(o, a[i], i, a), --i); |
---|
| 37 | }, |
---|
| 38 | mapRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 39 | // summary: |
---|
| 40 | // creates a new array with the results of calling |
---|
| 41 | // a provided function on every element in this array. |
---|
| 42 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 43 | o = o || kernel.global; f = df.lambda(f); |
---|
| 44 | var n = a.length, t = new Array(n), i = n - 1, j = 0; |
---|
| 45 | for(; i >= 0; t[j++] = f.call(o, a[i], i, a), --i); |
---|
| 46 | return t; // Array |
---|
| 47 | }, |
---|
| 48 | everyRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 49 | // summary: |
---|
| 50 | // tests whether all elements in the array pass the test |
---|
| 51 | // implemented by the provided function. |
---|
| 52 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 53 | o = o || kernel.global; f = df.lambda(f); |
---|
| 54 | for(var i = a.length - 1; i >= 0; --i){ |
---|
| 55 | if(!f.call(o, a[i], i, a)){ |
---|
| 56 | return false; // Boolean |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | return true; // Boolean |
---|
| 60 | }, |
---|
| 61 | someRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ |
---|
| 62 | // summary: |
---|
| 63 | // tests whether some element in the array passes the test |
---|
| 64 | // implemented by the provided function. |
---|
| 65 | if(typeof a == "string"){ a = a.split(""); } |
---|
| 66 | o = o || kernel.global; f = df.lambda(f); |
---|
| 67 | for(var i = a.length - 1; i >= 0; --i){ |
---|
| 68 | if(f.call(o, a[i], i, a)){ |
---|
| 69 | return true; // Boolean |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | return false; // Boolean |
---|
| 73 | } |
---|
| 74 | }); |
---|
| 75 | |
---|
| 76 | return df; |
---|
| 77 | }); |
---|