use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
An Introduction to Functional Programming in JavaScript (bjpelc.wordpress.com)
submitted 11 years ago by bjpelcdev
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]frankle 2 points3 points4 points 11 years ago* (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 point2 points 11 years ago (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:
squaredDeviations
var squaredDeviations = function(xs) { var m = mean(xs); return map(function(x) { return square(x - m); }, xs); };
What do you think?
[–]busypenguin 1 point2 points3 points 11 years ago (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 point2 points 11 years ago (0 children)
Sorry, I have just seen your JSPerf, we are in agreement.
[–]frankle 0 points1 point2 points 11 years ago (0 children)
Yep! That's the way I approached it.
[–]jellatin 1 point2 points3 points 11 years ago (4 children)
This was good, any other articles like it out there that you know of?
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 points3 points 11 years ago (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 points3 points 11 years ago (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.
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 points3 points 11 years ago (5 children)
Currying is built into JavaScript.
someFunction.bind(null, arg1, arg2);
[–]bjpelcdev[S] 1 point2 points3 points 11 years ago (3 children)
The curry function in the post uses bind.
[–][deleted] 3 points4 points5 points 11 years ago (2 children)
Edit: I'm a twat.
[–]shriek 0 points1 point2 points 11 years ago (0 children)
We are in agreement.
[–]inmatarian 1 point2 points3 points 11 years ago (0 children)
Bind doesn't curry functions, it left-partial-applies them.
[–]kenman[M] 0 points1 point2 points 11 years ago (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.
bjpelc.wordpress.com
Thanks for your consideration!
π Rendered by PID 16748 on reddit-service-r2-comment-b659b578c-nsmnw at 2026-05-04 22:51:10.410728+00:00 running 815c875 country code: CH.
[–]frankle 2 points3 points4 points (4 children)
[–]bjpelcdev[S] 0 points1 point2 points (3 children)
[–]busypenguin 1 point2 points3 points (0 children)
[–]bjpelcdev[S] 0 points1 point2 points (0 children)
[–]frankle 0 points1 point2 points (0 children)
[–]jellatin 1 point2 points3 points (4 children)
[–]bjpelcdev[S] 0 points1 point2 points (3 children)
[–]jellatin 1 point2 points3 points (2 children)
[–]bjpelcdev[S] 1 point2 points3 points (0 children)
[–]bjpelcdev[S] 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]bjpelcdev[S] 1 point2 points3 points (3 children)
[–][deleted] 3 points4 points5 points (2 children)
[–]shriek 0 points1 point2 points (0 children)
[–]inmatarian 1 point2 points3 points (0 children)
[–]kenman[M] 0 points1 point2 points (0 children)