Last active
March 5, 2017 22:06
-
-
Save lubien/395dd2cd8ac4c75a5fe416fd3f4d1c43 to your computer and use it in GitHub Desktop.
Teaching some FP to friends
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var counter = 0 | |
function count() { | |
return counter++ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function count(x) { | |
return x + 1 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = [1, 2, 3] | |
function addToList(list, value) { | |
return [...list, value] | |
} | |
list // => [1, 2, 3] | |
var newList = addToList(list, 4) | |
newList // => [1, 2, 3, 4] | |
list // => [1, 2, 3] | |
newList === list // false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function incrementList(list) { | |
for (var i = 0; i < list.length; i++) | |
list[i]++ | |
return list | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var players = [ | |
{name: 'João' , username: 'lubien' , cards: 6}, | |
{name: 'Rian' , username: 'vergachov', cards: 5}, | |
{name: 'Krishna', username: 'shinark' , cards: 7} | |
] | |
function playerCards(player) { return player.cards } | |
function sum(x, y) { return x + y } | |
players | |
.map(playerCards) // => [6, 5, 7] | |
.reduce(sum) // => 18 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var players = [ | |
{name: 'João' , username: 'lubien' , cards: 6}, | |
{name: 'Rian' , username: 'vergachov', cards: 5}, | |
{name: 'Krishna', username: 'shinark' , cards: 7} | |
] | |
function playerCards(player) { return player.cards } | |
players.map(playerCards) // => [6, 5, 7] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function increment(x) { return x + 1 } | |
function incrementList(list) { return list.map(increment) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mulList(list) { | |
var acc = 1 | |
for (var i = 0; i < list.length; i++) | |
acc *= list[i] | |
return acc | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var list = [1, 2, 3] | |
function addToList(list, value) { | |
list.push(value) | |
return list | |
} | |
list // => [1, 2, 3] | |
var newList = addToList(list, 4) | |
newList // => [1, 2, 3, 4] | |
list // => [1, 2, 3, 4] !! | |
newList === list // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var players = [ | |
{name: 'João' , username: 'lubien' , cards: 6}, | |
{name: 'Rian' , username: 'vergachov', cards: 5}, | |
{name: 'Krishna', username: 'shinark' , cards: 7} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(x, y) { return x + y } | |
function mul(x, y) { return x * y } | |
function reduce(list, fn, initial) { | |
var acc = initial | |
for (var i = 0; i < list.length; i++) | |
acc = fn(acc, list[i]) | |
return acc | |
} | |
function sumList(list) { return reduce(list, sum, 0) } | |
function mulList(list) { return reduce(list, mul, 1) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(x, y) { return x + y } | |
function mul(x, y) { return x * y } | |
function sumList(list) { return list.reduce(sum, 0) } | |
function mulList(list) { return list.reduce(mul, 1) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sum(x, y) { return x + y } | |
function mul(x, y) { return x * y } | |
function squeezeList(list, fn, initial) { | |
var acc = initial | |
for (var i = 0; i < list.length; i++) | |
acc = fn(acc, list[i]) | |
return acc | |
} | |
function sumList(list) { return squeezeList(list, sum, 0) } | |
function mulList(list) { return squeezeList(list, mul, 1) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sumList(list) { | |
var acc = 0 | |
for (var i = 0; i < list.length; i++) | |
acc += list[i] | |
return acc | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function increment(x) { return x + 1 } | |
function updateList(list, fn) { | |
for (var i = 0; i < list.length; i++) | |
list[i] = fn(list[i]) | |
return list | |
} | |
function incrementList(list) { return updateList(list, increment) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment