Skip to content

Instantly share code, notes, and snippets.

View luijar's full-sized avatar

Luis Atencio luijar

View GitHub Profile
@luijar
luijar / lens-arr-head.js
Created September 26, 2015 21:56
Avoid side effect with lenses
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"]
// 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"]
@luijar
luijar / lens-obs.js
Created September 26, 2015 21:38
Mapping lenses over a collection of objects
// 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)));
@luijar
luijar / simple-lens.js
Last active September 26, 2015 20:53
Ramda and Lenses
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)
@luijar
luijar / person-copy-on-change.js
Created September 25, 2015 23:29
Copy on change in person class
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);
}
@luijar
luijar / money.js
Created September 25, 2015 23:00
Simple Money Value Object
function money(value, currency) {
var _value = value;
var _currency = currency;
return {
value: function () {
return _value;
},
currency: function () {
return _currency;
@luijar
luijar / person.js
Created September 25, 2015 22:13
Simple person class
class Person {
constructor(firstname, lastname) {
this._firstname = firstname;
this._lastname = lastname;
}
set firstname(f) {
this._firstname = f;
}
@luijar
luijar / ch01-magic-run.js
Last active June 2, 2024 12:33
Functional Programming in JavaScript Chapter 01 - run function
/*
* 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));