3 ms·
guess that really depends. var nums = Array.apply(0, Array(300)).map(function(x, idx) { return idx; }); var filter = ramda.filter; var map = ramda
by randomguy7788 12y ago
guess that really depends.
var nums = Array.apply(0, Array(300)).map(function(x, idx) {
return idx;
});
var filter = ramda.filter;
var map = ramda.map;
var reduce = ramda.reduce;
var compose = ramda.compose;
var gt100pred = function(x) {
return x > 100;
};
var add = function(a, b) {
return a + b;
};
var result = nums
.filter(gt100pred)
.map(Math.sqrt)
.reduce(add, 0);
var gt100 = filter(gt100pred);
var sqrt = map(Math.sqrt);
var accum = reduce(add, 0);
var result = compose(accum, sqrt, gt100)(nums);
=============
with the above your code does look shorter. but there is one big plus with the latter that not only can you pass around the predicate but the operation in itself can be passed around(gt100 is reusable etc..). (ramda actually works like a functional library wherein the function/predicate is the first argument and the data structure is the last. this makes partial function application and currying work much better)
also when we do away with the boilerplate the final thing looks like this:
// "I want the sum of the square roots
// of all numbers greater than 100 in this list".
var result = nums
.filter(gt100pred)
.map(Math.sqrt)
.reduce(add, 0);
vs
// think of it as pipelining from right to left.
// get any number > 100, sqrt it, then accumulate it.
var result = compose(accum, sqrt, gt100)(nums);
and if this was haskell:
foldl (+) 0 . map sqrt . filter (> 100) $ [0..300]
- rybosome 12y agoWell, there's one of the big problems; param ordering isn't ideal. Believe me, you are preaching to the choir. I had a long argument with the people who maintain our JS library about the merits of adding reverse and curry function so that I could write something like the following: pluck = curry(reverse(object.get)); var names = people.map(pluck('name')); ...but they weren't having it. :-) As much as I like writing Haskell (and believe me, I do) JS can't do it without heavy library support. You may be better off writing LiveScript or Fey at that point.
- codygman 12y agoPurescript[0] seems to be hit a very good balance between not too far from JS while providing the closest to Haskell experience and most of the benefits. Check out the "first steps" tutorial[1] solving Euler #1 0: http://www.purescript.org/ http://www.purescript.org/ 1: http://www.purescript.org/posts/First-Steps/ http://www.purescript.org/posts/First-Steps/
- randomguy7788 12y agooh for sure, i agree with your sentiments (since there are no intrinsic language features nor primitives to make any of this easy in js. even ramda is just a better crutch really). i am actually in the same situation :P but you did say it was unpleasant to read (which i still think it isnt, but not as good as other languages that have better support). haha man i dont know how many time ive written a reverse/flip function in our codebase and then erased it an hour later cause i know i would be called out on it lol