Ramda: Practical Functional Javascript

• first class functions
• higher order functions
• purity
• immutability
• composition
• partial application & curry
• recursion & optimization
• Lazy evaluation

Reduce

Map

Filter

# Recognize most loops are one of

## another _ ???

Ramda is NOT a drop-in replacement for Underscore (or LoDash). It is intended to work with a different style of coding.

```Underscore/LoDash
_.map([1, 2, 3], multiply3);
// → [3, 6, 9]```
` `
• Ramda methods are automatically curried.

• Ramda takes the function(s) first, and the data last.

```Ramda
R.map(multiply3, [1, 2, 3]);```
`// → [3, 6, 9]`

# Currying

Currying is the process of turning a function that expects multiple parameters into one that, when supplied fewer parameters, returns a new function that awaits the remaining ones.

``````
var getProperty = R.curry(function(prop, obj) {
return obj[prop];
});

``````
``````
var getProperty = function(prop, obj) {
return obj[prop];
};

var name = getProperty('name', {'name' : 'damillionaire'});

``````
``````

var getName = getProperty('name');
``````
``````var double = R.multiply(2);

double(13);  // → 26``````
``````
var greater = function(a,b) {
return a > b ? a : b;
};

var max = R.reduce(greater, -Infinity);

max([1,-3483,9,7,2]) // 9
max([-21,-3483,-2,-1]) // -1
``````
``R.multiply(2, 13); // → 26``

_ vs Ramda

``````underscore/lodash

var firstTwoLetters = function(words) {
return _.map(words, function(word) {
return _.first(word, 2);
});
}

firstTwoLetters(['andy', 'nero']);
// [‘an’, ‘ne’]
``````
``````Ramda.js

var firstTwoLetters = R.map(R.first(2));

firstTwoLetters(['andy', 'nero']);
// [‘an’, ‘ne’]
``````

# compose

``````
function compose(g, f) {
return function(x) {
return g(f(x));
};
}``````
``````
var wordCount = R.compose(R.length, R.split(' '));

wordCount("There are seven words in this sentence"); // 7``````
``````var wordCount = function(str) {
var words = _.split(' ', str);
return _.length(words);
}

wordCount("There are seven words in this sentence"); // 7``````

# compose

"Cool. But does it do promises?"

# R.pipeP()

``````var data = {
result: "SUCCESS",
interfaceVersion: "1.0.3",
requested: "10/17/2013 15:31:20",
lastUpdated: "10/16/2013 10:52:39",
{id: 104, complete: false,            priority: "high",
title: "Do something",      created: "9/22/2013"},
{id: 105, complete: false,            priority: "medium",
title: "Do something else", created: "9/22/2013"},
{id: 107, complete: true,             priority: "high",
title: "Fix the foo",       created: "9/22/2013"},
{id: 108, complete: false,            priority: "low",
title: "Adjust the bar",    created: "9/25/2013"},
{id: 110, complete: false,            priority: "medium",
title: "Rename everything", created: "10/2/2013"},
{id: 112, complete: true,             priority: "high",
title: "Alter all quuxes",  created: "10/5/2013"}
// , ...
]
};``````

Suppose we expect to get some data like this:

``````

// OUTPUT
[
{id: 110, title: "Rename everything",
dueDate: "2013-11-15", priority: "medium"},
{id: 104, title: "Do something",
dueDate: "2013-11-29", priority: "high"}
]``````
``````getIncompleteTaskSummaries = function(membername) {
return fetchData()
.then(function(data) {
})
var results = [];
for (var i = 0, len = tasks.length; i < len; i++) {
}
}
return results;
})
var results = [];
for (var i = 0, len = tasks.length; i < len; i++) {
}
}
return results;
})
for (var i = 0, len = tasks.length; i < len; i++) {
results.push({
})
}
return results;
})
var a = first.dueDate, b = second.dueDate;
return a < b ? -1 : a > b ? 1 : 0;
});
});
};
``````
``````

R.composeP(R.sortBy(R.get('dueDate')),
R.map(R.pick(['id', 'dueDate', 'title', 'priority'])),
R.reject(R.propEq('complete', true))
fetchData);

``````

var sortByDueDate = R.composeP(R.sortBy(R.porp('dueDate')));

var getRequiredFields = R.composeP(R.map(R.pick(['id', 'dueDate', 'title', 'priority'])));

var removeStatusComplete = R.composeP(R.reject(R.propEq('complete', true));

R.pCompose(sortByDueDate,
getRequiredFields,
removeStatusComplete)
fetchData);

``````
function log(x) {
console.log(x);
return x;
};

R.pCompose(sortByDueDate,
getRequiredFields,
removeStatusComplete)
log,
log,
fetchData);

``````"var vanillia = ['10', '10', '10'];"
"vanilia.map(parseInt)"
[10, NaN, 2]

"_.map(['10','10','10'], parseInt);"
[10, NaN, 2]

"lodash.map(['10','10','10'], parseInt);"
[10, NaN, 2]
``````
``````
"R.map(parseInt, ['10', '10', '10']);"
[10, 10, 10]
``````

• Encourages lean functions

• More declarative

• More testable code

• Build new functions from other functions

• Avoid specifying sequence

• Avoid unnecessary variables and functions

• Mathematically backed

## why CURRY / COMPOSE?

map, reduce, filter

+ Functor's

=

## </>

``````

01 function curry(fn) {
02   return function() {
03     if (fn.length > arguments.length) {
04       var slice = Array.prototype.slice;
05       var args = slice.apply(arguments);
06       return function() {
07         return fn.apply(
08           null, args.concat(slice.apply(arguments)));
09       };
10     }
11     return fn.apply(null, arguments);
12   };
13 }``````
``````
var setResults = setHtml('#results');
``````
``````
var renderVideos = R.map(render);
``````
``````
var getVideos = R.pCompose(R.prop('entry'),
R.prop('feed'),
R.prop('data'));
``````
``````var queryYoutube = R.pCompose(setResults,
renderVideos,
getVideos,
http.getJSON);

``````
``````