Debian Codegraph @

dojox/lang/functional/curry.js

raw symbols SCIP
1 define(["dojo/_base/lang", "./lambda"],
2 function(lang, df){
3
4 // This module adds high-level functions and related constructs:
5 // - currying and partial functions
6 // - argument pre-processing: mixer and flip
7
8 // Acknowledgements:
9 // - partial() is based on work by Oliver Steele
10 // (http://osteele.com/sources/javascript/functional/functional.js)
11 // which was published under MIT License
12
13 // Defined methods:
14 // - take any valid lambda argument as the functional argument
15 var ap = Array.prototype;
16 var currying = function(/*Object*/ info){
17 return function(){ // Function
18 var args = info.args.concat(ap.slice.call(arguments, 0));
19 if(arguments.length + info.args.length < info.arity){
20 return currying({func: info.func, arity: info.arity, args: args});
21 }
22 return info.func.apply(this, args);
23 };
24 };
25
26 lang.mixin(df, {
27 // currying and partial functions
28 curry: function(/*Function|String|Array*/ f, /*Number?*/ arity){
29 // summary:
30 // curries a function until the arity is satisfied, at
31 // which point it returns the calculated value.
32 f = df.lambda(f);
33 arity = typeof arity == "number" ? arity : f.length;
34 return currying({func: f, arity: arity, args: []}); // Function
35 },
36 arg: {}, // marker for missing arguments
37 partial: function(/*Function|String|Array*/ f){
38 // summary:
39 // creates a function where some arguments are bound, and
40 // some arguments (marked as dojox.lang.functional.arg) are will be
41 // accepted by the final function in the order they are encountered.
42 // description:
43 // This method is used to produce partially bound
44 // functions. If you want to change the order of arguments, use
45 // dojox.lang.functional.mixer() or dojox.lang.functional.flip().
46 var a = arguments, l = a.length, args = new Array(l - 1), p = [], i = 1, t;
47 f = df.lambda(f);
48 for(; i < l; ++i){
49 t = a[i];
50 args[i - 1] = t;
51 if(t === df.arg){
52 p.push(i - 1);
53 }
54 }
55 return function(){ // Function
56 var t = ap.slice.call(args, 0), // clone the array
57 i = 0, l = p.length;
58 for(; i < l; ++i){
59 t[p[i]] = arguments[i];
60 }
61 return f.apply(this, t);
62 };
63 },
64 // argument pre-processing
65 mixer: function(/*Function|String|Array*/ f, /*Array*/ mix){
66 // summary:
67 // changes the order of arguments using an array of
68 // numbers mix --- i-th argument comes from mix[i]-th place
69 // of supplied arguments.
70 f = df.lambda(f);
71 return function(){ // Function
72 var t = new Array(mix.length), i = 0, l = mix.length;
73 for(; i < l; ++i){
74 t[i] = arguments[mix[i]];
75 }
76 return f.apply(this, t);
77 };
78 },
79 flip: function(/*Function|String|Array*/ f){
80 // summary:
81 // changes the order of arguments by reversing their
82 // order.
83 f = df.lambda(f);
84 return function(){ // Function
85 // reverse arguments
86 var a = arguments, l = a.length - 1, t = new Array(l + 1), i = 0;
87 for(; i <= l; ++i){
88 t[l - i] = a[i];
89 }
90 return f.apply(this, t);
91 };
92 }
93 });
94 });