Last active
July 7, 2017 21:58
Star
You must be signed in to star a gist
Functional Javascript
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
"use strict"; | |
//Array.prototype.map | |
function multiplyBy ( multiplier ) { | |
return function ( value ) { | |
return value * multiplier; | |
}; | |
} | |
var array = [1, 2, 3, 4, 2]; | |
var doubled = array.map( multiplyBy( 2 ) ); | |
console.log(doubled); | |
//Array.prototype.reduce | |
function doubleAll ( result, value ) { | |
return result + multiplyBy( 2 )( value ); | |
} | |
var doubledAllValue = array.reduce( doubleAll, 0); | |
console.log(doubledAllValue); | |
//Array.prototype.filter | |
function valueCompare ( value ) { | |
return function ( number ) { | |
return number === value; | |
}; | |
} | |
var twos = array.filter( valueCompare( 2 ) ); | |
console.log(twos); |
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
//We remove use strict so our program will not immediatly fail at line 9 | |
const a = "Hamilton" | |
const b = { title: "Let It Go", | |
album: "Frozen"}; | |
b.album = a; // This should work | |
console.log(b); | |
a = "Wicked"; // This should fail | |
console.log(a); |
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
"use strict"; | |
function doubleMap(numbers) { | |
return numbers.map( function (n) { | |
return n * 2; | |
}); | |
} | |
console.log(doubleMap([ 2, 3, 4])); |
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
//We remove use strict so our program will not immediatly fail at line 7 and 9 | |
const a = "Hamilton" | |
const b = Object.freeze({ title: "Let It Go", | |
album: "Frozen", | |
ratings: [ 2, 3, 2, 5 ]}); | |
b = a; // This should fail | |
console.log(b); | |
b.album = a; // This should fail | |
console.log(b); | |
b.ratings.push(1); // This should work | |
console.log(b); |
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
"use strict"; | |
function toLowerCase ( string ) { | |
return string.toLocaleLowerCase(); | |
} | |
function dashify ( string ) { | |
return string.replace(" ", "-"); | |
} | |
var slug = encodeURIComponent( | |
dashify( | |
toLowerCase("Blog Title"))); | |
console.log(slug); |
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
"use strict"; | |
function doubleMap(numbers) { | |
var doubled = [], i = 0; | |
for (; i < numbers.length; i++) { | |
doubled.push(numbers[i] * 2); | |
} | |
return doubled; | |
} | |
console.log(doubleMap([ 2, 3, 4])); |
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
"use strict"; | |
function pipe(args) { | |
// We can use an array or the argument list | |
var fns = Array.isArray(args) ? args : | |
Array.prototype.slice.call(arguments); | |
return function(initValue) { | |
return fns.reduce(function(value, fn) { | |
return fn(value); | |
}, initValue); | |
}; | |
} | |
function toLowerCase(string) { | |
return string.toLocaleLowerCase(); | |
} | |
function dashify(string) { | |
return string.replace(" ", "-"); | |
} | |
function appendToUrl(url) { | |
return function(slug) { | |
return url + slug; | |
}; | |
} | |
var title = "Blog Title"; | |
var host = "https://google.com/" | |
var slugify = pipe(toLowerCase, | |
dashify, | |
encodeURIComponent); | |
var urlify = function(url) { | |
return pipe(slugify, | |
appendToUrl(url)); | |
}; | |
var slug = slugify(title); | |
var url = urlify(title); | |
console.log(slug); | |
console.log(url); |
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
"use strict"; | |
var c = 2; | |
function impureFunction ( item ) { | |
item.a = item.a * c; | |
return item.a; | |
} | |
function impureFunction2 ( item, b ) { | |
item.a = item.a * b; | |
return item.a; | |
} | |
function pureFunction ( a, b ) { | |
return a * b; | |
} | |
var i = { a: 5 }; | |
console.log(impureFunction(i)); //Not pure. Mutates item. Shared State. | |
console.log(c, i); | |
console.log(impureFunction2(i, c)); // Not pure. Still mutates item. | |
console.log(c, i); | |
console.log(pureFunction(i.a, c)); // Pure. Doesn't mutate item, shared state isn't used. | |
console.log(c, i); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment