4 ms·
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 lan
by vespakoen 12y ago
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);
https://github.com/ramda/ramda https://github.com/ramda/ramda
https://github.com/fitzgen/wu.js https://github.com/fitzgen/wu.js
- krebby 12y agoYou also have those in Underscore / Lo-Dash: _.filter(fields, {multilanguage: true}) _.where(fields, {multilanguage: false})
- jdd 12y agoThe 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});