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
/* | |
* Functional Programming in JavaScript | |
* Chapter 01 | |
* Magical -run- function in support of Listing 1.1 | |
* Author: Luis Atencio | |
*/ | |
// -run- with two functions | |
var run2 = function(f, g) { | |
return function(x) { | |
return f(g(x)); |
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
class Person { | |
constructor(firstname, lastname) { | |
this._firstname = firstname; | |
this._lastname = lastname; | |
} | |
set firstname(f) { | |
this._firstname = f; | |
} |
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 money(value, currency) { | |
var _value = value; | |
var _currency = currency; | |
return { | |
value: function () { | |
return _value; | |
}, | |
currency: function () { | |
return _currency; |
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
class Person { | |
constructor(firstname, lastname) { | |
this._firstname = firstname; | |
this._lastname = lastname; | |
} | |
// returns a new copy of the object | |
set firstname(f) { | |
return new Person(f, this._lastname); | |
} |
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 ana = new Person('Ana', 'Gonzalez'); | |
var firstnameLens = R.lensProp('firstname'); | |
// read firtstname | |
R.view(firstnameLens, ana); //-> 'Ana' | |
// transform firstname to upper case | |
R.over(firstnameLens, R.toUpper, ana); //-> 'ANA' | |
// set firstname (creates a branch new object) |
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
// Collection of person objects | |
var people = [ | |
ana, luis, carlos, nestor, maria | |
]; | |
var firstnames = | |
R.compose( | |
R.map(R.view(firstnameLens)), | |
R.map(R.over(firstnameLens, R.toUpper))); | |
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
// Remove the first element | |
var people = [ | |
ana, luis, carlos, nestor, maria | |
]; | |
function head(arr) { | |
return arr.shift(); | |
} | |
head(people); // side effect (changed the content of the original array - people) | |
console.log(firstnames(people)); //-> ["LUIS", "CARLOS", "NESTOR", "MARIA"] |
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 headLens = R.lensIndex(0); // point to first element in the array | |
R.view(headLens, people); //=> ana | |
var array2 = R.set(headLens, 'OLIVIA', firstnames); //=> ["OLIVIA", "LUIS", "CARLOS", "NESTOR", "MARIA"] | |
R.over(headLens, R.toLower, array2); //=> ["olivia", "LUIS", "CARLOS", "NESTOR", "MARIA"] |