From e44a7e37b6c7b5961adaffc62b9042b8d442938e Mon Sep 17 00:00:00 2001 From: mensonge Date: Thu, 13 Nov 2008 09:49:11 +0000 Subject: New feature: basic Ajax suggestion for tags and implementation of Dojo toolkit git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@151 b3834d28-1941-0410-a4f8-b48e95affb8f --- includes/js/dojox/lang/functional/array.js | 113 ++++++++++++++++++++++++++ includes/js/dojox/lang/functional/curry.js | 96 ++++++++++++++++++++++ includes/js/dojox/lang/functional/fold.js | 87 ++++++++++++++++++++ includes/js/dojox/lang/functional/lambda.js | 110 +++++++++++++++++++++++++ includes/js/dojox/lang/functional/listcomp.js | 54 ++++++++++++ includes/js/dojox/lang/functional/object.js | 48 +++++++++++ includes/js/dojox/lang/functional/reversed.js | 79 ++++++++++++++++++ includes/js/dojox/lang/functional/scan.js | 84 +++++++++++++++++++ includes/js/dojox/lang/functional/sequence.js | 38 +++++++++ includes/js/dojox/lang/functional/zip.js | 45 ++++++++++ 10 files changed, 754 insertions(+) create mode 100644 includes/js/dojox/lang/functional/array.js create mode 100644 includes/js/dojox/lang/functional/curry.js create mode 100644 includes/js/dojox/lang/functional/fold.js create mode 100644 includes/js/dojox/lang/functional/lambda.js create mode 100644 includes/js/dojox/lang/functional/listcomp.js create mode 100644 includes/js/dojox/lang/functional/object.js create mode 100644 includes/js/dojox/lang/functional/reversed.js create mode 100644 includes/js/dojox/lang/functional/scan.js create mode 100644 includes/js/dojox/lang/functional/sequence.js create mode 100644 includes/js/dojox/lang/functional/zip.js (limited to 'includes/js/dojox/lang/functional') diff --git a/includes/js/dojox/lang/functional/array.js b/includes/js/dojox/lang/functional/array.js new file mode 100644 index 0000000..45c2613 --- /dev/null +++ b/includes/js/dojox/lang/functional/array.js @@ -0,0 +1,113 @@ +if(!dojo._hasResource["dojox.lang.functional.array"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.array"] = true; +dojo.provide("dojox.lang.functional.array"); + +dojo.require("dojox.lang.functional.lambda"); + +// This module adds high-level functions and related constructs: +// - array-processing functions similar to standard JS functions + +// Notes: +// - this module provides JS standard methods similar to high-level functions in dojo/_base/array.js: +// forEach, map, filter, every, some + +// Defined methods: +// - take any valid lambda argument as the functional argument +// - operate on dense arrays +// - take a string as the array argument +// - take an iterator objects as the array argument + +(function(){ + var d = dojo, df = dojox.lang.functional; + + d.mixin(df, { + // JS 1.6 standard array functions, which can take a lambda as a parameter. + // Consider using dojo._base.array functions, if you don't need the lambda support. + filter: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: creates a new array with all elements that pass the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var t = [], v; + if(d.isArray(a)){ + for(var i = 0, n = a.length; i < n; ++i){ + v = a[i]; + if(f.call(o, v, i, a)){ t.push(v); } + } + }else{ + for(var i = 0; a.hasNext();){ + v = a.next(); + if(f.call(o, v, i++)){ t.push(v); } + } + } + return t; // Array + }, + forEach: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: executes a provided function once per array element. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + if(d.isArray(a)){ + for(var i = 0, n = a.length; i < n; f.call(o, a[i], i, a), ++i); + }else{ + for(var i = 0; a.hasNext(); f.call(o, a.next(), i++)); + } + }, + map: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: creates a new array with the results of calling + // a provided function on every element in this array. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var t, n; + if(d.isArray(a)){ + t = new Array(n = a.length); + for(var i = 0; i < n; t[i] = f.call(o, a[i], i, a), ++i); + }else{ + t = []; + for(var i = 0; a.hasNext(); t.push(f.call(o, a.next(), i++))); + } + return t; // Array + }, + every: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: tests whether all elements in the array pass the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + if(d.isArray(a)){ + for(var i = 0, n = a.length; i < n; ++i){ + if(!f.call(o, a[i], i, a)){ + return false; // Boolean + } + } + }else{ + for(var i = 0; a.hasNext();){ + if(!f.call(o, a.next(), i++)){ + return false; // Boolean + } + } + } + return true; // Boolean + }, + some: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: tests whether some element in the array passes the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + if(d.isArray(a)){ + for(var i = 0, n = a.length; i < n; ++i){ + if(f.call(o, a[i], i, a)){ + return true; // Boolean + } + } + }else{ + for(var i = 0; a.hasNext();){ + if(f.call(o, a.next(), i++)){ + return true; // Boolean + } + } + } + return false; // Boolean + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/curry.js b/includes/js/dojox/lang/functional/curry.js new file mode 100644 index 0000000..7c795ec --- /dev/null +++ b/includes/js/dojox/lang/functional/curry.js @@ -0,0 +1,96 @@ +if(!dojo._hasResource["dojox.lang.functional.curry"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.curry"] = true; +dojo.provide("dojox.lang.functional.curry"); + +dojo.require("dojox.lang.functional.lambda"); + +// This module adds high-level functions and related constructs: +// - currying and partial functions +// - argument pre-processing: mixer and flip + +// Acknoledgements: +// - partial() is based on work by Oliver Steele +// (http://osteele.com/sources/javascript/functional/functional.js) +// which was published under MIT License + +// Defined methods: +// - take any valid lambda argument as the functional argument + +(function(){ + var df = dojox.lang.functional; + + var currying = function(/*Object*/ info){ + return function(){ // Function + if(arguments.length + info.args.length < info.arity){ + return currying({func: info.func, arity: info.arity, + args: Array.prototype.concat.apply(info.args, arguments)}); + } + return info.func.apply(this, Array.prototype.concat.apply(info.args, arguments)); + }; + }; + + dojo.mixin(df, { + // currying and partial functions + curry: function(/*Function|String|Array*/ f, /*Number?*/ arity){ + // summary: curries a function until the arity is satisfied, at + // which point it returns the calculated value. + f = df.lambda(f); + arity = typeof arity == "number" ? arity : f.length; + return currying({func: f, arity: arity, args: []}); // Function + }, + arg: {}, // marker for missing arguments + partial: function(/*Function|String|Array*/ f){ + // summary: creates a function where some arguments are bound, and + // some arguments (marked as dojox.lang.functional.arg) are will be + // accepted by the final function in the order they are encountered. + // description: This method is used to produce partially bound + // functions. If you want to change the order of arguments, use + // dojox.lang.functional.mixer() or dojox.lang.functional.flip(). + var a = arguments, args = new Array(a.length - 1), p = []; + f = df.lambda(f); + for(var i = 1; i < a.length; ++i){ + var t = a[i]; + args[i - 1] = t; + if(t == df.arg){ + p.push(i - 1); + } + } + return function(){ // Function + var t = Array.prototype.slice.call(args, 0); // clone the array + for(var i = 0; i < p.length; ++i){ + t[p[i]] = arguments[i]; + } + return f.apply(this, t); + }; + }, + // argument pre-processing + mixer: function(/*Function|String|Array*/ f, /*Array*/ mix){ + // summary: changes the order of arguments using an array of + // numbers mix --- i-th argument comes from mix[i]-th place + // of supplied arguments. + f = df.lambda(f); + return function(){ // Function + var t = new Array(mix.length); + for(var i = 0; i < mix.length; ++i){ + t[i] = arguments[mix[i]]; + } + return f.apply(this, t); + }; + }, + flip: function(/*Function|String|Array*/ f){ + // summary: changes the order of arguments by reversing their + // order. + f = df.lambda(f); + return function(){ // Function + // reverse arguments + var a = arguments, l = a.length - 1, t = new Array(l + 1), i; + for(i = 0; i <= l; ++i){ + t[l - i] = a[i]; + } + return f.apply(this, t); + }; + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/fold.js b/includes/js/dojox/lang/functional/fold.js new file mode 100644 index 0000000..90a9a3d --- /dev/null +++ b/includes/js/dojox/lang/functional/fold.js @@ -0,0 +1,87 @@ +if(!dojo._hasResource["dojox.lang.functional.fold"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.fold"] = true; +dojo.provide("dojox.lang.functional.fold"); + +dojo.require("dojox.lang.functional.lambda"); + +// This module adds high-level functions and related constructs: +// - "fold" family of functions + +// Notes: +// - missing high-level functions are provided with the compatible API: +// foldl, foldl1, foldr, foldr1 +// - missing JS standard functions are provided with the compatible API: +// reduce, reduceRight + +// Defined methods: +// - take any valid lambda argument as the functional argument +// - operate on dense arrays +// - take a string as the array argument +// - take an iterator objects as the array argument (only foldl, foldl1, and reduce) + +(function(){ + var d = dojo, df = dojox.lang.functional; + + d.mixin(df, { + // classic reduce-class functions + foldl: function(/*Array|String|Object*/ a, /*Function*/ f, /*Object*/ z, /*Object?*/ o){ + // summary: repeatedly applies a binary function to an array from left + // to right using a seed value as a starting point; returns the final + // value. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + if(d.isArray(a)){ + for(var i = 0, n = a.length; i < n; z = f.call(o, z, a[i], i, a), ++i); + }else{ + for(var i = 0; a.hasNext(); z = f.call(o, z, a.next(), i++)); + } + return z; // Object + }, + foldl1: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: repeatedly applies a binary function to an array from left + // to right; returns the final value. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var z; + if(d.isArray(a)){ + z = a[0]; + for(var i = 1, n = a.length; i < n; z = f.call(o, z, a[i], i, a), ++i); + }else if(a.hasNext()){ + z = a.next(); + for(var i = 1; a.hasNext(); z = f.call(o, z, a.next(), i++)); + } + return z; // Object + }, + foldr: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){ + // summary: repeatedly applies a binary function to an array from right + // to left using a seed value as a starting point; returns the final + // value. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + for(var i = a.length; i > 0; --i, z = f.call(o, z, a[i], i, a)); + return z; // Object + }, + foldr1: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: repeatedly applies a binary function to an array from right + // to left; returns the final value. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var n = a.length, z = a[n - 1]; + for(var i = n - 1; i > 0; --i, z = f.call(o, z, a[i], i, a)); + return z; // Object + }, + // JS 1.8 standard array functions, which can take a lambda as a parameter. + reduce: function(/*Array|String|Object*/ a, /*Function*/ f, /*Object?*/ z){ + // summary: apply a function simultaneously against two values of the array + // (from left-to-right) as to reduce it to a single value. + return arguments.length < 3 ? df.foldl1(a, f) : df.foldl(a, f, z); // Object + }, + reduceRight: function(/*Array|String*/ a, /*Function*/ f, /*Object?*/ z){ + // summary: apply a function simultaneously against two values of the array + // (from right-to-left) as to reduce it to a single value. + return arguments.length < 3 ? df.foldr1(a, f) : df.foldr(a, f, z); // Object + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/lambda.js b/includes/js/dojox/lang/functional/lambda.js new file mode 100644 index 0000000..60be9f3 --- /dev/null +++ b/includes/js/dojox/lang/functional/lambda.js @@ -0,0 +1,110 @@ +if(!dojo._hasResource["dojox.lang.functional.lambda"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.lambda"] = true; +dojo.provide("dojox.lang.functional.lambda"); + +// This module adds high-level functions and related constructs: +// - anonymous functions built from the string + +// Acknoledgements: +// - lambda() is based on work by Oliver Steele +// (http://osteele.com/sources/javascript/functional/functional.js) +// which was published under MIT License + +// Notes: +// - lambda() produces functions, which after the compilation step are +// as fast as regular JS functions (at least theoretically). + +// Lambda input values: +// - returns functions unchanged +// - converts strings to functions +// - converts arrays to a functional composition + +(function(){ + var df = dojox.lang.functional; + + // split() is augmented on IE6 to ensure the uniform behavior + var split = "ab".split(/a*/).length > 1 ? String.prototype.split : + function(sep){ + var r = this.split.call(this, sep), + m = sep.exec(this); + if(m && m.index == 0){ r.unshift(""); } + return r; + }; + + var lambda = function(/*String*/ s){ + var args = [], sects = split.call(s, /\s*->\s*/m); + if(sects.length > 1){ + while(sects.length){ + s = sects.pop(); + args = sects.pop().split(/\s*,\s*|\s+/m); + if(sects.length){ sects.push("(function(" + args + "){return (" + s + ")})"); } + } + }else if(s.match(/\b_\b/)){ + args = ["_"]; + }else{ + var l = s.match(/^\s*(?:[+*\/%&|\^\.=<>]|!=)/m), + r = s.match(/[+\-*\/%&|\^\.=<>!]\s*$/m); + if(l || r){ + if(l){ + args.push("$1"); + s = "$1" + s; + } + if(r){ + args.push("$2"); + s = s + "$2"; + } + }else{ + // the point of the long regex below is to exclude all well-known + // lower-case words from the list of potential arguments + var vars = s. + replace(/(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|this|true|false|null|undefined|typeof|instanceof|in|delete|new|void|arguments|decodeURI|decodeURIComponent|encodeURI|encodeURIComponent|escape|eval|isFinite|isNaN|parseFloat|parseInt|unescape|dojo|dijit|dojox|window|document|'(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"/g, ""). + match(/([a-z_$][a-z_$\d]*)/gi) || []; + var t = {}; + dojo.forEach(vars, function(v){ + if(!(v in t)){ + args.push(v); + t[v] = 1; + } + }); + } + } + return {args: args, body: "return (" + s + ");"}; // Object + }; + + var compose = function(/*Array*/ a){ + return a.length ? + function(){ + var i = a.length - 1, x = df.lambda(a[i]).apply(this, arguments); + for(--i; i >= 0; --i){ x = df.lambda(a[i]).call(this, x); } + return x; + } + : + // identity + function(x){ return x; }; + }; + + dojo.mixin(df, { + // lambda + buildLambda: function(/*String*/ s){ + // summary: builds a function from a snippet, returns a string, + // which represents the function. + // description: This method returns a textual representation of a function + // built from the snippet. It is meant to be evaled in the proper context, + // so local variables can be pulled from the environment. + s = lambda(s); + return "function(" + s.args.join(",") + "){" + s.body + "}"; // String + }, + lambda: function(/*Function|String|Array*/ s){ + // summary: builds a function from a snippet, or array (composing), returns + // a function object; functions are passed through unmodified. + // description: This method is used to normalize a functional representation + // (a text snippet, an array, or a function) to a function object. + if(typeof s == "function"){ return s; } + if(s instanceof Array){ return compose(s); } + s = lambda(s); + return new Function(s.args, s.body); // Function + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/listcomp.js b/includes/js/dojox/lang/functional/listcomp.js new file mode 100644 index 0000000..adb4883 --- /dev/null +++ b/includes/js/dojox/lang/functional/listcomp.js @@ -0,0 +1,54 @@ +if(!dojo._hasResource["dojox.lang.functional.listcomp"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.listcomp"] = true; +dojo.provide("dojox.lang.functional.listcomp"); + +// This module adds high-level functions and related constructs: +// - list comprehensions similar to JavaScript 1.7 + +// Notes: +// - listcomp() produces functions, which after the compilation step are +// as fast as regular JS functions (at least theoretically). + +(function(){ + var g_re = /\bfor\b|\bif\b/gm; + + var listcomp = function(/*String*/ s){ + var frag = s.split(g_re), act = s.match(g_re), + head = ["var r = [];"], tail = []; + for(var i = 0; i < act.length;){ + var a = act[i], f = frag[++i]; + if(a == "for" && !/^\s*\(\s*(;|var)/.test(f)){ + f = f.replace(/^\s*\(/, "(var "); + } + head.push(a, f, "{"); + tail.push("}"); + } + return head.join("") + "r.push(" + frag[0] + ");" + tail.join("") + "return r;"; // String + }; + + dojo.mixin(dojox.lang.functional, { + buildListcomp: function(/*String*/ s){ + // summary: builds a function from a text snippet, which represents a valid + // JS 1.7 list comprehension, returns a string, which represents the function. + // description: This method returns a textual representation of a function + // built from the list comprehension text snippet (conformant to JS 1.7). + // It is meant to be evaled in the proper context, so local variable can be + // pulled from the environment. + return "function(){" + listcomp(s) + "}"; // String + }, + compileListcomp: function(/*String*/ s){ + // summary: builds a function from a text snippet, which represents a valid + // JS 1.7 list comprehension, returns a function object. + // description: This method returns a function built from the list + // comprehension text snippet (conformant to JS 1.7). It is meant to be + // reused several times. + return new Function([], listcomp(s)); // Function + }, + listcomp: function(/*String*/ s){ + // summary: executes the list comprehension building an array. + return (new Function([], listcomp(s)))(); // Array + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/object.js b/includes/js/dojox/lang/functional/object.js new file mode 100644 index 0000000..52b6272 --- /dev/null +++ b/includes/js/dojox/lang/functional/object.js @@ -0,0 +1,48 @@ +if(!dojo._hasResource["dojox.lang.functional.object"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.object"] = true; +dojo.provide("dojox.lang.functional.object"); + +dojo.require("dojox.lang.functional.lambda"); + +// This module adds high-level functions and related constructs: +// - object/dictionary helpers + +// Defined methods: +// - take any valid lambda argument as the functional argument + +(function(){ + var d = dojo, df = dojox.lang.functional, empty = {}; + + d.mixin(df, { + // object helpers + forIn: function(/*Object*/ obj, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: iterates over all object members skipping members, which + // are present in the empty object (IE and/or 3rd-party libraries). + o = o || d.global; f = df.lambda(f); + for(var i in obj){ + if(i in empty){ continue; } + f.call(o, obj[i], i, obj); + } + }, + keys: function(/*Object*/ obj){ + // summary: returns an array of all keys in the object + var t = []; + for(var i in obj){ + if(i in empty){ continue; } + t.push(i); + } + return t; // Array + }, + values: function(/*Object*/ obj){ + // summary: returns an array of all values in the object + var t = []; + for(var i in obj){ + if(i in empty){ continue; } + t.push(obj[i]); + } + return t; // Array + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/reversed.js b/includes/js/dojox/lang/functional/reversed.js new file mode 100644 index 0000000..4375948 --- /dev/null +++ b/includes/js/dojox/lang/functional/reversed.js @@ -0,0 +1,79 @@ +if(!dojo._hasResource["dojox.lang.functional.reversed"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.reversed"] = true; +dojo.provide("dojox.lang.functional.reversed"); + +dojo.require("dojox.lang.functional.lambda"); + +// This module adds high-level functions and related constructs: +// - reversed versions of array-processing functions similar to standard JS functions + +// Notes: +// - this module provides reversed versions of standard array-processing functions: +// forEachRev, mapRev, filterRev + +// Defined methods: +// - take any valid lambda argument as the functional argument +// - operate on dense arrays +// - take a string as the array argument + +(function(){ + var d = dojo, df = dojox.lang.functional; + + d.mixin(df, { + // JS 1.6 standard array functions, which can take a lambda as a parameter. + // Consider using dojo._base.array functions, if you don't need the lambda support. + filterRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: creates a new array with all elements that pass the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var t = [], v; + for(var i = a.length - 1; i >= 0; --i){ + v = a[i]; + if(f.call(o, v, i, a)){ t.push(v); } + } + return t; // Array + }, + forEachRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: executes a provided function once per array element. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + for(var i = a.length - 1; i >= 0; f.call(o, a[i], i, a), --i); + }, + mapRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: creates a new array with the results of calling + // a provided function on every element in this array. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var n = a.length, t = new Array(n); + for(var i = n - 1, j = 0; i >= 0; t[j++] = f.call(o, a[i], i, a), --i); + return t; // Array + }, + everyRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: tests whether all elements in the array pass the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + for(var i = a.length - 1; i >= 0; --i){ + if(!f.call(o, a[i], i, a)){ + return false; // Boolean + } + } + return true; // Boolean + }, + someRev: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: tests whether some element in the array passes the test + // implemented by the provided function. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + for(var i = a.length - 1; i >= 0; --i){ + if(f.call(o, a[i], i, a)){ + return true; // Boolean + } + } + return false; // Boolean + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/scan.js b/includes/js/dojox/lang/functional/scan.js new file mode 100644 index 0000000..65a3ef2 --- /dev/null +++ b/includes/js/dojox/lang/functional/scan.js @@ -0,0 +1,84 @@ +if(!dojo._hasResource["dojox.lang.functional.scan"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.scan"] = true; +dojo.provide("dojox.lang.functional.scan"); + +dojo.require("dojox.lang.functional.lambda"); + +// This module adds high-level functions and related constructs: +// - "scan" family of functions + +// Notes: +// - missing high-level functions are provided with the compatible API: +// scanl, scanl1, scanr, scanr1 + +// Defined methods: +// - take any valid lambda argument as the functional argument +// - operate on dense arrays +// - take a string as the array argument +// - take an iterator objects as the array argument (only scanl, and scanl1) + +(function(){ + var d = dojo, df = dojox.lang.functional; + + d.mixin(df, { + // classic reduce-class functions + scanl: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){ + // summary: repeatedly applies a binary function to an array from left + // to right using a seed value as a starting point; returns an array + // of values produced by foldl() at that point. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var t, n; + if(d.isArray(a)){ + t = new Array((n = a.length) + 1); + t[0] = z; + for(var i = 0; i < n; z = f.call(o, z, a[i], i, a), t[++i] = z); + }else{ + t = [z]; + for(var i = 0; a.hasNext(); t.push(z = f.call(o, z, a.next(), i++))); + } + return t; // Array + }, + scanl1: function(/*Array|String|Object*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: repeatedly applies a binary function to an array from left + // to right; returns an array of values produced by foldl1() at that + // point. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var t, n, z; + if(d.isArray(a)){ + t = new Array(n = a.length); + t[0] = z = a[0]; + for(var i = 1; i < n; t[i] = z = f.call(o, z, a[i], i, a), ++i); + }else if(a.hasNext()){ + t = [z = a.next()]; + for(var i = 1; a.hasNext(); t.push(z = f.call(o, z, a.next(), i++))); + } + return t; // Array + }, + scanr: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){ + // summary: repeatedly applies a binary function to an array from right + // to left using a seed value as a starting point; returns an array + // of values produced by foldr() at that point. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var n = a.length, t = new Array(n + 1); + t[n] = z; + for(var i = n; i > 0; --i, z = f.call(o, z, a[i], i, a), t[i] = z); + return t; // Array + }, + scanr1: function(/*Array|String*/ a, /*Function|String|Array*/ f, /*Object?*/ o){ + // summary: repeatedly applies a binary function to an array from right + // to left; returns an array of values produced by foldr1() at that + // point. + if(typeof a == "string"){ a = a.split(""); } + o = o || d.global; f = df.lambda(f); + var n = a.length, t = new Array(n), z = a[n - 1]; + t[n - 1] = z; + for(var i = n - 1; i > 0; --i, z = f.call(o, z, a[i], i, a), t[i] = z); + return t; // Array + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/sequence.js b/includes/js/dojox/lang/functional/sequence.js new file mode 100644 index 0000000..c5aaa1c --- /dev/null +++ b/includes/js/dojox/lang/functional/sequence.js @@ -0,0 +1,38 @@ +if(!dojo._hasResource["dojox.lang.functional.sequence"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.sequence"] = true; +dojo.provide("dojox.lang.functional.sequence"); + +dojo.require("dojox.lang.functional.lambda"); + +// This module adds high-level functions and related constructs: +// - sequence generators + +// Defined methods: +// - take any valid lambda argument as the functional argument + +(function(){ + var d = dojo, df = dojox.lang.functional; + + d.mixin(df, { + // sequence generators + repeat: function(/*Number*/ n, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){ + // summary: builds an array by repeatedly applying a unary function N times + // with a seed value Z. + o = o || d.global; f = df.lambda(f); + var t = new Array(n); + t[0] = z; + for(var i = 1; i < n; t[i] = z = f.call(o, z), ++i); + return t; // Array + }, + until: function(/*Function|String|Array*/ pr, /*Function|String|Array*/ f, /*Object*/ z, /*Object?*/ o){ + // summary: builds an array by repeatedly applying a unary function with + // a seed value Z until the predicate is satisfied. + o = o || d.global; f = df.lambda(f); pr = df.lambda(pr); + var t = []; + for(; !pr.call(o, z); t.push(z), z = f.call(o, z)); + return t; // Array + } + }); +})(); + +} diff --git a/includes/js/dojox/lang/functional/zip.js b/includes/js/dojox/lang/functional/zip.js new file mode 100644 index 0000000..523f400 --- /dev/null +++ b/includes/js/dojox/lang/functional/zip.js @@ -0,0 +1,45 @@ +if(!dojo._hasResource["dojox.lang.functional.zip"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. +dojo._hasResource["dojox.lang.functional.zip"] = true; +dojo.provide("dojox.lang.functional.zip"); + +// This module adds high-level functions and related constructs: +// - zip combiners + +// Defined methods: +// - operate on dense arrays + +(function(){ + var df = dojox.lang.functional; + + dojo.mixin(df, { + // combiners + zip: function(){ + // summary: returns an array of arrays, where the i-th array + // contains the i-th element from each of the argument arrays. + // description: This is the venerable zip combiner (for example, + // see Python documentation for general details). The returned + // array is truncated to match the length of the shortest input + // array. + var n = arguments[0].length, m = arguments.length, i; + for(i = 1; i < m; n = Math.min(n, arguments[i++].length)); + var t = new Array(n), j; + for(i = 0; i < n; ++i){ + var p = new Array(m); + for(j = 0; j < m; p[j] = arguments[j][i], ++j); + t[i] = p; + } + return t; // Array + }, + unzip: function(/*Array*/ a){ + // summary: similar to dojox.lang.functional.zip(), but takes + // a single array of arrays as the input. + // description: This function is similar to dojox.lang.functional.zip() + // and can be used to unzip objects packed by + // dojox.lang.functional.zip(). It is here mostly to provide + // a short-cut for the different method signature. + return df.zip.apply(null, a); // Array + } + }); +})(); + +} -- cgit v1.2.3