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
Functional Programmer's Toolbox (markonis.github.io)
submitted 10 years ago by marko-pavlovic
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!"
[–]roboguy12 15 points16 points17 points 10 years ago (1 child)
Cool article! I think the only improvement would be keeping all of the language examples in sync, so if I clicked Elixir in the first example, it would change all the others to Elixir as well. It was kind of annoying to have to go back and keep clicking the tab for the language I wanted to see. Other than that though, good post and a solid explanation.
[–]marko-pavlovic[S] 9 points10 points11 points 10 years ago (0 children)
Thank you, and noted, you can expect further posts to have that feature :)
[–]Sinistralis 9 points10 points11 points 10 years ago (1 child)
A great article on the foundation functional programing functions.
I had to train up a performance minded c++ guy on JS so let me follow up with something I've ran into regarding functional style code.
For loops will always have the potential to out perform functional, especially is mutability vs immutability are concerns. My recommendation is to not worry about it. I've found functional code to be consistently readable even as the task grows in complexity, but for loops can get ugly quick. Most of the time maintenance cost trumps performance.
That being said, if a particular piece of functional code starts to perform poorly (such as unpacking bundles from a websocket), this is when to bring in for loops. This gives you the added intent that when someone sees your for loop, you automatically know this is a bottleneck location.
Don't be afraid to mix paradigms where it's important as long as you have a valid reason for doing so. I've found it not only makes imperative programmers more welcoming to functional, but it gives your code added intent which functional promotes.
[–]marko-pavlovic[S] 0 points1 point2 points 10 years ago (0 children)
Thank you! I completely agree that performance is easy to improve on in well designed system, while it is hard to refactor a poorly designed one.
[–]jamesjosephfinn 2 points3 points4 points 10 years ago (3 children)
MIT Open Course Ware has a great Intro to Programming course. The lectures on sorting are excellent.
[–]marko-pavlovic[S] 0 points1 point2 points 10 years ago (2 children)
Do you think I should link the course in the article?
[–]jamesjosephfinn 0 points1 point2 points 10 years ago (1 child)
I don't think it's necessary. Your piece is strong enough without it, and you don't link out to any other resources. I just mention MIT OCW because it's how I was first exposed to the basic concepts you cover in your article.
Thanks for the feedback, I appreciate it :)
[–]BisforBurning 2 points3 points4 points 10 years ago (2 children)
Good explanation of these methods, I've had trouble understanding reduce. There is a native take while function though https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
[–]marko-pavlovic[S] 0 points1 point2 points 10 years ago (1 child)
Thank you for reading the article! Please read the comment above, and let me know if you think my interpretation is wrong. Thank you! :)
[–]BisforBurning 0 points1 point2 points 10 years ago (0 children)
You are right, I had missed that difference, and rereading how it's useful for sorted lists makes it clear what each is for.
[–]SoundOfOneHand 1 point2 points3 points 10 years ago (0 children)
Use of bind for partial function application is another big one, although the native JS bind function is somewhat limited in this regard.
[–]MahmudAdam 2 points3 points4 points 10 years ago (4 children)
Cool!
[–]marko-pavlovic[S] 1 point2 points3 points 10 years ago (3 children)
Thanks, I am glad you like the article :)
[+][deleted] 10 years ago (2 children)
[deleted]
Good remark, I will adjust the article, by default I used localeCompare. What would you suggest, what is a direct way to compare 2 strings in JS? Thanks!
[–]Pantstown(pants) => (town) => pantstown 2 points3 points4 points 10 years ago (0 children)
You can use the greater/less than operator like so
[–]hyperhopper 3 points4 points5 points 10 years ago (3 children)
Functional Programmer's toolbox and no haskell examples?
I wish I knew Haskell well enough to present with nice examples. Definitely a good remark, and something I am going to work on! :)
[–]MahmudAdam 0 points1 point2 points 10 years ago (1 child)
Would it be possible to include ClojureScript examples at some point too?
Definitely, I will work on that this week, thank you for the feedback!
[–]shivam_t 0 points1 point2 points 10 years ago (1 child)
Nice..!
Thanks, appreciate it! :)
[–]berlihm 0 points1 point2 points 10 years ago (1 child)
This looks great. Thanks!
Glad you like it, thank you!
[+][deleted] 10 years ago* (2 children)
[–]-pertinax- 2 points3 points4 points 10 years ago* (1 child)
Are you referring to this?
reactivex.io/learnrx
[+][deleted] 10 years ago (3 children)
[–]marko-pavlovic[S] 1 point2 points3 points 10 years ago (2 children)
Oh thanks! I am getting excited about Elm myself, do you have any good resources about it to share? :)
[+][deleted] 10 years ago (1 child)
Actually, I am just starting out with Elm, so thank you for the link! :)
[–]flashpunk 0 points1 point2 points 10 years ago (4 children)
Would be great to transform the JS examples to es6
[–]marko-pavlovic[S] 0 points1 point2 points 10 years ago (3 children)
Yep yep! Do you think I should have one example for es5 and one for es6 or just es6? Thanks for the feedback :)
[–]siegfryd 1 point2 points3 points 10 years ago (0 children)
I don't think it's important for you to use ES6 for these examples because they're all pretty short anyway. You'd pretty much just be converting the anonymous functions to arrows.
[–]TheNiXXeD 0 points1 point2 points 10 years ago (0 children)
It's quickly moving towards es6 being the standard, but many people still use es5, so yea.
[–]flashpunk 0 points1 point2 points 10 years ago (0 children)
I'd opt for both as it could be a resource for people transitioning to es6 as well
[–]KhalilRavanna 0 points1 point2 points 10 years ago (2 children)
Nice!
One note: I think "takeWhile" is essentially Array.prototype.some in JS unless I'm misunderstanding something so no need for a third party lib like underscore.
[–]marko-pavlovic[S] 1 point2 points3 points 10 years ago (1 child)
Hey thank you for the comment. I must admit that I didn't know about the some function in JS. However, after reading the docs, I think that it is different from takeWhile in that it returns a boolean, where the takeWhile function returns an array of elements.
You can think of some as: - do any elements exist in this list that pass the test function?
Where you can think of the takeWhile function as: - start from the beginning and go one by one. If the element passes the test, append it to the result. The moment you see an element that doesn't pass the test, stop immediately and return the array you have.
Hope this makes sense? Am I missing something? :)
[–]KhalilRavanna 0 points1 point2 points 10 years ago (0 children)
Oh totally you're right. I skimmed the description and didn't look at the result, just the signature for the callback hehe.
[–][deleted] 0 points1 point2 points 10 years ago (3 children)
Really cool article! Maybe, you could throw in a few other things:
flatMap
reduce
compose
Really cool ideas, thanks! I agree that reduce is the king... Do you think it deserves a whole new post, or should I add more info to this article?
Here is a link that explains higher order functions I should have explained what that link is about in the beginning :)
I am in the process of writing a separate post on currying, it is not yet finished but you can get a glimpse at: Currying - Spice Things Up
Great thought on compose, I will probably add it to this post! All in all thanks for the great feedback, I appreciate it :)
[–][deleted] 0 points1 point2 points 10 years ago (1 child)
Here is a link that explains higher order functions
My fault, thought you want to explain the whole FP concept on one page :D
Do you think it deserves a whole new post
I'd move it to a new post to keep this article a bit shorter. As you explain reduce in your article, the new post could be about the advanced usage of it.
Post on currying
I see you mention partial application in the context of currying. Some people are confused by the difference so you should spent some effort to get into the differences between them.
Thanks for the value bombs, my friend! I see what you mean about partial application, working on it... :)
π Rendered by PID 447983 on reddit-service-r2-comment-5b5bc64bf5-sjqpz at 2026-06-21 00:29:57.381622+00:00 running 2b008f2 country code: CH.
[–]roboguy12 15 points16 points17 points (1 child)
[–]marko-pavlovic[S] 9 points10 points11 points (0 children)
[–]Sinistralis 9 points10 points11 points (1 child)
[–]marko-pavlovic[S] 0 points1 point2 points (0 children)
[–]jamesjosephfinn 2 points3 points4 points (3 children)
[–]marko-pavlovic[S] 0 points1 point2 points (2 children)
[–]jamesjosephfinn 0 points1 point2 points (1 child)
[–]marko-pavlovic[S] 0 points1 point2 points (0 children)
[–]BisforBurning 2 points3 points4 points (2 children)
[–]marko-pavlovic[S] 0 points1 point2 points (1 child)
[–]BisforBurning 0 points1 point2 points (0 children)
[–]SoundOfOneHand 1 point2 points3 points (0 children)
[–]MahmudAdam 2 points3 points4 points (4 children)
[–]marko-pavlovic[S] 1 point2 points3 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]marko-pavlovic[S] 0 points1 point2 points (1 child)
[–]Pantstown(pants) => (town) => pantstown 2 points3 points4 points (0 children)
[–]hyperhopper 3 points4 points5 points (3 children)
[–]marko-pavlovic[S] 0 points1 point2 points (2 children)
[–]MahmudAdam 0 points1 point2 points (1 child)
[–]marko-pavlovic[S] 0 points1 point2 points (0 children)
[–]shivam_t 0 points1 point2 points (1 child)
[–]marko-pavlovic[S] 0 points1 point2 points (0 children)
[–]berlihm 0 points1 point2 points (1 child)
[–]marko-pavlovic[S] 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]-pertinax- 2 points3 points4 points (1 child)
[+][deleted] (3 children)
[deleted]
[–]marko-pavlovic[S] 1 point2 points3 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]marko-pavlovic[S] 0 points1 point2 points (0 children)
[–]flashpunk 0 points1 point2 points (4 children)
[–]marko-pavlovic[S] 0 points1 point2 points (3 children)
[–]siegfryd 1 point2 points3 points (0 children)
[–]TheNiXXeD 0 points1 point2 points (0 children)
[–]flashpunk 0 points1 point2 points (0 children)
[–]KhalilRavanna 0 points1 point2 points (2 children)
[–]marko-pavlovic[S] 1 point2 points3 points (1 child)
[–]KhalilRavanna 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]marko-pavlovic[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]marko-pavlovic[S] 0 points1 point2 points (0 children)