Partial Application
In Javascript
(and why I like it)
Dude, who are you?
A’braham (Ah-brah-haum) Barakhyahu (Ba-rock-yahu)
Web developer (js, C# rest services)
jsGrunt
Vim kidd...
First Class Functions
Pass functions as parameters into
other functions
function shalom() {
return ‘Shalom’;
}
function sayToConsole(someGreetin...
Functions can return functions
function sayToConsole(greeting) {
return function(message) {
console.log( greeting + ‘ ‘ + ...
Contrived Examples Ahead!
Instead of this
function add(x,y) {
return x + y;
}
for(var i = 0; i < 6 ; i++) {
console.log(add(5,i));
}
You could do this
function add(x) {
return function(y) {
return x + y;
}
}
var add5To = add(5);
for(var i = 0; i < 6 ; i++...
Or use underscore/lodash/
function add(x,y) {
return x + y;
}
var add5To = _.partial(add,5);
for(var i = 0; i < 6 ; i++) {...
little closer to a real-world example
sample
Refernces for more
Functional Javascript by Michael Fogus -

http://store.

kobobooks.com/en-us/books/Functional-JavaScrip...
Thank you for you patience!
A’braham Barakhyahu
@blessYAHU
barakhyahu@gmail.com
Upcoming SlideShare
Loading in …5
×

Partial Application in Javascript (and why I like it)

515 views

Published on

Slidedeck from RVA.js lightning talk on Partial Application.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
515
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Partial Application in Javascript (and why I like it)

  1. 1. Partial Application In Javascript (and why I like it)
  2. 2. Dude, who are you? A’braham (Ah-brah-haum) Barakhyahu (Ba-rock-yahu) Web developer (js, C# rest services) jsGrunt Vim kiddie FP grasshopper
  3. 3. First Class Functions
  4. 4. Pass functions as parameters into other functions function shalom() { return ‘Shalom’; } function sayToConsole(someGreeting) { console.log(someGreeting()); } sayToConsole(shalom);
  5. 5. Functions can return functions function sayToConsole(greeting) { return function(message) { console.log( greeting + ‘ ‘ + message); } } var greetBobAndSay = sayToConsole(‘Hi Bob!’); greetBobAndSay(‘I have fantastic pants’);
  6. 6. Contrived Examples Ahead!
  7. 7. Instead of this function add(x,y) { return x + y; } for(var i = 0; i < 6 ; i++) { console.log(add(5,i)); }
  8. 8. You could do this function add(x) { return function(y) { return x + y; } } var add5To = add(5); for(var i = 0; i < 6 ; i++) { console.log(add5To(i));
  9. 9. Or use underscore/lodash/ function add(x,y) { return x + y; } var add5To = _.partial(add,5); for(var i = 0; i < 6 ; i++) { console.log(add5To(i)); } [other functional library here]
  10. 10. little closer to a real-world example sample
  11. 11. Refernces for more Functional Javascript by Michael Fogus - http://store. kobobooks.com/en-us/books/Functional-JavaScript/ Lodash’s partialRight Underscore’s partial Currying vs. Partial Application - http://lodash.com/docs#partialRight http://underscorejs.org/#partial http://www.2ality.com/2011/09/currying- vs-part-eval.html Partial Application in Javascript in-javascript/ http://ejohn.org/blog/partial-functions-
  12. 12. Thank you for you patience! A’braham Barakhyahu @blessYAHU barakhyahu@gmail.com

×