Functional programming in JS is possible, ramda / wu (others?) make it very easy to get started, of course it is not as great as with functional programming languages, but still, it allows you to do pretty cool stuff, it's like lodash but with the callback moved to the first argument, turning something like this:
var isMultilanguage = function(field) {
return field.multilanguage === true;
};
var isNotMultilanguage = function(field) {
return field.multilanguage !== true;
};
var getMultilanguageFields = function (fields) {
return _.filter(fields, isMultilanguage);
};
var getNonMultilanguageFields = function (fields) {
return _.filter(fields, isNotMultilanguage);
};
into this
var isMultilanguage = R.where({multilanguage: true});
var isNotMultilanguage = R.not(isMultilanguage);
var getMultilanguageFields = R.filter(isMultilanguage);
var getNonMultilanguageFields = R.filter(isNotMultilanguage);
The current stable version of Lo-Dash supports _.curry. In 3.0 Lo-Dash will add support _.rearg, _.ary, & _.curryRight.
Using a combo of them you can easily create auto-curried functions.
var where = _.curry(_.rearg(_.where,1,0));
var isMultilang = where({multilanguage: true});
Comments
Functional programming in JS is possible, ramda / wu (others?) make it very easy to get started, of course it is not as great as with functional programming languages, but still, it allows you to do pretty cool stuff, it's like lodash but with the callback moved to the first argument, turning something like this:
var isMultilanguage = function(field) {
};var isNotMultilanguage = function(field) {
};var getMultilanguageFields = function (fields) {
};var getNonMultilanguageFields = function (fields) {
};into this
var isMultilanguage = R.where({multilanguage: true});
var isNotMultilanguage = R.not(isMultilanguage);
var getMultilanguageFields = R.filter(isMultilanguage);
var getNonMultilanguageFields = R.filter(isNotMultilanguage);
https://github.com/ramda/ramda
https://github.com/fitzgen/wu.js
The current stable version of Lo-Dash supports _.curry. In 3.0 Lo-Dash will add support _.rearg, _.ary, & _.curryRight. Using a combo of them you can easily create auto-curried functions.
You also have those in Underscore / Lo-Dash:
_.filter(fields, {multilanguage: true}) _.where(fields, {multilanguage: false})