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

Sign up
Appearance settings

Commit 1d55fc8

Browse files
currying
1 parent eb183b1 commit 1d55fc8

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

‎04-functions.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,35 @@ hello.apply(null, ['hey!']);
167167
* second argument is an array of arguments
168168
* in non-strict mode if first argument is null then it'll point at the global object
169169
* ```Function.prototype.call``` is just syntactic sugar over apply
170-
* call expects parameters as normal instead of an array
170+
* call expects parameters as normal parameter list instead of an array
171+
172+
## partial application
173+
174+
* call a function with less than all of it's arguments
175+
* and return a function expecting the rest of the arguments
176+
* this transformation of function is called currying or schonfinkelizing
177+
* use when find yourself calling a function with same arguments
178+
179+
```js
180+
function curriedAdd(x,y) {
181+
if(y === undefined){
182+
return function(y){
183+
return x+y;
184+
}
185+
}
186+
return x + y;
187+
}
188+
189+
//or with more general curry function
190+
function curry(fn){
191+
var slice = Array.prototype.slice; // needed because arguments is not a real Array?
192+
var storedArgs = slice.call(arguments, 1);
193+
return function(){
194+
var newArgs = slice.call(arguments);
195+
var args = storedArgs.concat(newArgs);
196+
fn.apply(null, args);
197+
};
198+
}
199+
200+
curry(add, 6)(7);
201+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /