Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
72 lines (58 loc) · 2.06 KB

File metadata and controls

72 lines (58 loc) · 2.06 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: Partial application
Description: the process of fixing a number of arguments to a function, producing another function of smaller arity
*/
var partialAny = (function(aps){
// This function will be returned as a result of the immediately
// invoked function expression and assigned to the `partialAny` var.
function func(fn){
var argsOrig = aps.call(arguments, 1);
return function() {
var args = [],
argsPartial = aps.call(arguments),
i = 0;
// Iterate over all the originally-spedicified arguments. If that
// argument was the `partialAny._` placeholder, use the next just
// passed-in argument, otherwise use the originally-specified
// argument.
for ( ; i < argsOrig.length; i++ ) {
args[i] = argsOrig[i] === func._
? argsPartial.shift()
: argsOrig[i];
}
// If any just-passed-in arguments remain, add them to the end.
return fn.apply( this, args.concat( argsPartial ));
};
}
// This is used as the placeholder argument.
func._ = {};
return func;
})(Array.prototype.slice);
// Slightly more legitimate example
function hex( r, g, b ) {
return '#' + r + g + b;
}
var redMax = partialAny( hex, 'ff', partialAny._, partialAny._);
console.log(redMax('11','22')); // "#ff1122"
// Because `_` is easier on the eyes than `partialAny._`, let's use
// that instead. This is, of course, entirely optional, and the name
// could just as well be `foo` or `PLACEHOLDER` instead of `_`.
var __ = partialAny._;
var greenMax = partialAny( hex, __, 'ff' );
console.log(greenMax( '33', '44' ));
var blueMax = partialAny( hex, __, __, 'ff' );
console.log(blueMax( '55', '66' ));
var magentaMax = partialAny( hex, 'ff', __, 'ff' );
console.log(magentaMax( '77' ));
// reference
// http://msdn.microsoft.com/en-us/magazine/gg575560.aspx
</script>
</body>
</html>
Morty Proxy This is a proxified and sanitized view of the page, visit original site.