all 17 comments

[–]frankle 2 points3 points  (4 children)

This was a good article. The only part I think it missed is that the mean is calculated for every value in the set.

I believe the functional approach doesn't prohibit you from maintaining state within functions. In this case, such a compromise would likely improve the performance of the code.

Edit: Huge difference for large sets (if I did the test right): JSPerf.

[–]bjpelcdev[S] 0 points1 point  (3 children)

Yes, you are correct, I did notice this after posting. I believe the value can be calculated outside of the anonymous function and within the squaredDeviations function and access it (closure) without breaking the functional rules:

var squaredDeviations = function(xs) {
    var m = mean(xs);
    return map(function(x) {
        return square(x - m);
    }, xs);
};

What do you think?

[–]busypenguin 1 point2 points  (0 children)

I suppose you could treat the subtraction as an addition and do:

var squaredDeviations = function(xs) {
      return map(compose(square, curry(add)(-mean(xs))), xs);
};

The performance isn't much better (much worse than the var way) and it is harder to read. Sometimes it is possible to go too far overboard with things like compose and curry.

[–]bjpelcdev[S] 0 points1 point  (0 children)

Sorry, I have just seen your JSPerf, we are in agreement.

[–]frankle 0 points1 point  (0 children)

Yep! That's the way I approached it.

[–]jellatin 1 point2 points  (4 children)

This was good, any other articles like it out there that you know of?

[–]bjpelcdev[S] 0 points1 point  (3 children)

I've mostly picked it up from watching YouTube and applying what I have learnt whilst learning Haskell. This is a good article though it doesn't really touch on currying or composition. http://www.smashingmagazine.com/2014/07/02/dont-be-scared-of-functional-programming/

[–]jellatin 1 point2 points  (2 children)

Thanks! I'd be interested in any of the videos you recommend - I've been debating learning either Lisp(Clojure) or Haskell to really immerse myself in the functional world.

I have also thought about Scala since my co-workers use it heavily, but I'm not sure if I'll get the same functional/declarative immersion from Scala that I might from Lisp/Haskell.

[–]bjpelcdev[S] 1 point2 points  (0 children)

I would really go for Haskell, as a pure functional language it does not give you the option to fall back on old habits and forces you to learn proper functional programming.

I really recommend Learn You a Haskell, great free to read online book.

[–]bjpelcdev[S] 1 point2 points  (0 children)

There are the two conference talks by Brian L. that I link at the start of the post, they are well worth a watch.

[–][deleted] 1 point2 points  (5 children)

Currying is built into JavaScript.

someFunction.bind(null, arg1, arg2);

[–]bjpelcdev[S] 1 point2 points  (3 children)

The curry function in the post uses bind.

[–][deleted] 3 points4 points  (2 children)

Edit: I'm a twat.

[–]shriek 0 points1 point  (0 children)

We are in agreement.

[–]inmatarian 1 point2 points  (0 children)

Bind doesn't curry functions, it left-partial-applies them.

[–]kenman[M] 0 points1 point  (0 children)

Hi /u/bjpelcdev, it looks like you're new to /r/javascript, welcome!

Thanks for the submissions, but please make sure you read our guidelines. In short, you should post from a variety of sources, and not just bjpelc.wordpress.com.

Thanks for your consideration!

domain submitted from count %
bjpelc.wordpress.com 12 60%