SlideShare a Scribd company logo
Talk Functional Javascript!
//+ CV.js :: September 18th, 2013
Kevin Welcher
Shout Out
Shout Out
Something about Camp?
10k Feet
• [Underscore.js / Lodash.js]
• Functional Concepts
• Composition
• Currying
• [Demo]
Underscore.js
Lodash.js
Highlights
• reduce
• map
• pluck
• filter
• find
reduce
• The grandaddy, most iterators can be made
out of reduce
• “Reduce a list to a new object”
map
• Bread and butter iterator
• Used to transform a list of elements from
one type to another
• Whatever is returned within the iterator is
the new object
pluck
• Iterates over a list of objects and returns a
new list made of the specified property
from each object
• A common use for map
• IE: plucking the name from an array of
people objects
filter
• Only allows values that pass a predicate
into the new list
• As with all these methods, it returns copies
of the data
find
• Accepts a predicate and returns the first
item that passes the predicate
Much, much more...
• Many predicates
(isArray, isNull, ...)
• keys / values
• groupBy
• result
• array methods
• union / intersection
• chain / compose
• extend / defaults
• simple templates
• pick / omit
FP vs OO
• The language of the... language
• How complexity is hidden
• How data is stored
Functional Concepts
Function purity
• Deterministic
• Does not depend on external state
• Does not depend on IO
• Does not cause side effects
Function purity
var View = Backbone.View.extend({
initialize: function() {
this.generateList();
},
generateList: function() {
this.list = new Backbone.Collection([
{name: 'sally'},
{name: 'joe'}
]);
}
});
Function purity
var View = Backbone.View.extend({
initialize: function() {
this.list = this.generateList();
},
generateList: function() {
return new Backbone.Collection([
{name: 'sally'},
{name: 'joe'}
]);
}
});
Functions as data
• First class citizens
• Identified by their returns
• Modifiable
Functions as building blocks
Functions which consume the return value of the
function that follows.
Composition:
Functions as building blocks
a() b() c()
a(b(c(x))) === compose(a, b, c)(x)
Composition
Functions as building blocks
function add1 (x) {
return x + 1;
}
var add3 = compose(add1, add1, add1);
add3(0);
//> 3
Functions as building blocks
function add1 (x) {
return x + 1;
}
var add3 = compose(add1, add1, add1);
add3(0);
//> 3
0
Functions as building blocks
function add1 (x) {
return x + 1;
}
var add3 = compose(add1, add1, add1);
add3(0);
//> 3
01
Functions as building blocks
function add1 (x) {
return x + 1;
}
var add3 = compose(add1, add1, add1);
add3(0);
//> 3
012
Functions as building blocks
function add1 (x) {
return x + 1;
}
var add3 = compose(add1, add1, add1);
add3(0);
//> 3
0123
Functions as building blocks
function not (x) {
return !x;
}
var boolify = compose(not, not);
not(not({}));
boolify({});
//> true
Functions as building blocks
function addTitle (x) {
return ‘Mr. ‘ + x;
}
function addSalutation (x) {
return ‘G’day, ‘ + x;
}
var meetAndGreet = compose(addSalutation, addTitle);
addSalutation(addTitle('Baggins'));
meetAndGreet('Baggins');
//> G’day, Mr. Baggins
Functions as building blocks
function add (x, y) {
return x + y;
}
var add1 = compose(???);
//> profit
Currying
“Taking a function that takes multiple arguments and
transforming it to a chain of functions that accept a single
argument.” - Wikipedia
Notation
//+ <method name> :: type -> type -> type
Notation
//+ <method name> :: type -> type -> type
A function that take takes an argument and returns a
value of type
Notation
//+ <method name> :: type -> type -> type
A function that take takes an argument and returns a
value of type
integer -> integer
A function that takes in a single
argument of type integer
The return type of integer. Since
nothing follows, it is the final return
Notation
//+ <method name> :: type -> type -> type
A function that take takes an argument and returns a
value of type
Notation
//+ <method name> :: type -> type -> type
The ultimate return value of a function
//+ not :: a -> boolean
function not (x) {
return !x;
}
not(true) //> false
//+ not :: a -> boolean
function not (x) {
return !x;
}
not(true) //> false
The a (or any other lower case letter) means we
don’t care about the type.
//+ add :: a, a -> a
function add(x, y) {
return x + y;
}
add(1, 2); //> 3
//+ add :: a, a -> a
function add(x, y) {
return x + y;
}
add(1, 2); //> 3
I am abusing notation :(
“Taking a function that takes multiple arguments and
transforming it to a chain of functions that accept a single
argument.” - Wikipedia
//+ add :: a, a -> a
//+ add :: a -> a -> a
//+ add :: a -> a -> a
function add(x) {
return function (y) {
return x + y;
};
}
add(1)(2); //> 3
ed
//+ add :: a -> a -> a
function add(x) {
return function (y) {
return x + y;
};
}
add(1)(2); //> 3
ed
//+ add :: a -> a -> a
//+ add :: 1 -> a -> a
//+ add1 :: integer -> integer
var add1 = add(1);
add1(2);
//> 3
//+ add :: a -> a -> a
//+ add :: ‘Mr. ‘ -> a -> a
//+ addTitle :: string -> string
var addTitle = add('Mr. ');
addTitle('Baggins');
//> Mr. Baggins
Problem
That _ is ugly
//+ add :: a -> a -> a
function add(x) {
return function (y) {
return x + y;
};
}
add(1)(2); //> 3
autoCurry
A custom function that automatically curries for you.
For each parameter of a function, it returns a new
function.
That _ is ugly
//+ add :: a -> a -> a
var add = function(x, y) {
return x + y;
}.autoCurry();
add(1, 2); //> 3
add(1)(2); //> 3 Awww yissss
Cool Story Bru...
Let Me ConvinceYou
With...
More Examples!
reduce
//+ reduce :: fn -> a -> array -> a
var reduce = function (fn, start, list) {
// ...
}.autoCurry();
reduce
//+ reduce :: fn -> a -> array -> a
var reduce = function (fn, start, list) {
// ...
}.autoCurry();
function (cnrt, val, index, list) {
// ...
}
Sum all the numbers in
a list?
var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (crnt, val, index, list) {
return crnt + val;
}, 0, items);
//> 6
var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (crnt, val, index, list) {
return crnt + val;
}, 0, items);
//> 6
We don’t use these two
arguments so we can omit them
var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (crnt, val) {
return crnt + val;
}, 0, items);
//> 6
var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (crnt, val) {
return crnt + val;
}, 0, items);
//> 6
This function looks familiar...
//+ add :: a -> a -> a
var add = function(x, y) {
return x + y;
}.autoCurry();
var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (x, y) {
return x + y;
}, 0, items);
//> 6
var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(function (x, y) {
return x + y;
}, 0, items);
//> 6
Instead of inlining the function, just pass
the function’s pointer
var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(add, 0, items);
//> 6
var items = [1, 2, 3];
//+ reduce :: fn -> a -> array -> a
reduce(add, 0, items);
//> 6
Cool! But isn’t reduce curried?
Can we make this into a generic function?
var items = [1, 2, 3];
//+ reduce :: add -> 0 -> array -> a
//+ sum :: array -> integer
var sum = reduce(add, 0);
sum(items);
//> 6
Demo
https://gist.github.com/kaw2k/6312261
Lessons Learned?
Keep your methods short and
to the point
It is easier to build things if your building blocks are small
Stick your data as the last
parameter
reduce = function (fn, starting value, data)
function (general, ..., specific)
Optional parameters are hard
Make a general function and curry it to take optional stuff
Functional purity is pretty cool
• Easier to maintain
• Easier to reason
• Easier to test
• Easier to transport
Obligatory Marketing
(We are hiring)

More Related Content

PPTX
ES6 in Real Life
PDF
EcmaScript 6 - The future is here
PDF
Introduction to cron queue
PPTX
Curry functions in Javascript
PDF
Emerging Languages: A Tour of the Horizon
PDF
The Ring programming language version 1.5.4 book - Part 23 of 185
PDF
GPars For Beginners
PDF
Mozilla とブラウザゲーム
ES6 in Real Life
EcmaScript 6 - The future is here
Introduction to cron queue
Curry functions in Javascript
Emerging Languages: A Tour of the Horizon
The Ring programming language version 1.5.4 book - Part 23 of 185
GPars For Beginners
Mozilla とブラウザゲーム

What's hot (17)

PDF
ECMAScript 6
PDF
An Intro To ES6
PDF
NS2: AWK and GNUplot - PArt III
PDF
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
PDF
Swift Sequences & Collections
PDF
_Function Builders in Swift #love_swift
PDF
PDF
Functional programming using underscorejs
PDF
미려한 UI/UX를 위한 여정
PDF
Javascript essential-pattern
PDF
The Ring programming language version 1.10 book - Part 43 of 212
PDF
The Ring programming language version 1.3 book - Part 14 of 88
ODP
EcmaScript 6
PDF
ES6 - Next Generation Javascript
PPTX
Oop presentation
PDF
Oop assignment 02
PDF
Tracing and awk in ns2
ECMAScript 6
An Intro To ES6
NS2: AWK and GNUplot - PArt III
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
Swift Sequences & Collections
_Function Builders in Swift #love_swift
Functional programming using underscorejs
미려한 UI/UX를 위한 여정
Javascript essential-pattern
The Ring programming language version 1.10 book - Part 43 of 212
The Ring programming language version 1.3 book - Part 14 of 88
EcmaScript 6
ES6 - Next Generation Javascript
Oop presentation
Oop assignment 02
Tracing and awk in ns2
Ad

Similar to Functional Javascript, CVjs (20)

PPT
25-functions.ppt
PDF
TI1220 Lecture 6: First-class Functions
PDF
Why Haskell Matters
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
PPTX
Thinking Functionally with JavaScript
PPTX
Intro to Javascript
PDF
Functional programming basics
PDF
ES6: The Awesome Parts
PDF
Functional Programming for OO Programmers (part 2)
PDF
Javascript
PDF
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
PDF
TypeScript Introduction
PDF
Bindings: the zen of montage
PPTX
ES6 Overview
KEY
Object-Oriented JavaScript
KEY
Object-Oriented Javascript
PPTX
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
PPTX
Advanced JavaScript
ODP
Functors, applicatives, monads
PDF
Cycle.js - A functional reactive UI framework
25-functions.ppt
TI1220 Lecture 6: First-class Functions
Why Haskell Matters
Столпы функционального программирования для адептов ООП, Николай Мозговой
Thinking Functionally with JavaScript
Intro to Javascript
Functional programming basics
ES6: The Awesome Parts
Functional Programming for OO Programmers (part 2)
Javascript
HelsinkiJS meet-up. Dmitry Soshnikov - ECMAScript 6
TypeScript Introduction
Bindings: the zen of montage
ES6 Overview
Object-Oriented JavaScript
Object-Oriented Javascript
Object Oriented Programming using C++: Ch08 Operator Overloading.pptx
Advanced JavaScript
Functors, applicatives, monads
Cycle.js - A functional reactive UI framework
Ad

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation theory and applications.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Electronic commerce courselecture one. Pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Machine learning based COVID-19 study performance prediction
PDF
Unlocking AI with Model Context Protocol (MCP)
PPT
Teaching material agriculture food technology
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Programs and apps: productivity, graphics, security and other tools
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation_ Review paper, used for researhc scholars
A comparative analysis of optical character recognition models for extracting...
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Encapsulation theory and applications.pdf
The AUB Centre for AI in Media Proposal.docx
Assigned Numbers - 2025 - Bluetooth® Document
Electronic commerce courselecture one. Pdf
20250228 LYD VKU AI Blended-Learning.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Machine learning based COVID-19 study performance prediction
Unlocking AI with Model Context Protocol (MCP)
Teaching material agriculture food technology

Functional Javascript, CVjs

  • 1. Talk Functional Javascript! //+ CV.js :: September 18th, 2013 Kevin Welcher
  • 4. 10k Feet • [Underscore.js / Lodash.js] • Functional Concepts • Composition • Currying • [Demo]
  • 6. Highlights • reduce • map • pluck • filter • find
  • 7. reduce • The grandaddy, most iterators can be made out of reduce • “Reduce a list to a new object”
  • 8. map • Bread and butter iterator • Used to transform a list of elements from one type to another • Whatever is returned within the iterator is the new object
  • 9. pluck • Iterates over a list of objects and returns a new list made of the specified property from each object • A common use for map • IE: plucking the name from an array of people objects
  • 10. filter • Only allows values that pass a predicate into the new list • As with all these methods, it returns copies of the data
  • 11. find • Accepts a predicate and returns the first item that passes the predicate
  • 12. Much, much more... • Many predicates (isArray, isNull, ...) • keys / values • groupBy • result • array methods • union / intersection • chain / compose • extend / defaults • simple templates • pick / omit
  • 13. FP vs OO • The language of the... language • How complexity is hidden • How data is stored
  • 15. Function purity • Deterministic • Does not depend on external state • Does not depend on IO • Does not cause side effects
  • 16. Function purity var View = Backbone.View.extend({ initialize: function() { this.generateList(); }, generateList: function() { this.list = new Backbone.Collection([ {name: 'sally'}, {name: 'joe'} ]); } });
  • 17. Function purity var View = Backbone.View.extend({ initialize: function() { this.list = this.generateList(); }, generateList: function() { return new Backbone.Collection([ {name: 'sally'}, {name: 'joe'} ]); } });
  • 18. Functions as data • First class citizens • Identified by their returns • Modifiable
  • 19. Functions as building blocks Functions which consume the return value of the function that follows. Composition:
  • 20. Functions as building blocks a() b() c() a(b(c(x))) === compose(a, b, c)(x) Composition
  • 21. Functions as building blocks function add1 (x) { return x + 1; } var add3 = compose(add1, add1, add1); add3(0); //> 3
  • 22. Functions as building blocks function add1 (x) { return x + 1; } var add3 = compose(add1, add1, add1); add3(0); //> 3 0
  • 23. Functions as building blocks function add1 (x) { return x + 1; } var add3 = compose(add1, add1, add1); add3(0); //> 3 01
  • 24. Functions as building blocks function add1 (x) { return x + 1; } var add3 = compose(add1, add1, add1); add3(0); //> 3 012
  • 25. Functions as building blocks function add1 (x) { return x + 1; } var add3 = compose(add1, add1, add1); add3(0); //> 3 0123
  • 26. Functions as building blocks function not (x) { return !x; } var boolify = compose(not, not); not(not({})); boolify({}); //> true
  • 27. Functions as building blocks function addTitle (x) { return ‘Mr. ‘ + x; } function addSalutation (x) { return ‘G’day, ‘ + x; } var meetAndGreet = compose(addSalutation, addTitle); addSalutation(addTitle('Baggins')); meetAndGreet('Baggins'); //> G’day, Mr. Baggins
  • 28. Functions as building blocks function add (x, y) { return x + y; } var add1 = compose(???); //> profit
  • 29. Currying “Taking a function that takes multiple arguments and transforming it to a chain of functions that accept a single argument.” - Wikipedia
  • 30. Notation //+ <method name> :: type -> type -> type
  • 31. Notation //+ <method name> :: type -> type -> type A function that take takes an argument and returns a value of type
  • 32. Notation //+ <method name> :: type -> type -> type A function that take takes an argument and returns a value of type integer -> integer A function that takes in a single argument of type integer The return type of integer. Since nothing follows, it is the final return
  • 33. Notation //+ <method name> :: type -> type -> type A function that take takes an argument and returns a value of type
  • 34. Notation //+ <method name> :: type -> type -> type The ultimate return value of a function
  • 35. //+ not :: a -> boolean function not (x) { return !x; } not(true) //> false
  • 36. //+ not :: a -> boolean function not (x) { return !x; } not(true) //> false The a (or any other lower case letter) means we don’t care about the type.
  • 37. //+ add :: a, a -> a function add(x, y) { return x + y; } add(1, 2); //> 3
  • 38. //+ add :: a, a -> a function add(x, y) { return x + y; } add(1, 2); //> 3 I am abusing notation :(
  • 39. “Taking a function that takes multiple arguments and transforming it to a chain of functions that accept a single argument.” - Wikipedia
  • 40. //+ add :: a, a -> a //+ add :: a -> a -> a
  • 41. //+ add :: a -> a -> a function add(x) { return function (y) { return x + y; }; } add(1)(2); //> 3 ed
  • 42. //+ add :: a -> a -> a function add(x) { return function (y) { return x + y; }; } add(1)(2); //> 3 ed
  • 43. //+ add :: a -> a -> a //+ add :: 1 -> a -> a //+ add1 :: integer -> integer var add1 = add(1); add1(2); //> 3
  • 44. //+ add :: a -> a -> a //+ add :: ‘Mr. ‘ -> a -> a //+ addTitle :: string -> string var addTitle = add('Mr. '); addTitle('Baggins'); //> Mr. Baggins
  • 46. That _ is ugly //+ add :: a -> a -> a function add(x) { return function (y) { return x + y; }; } add(1)(2); //> 3
  • 47. autoCurry A custom function that automatically curries for you. For each parameter of a function, it returns a new function.
  • 48. That _ is ugly //+ add :: a -> a -> a var add = function(x, y) { return x + y; }.autoCurry(); add(1, 2); //> 3 add(1)(2); //> 3 Awww yissss
  • 52. reduce //+ reduce :: fn -> a -> array -> a var reduce = function (fn, start, list) { // ... }.autoCurry();
  • 53. reduce //+ reduce :: fn -> a -> array -> a var reduce = function (fn, start, list) { // ... }.autoCurry(); function (cnrt, val, index, list) { // ... }
  • 54. Sum all the numbers in a list?
  • 55. var items = [1, 2, 3]; //+ reduce :: fn -> a -> array -> a reduce(function (crnt, val, index, list) { return crnt + val; }, 0, items); //> 6
  • 56. var items = [1, 2, 3]; //+ reduce :: fn -> a -> array -> a reduce(function (crnt, val, index, list) { return crnt + val; }, 0, items); //> 6 We don’t use these two arguments so we can omit them
  • 57. var items = [1, 2, 3]; //+ reduce :: fn -> a -> array -> a reduce(function (crnt, val) { return crnt + val; }, 0, items); //> 6
  • 58. var items = [1, 2, 3]; //+ reduce :: fn -> a -> array -> a reduce(function (crnt, val) { return crnt + val; }, 0, items); //> 6 This function looks familiar...
  • 59. //+ add :: a -> a -> a var add = function(x, y) { return x + y; }.autoCurry();
  • 60. var items = [1, 2, 3]; //+ reduce :: fn -> a -> array -> a reduce(function (x, y) { return x + y; }, 0, items); //> 6
  • 61. var items = [1, 2, 3]; //+ reduce :: fn -> a -> array -> a reduce(function (x, y) { return x + y; }, 0, items); //> 6 Instead of inlining the function, just pass the function’s pointer
  • 62. var items = [1, 2, 3]; //+ reduce :: fn -> a -> array -> a reduce(add, 0, items); //> 6
  • 63. var items = [1, 2, 3]; //+ reduce :: fn -> a -> array -> a reduce(add, 0, items); //> 6 Cool! But isn’t reduce curried? Can we make this into a generic function?
  • 64. var items = [1, 2, 3]; //+ reduce :: add -> 0 -> array -> a //+ sum :: array -> integer var sum = reduce(add, 0); sum(items); //> 6
  • 67. Keep your methods short and to the point It is easier to build things if your building blocks are small
  • 68. Stick your data as the last parameter reduce = function (fn, starting value, data) function (general, ..., specific)
  • 69. Optional parameters are hard Make a general function and curry it to take optional stuff
  • 70. Functional purity is pretty cool • Easier to maintain • Easier to reason • Easier to test • Easier to transport