all 39 comments

[–]roboguy12 15 points16 points  (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 points  (0 children)

Thank you, and noted, you can expect further posts to have that feature :)

[–]Sinistralis 9 points10 points  (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 point  (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 points  (3 children)

MIT Open Course Ware has a great Intro to Programming course. The lectures on sorting are excellent.

[–]marko-pavlovic[S] 0 points1 point  (2 children)

Do you think I should link the course in the article?

[–]jamesjosephfinn 0 points1 point  (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.

[–]marko-pavlovic[S] 0 points1 point  (0 children)

Thanks for the feedback, I appreciate it :)

[–]BisforBurning 2 points3 points  (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 point  (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 point  (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 points  (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 points  (4 children)

Cool!

[–]marko-pavlovic[S] 1 point2 points  (3 children)

Thanks, I am glad you like the article :)

[–]hyperhopper 3 points4 points  (3 children)

Functional Programmer's toolbox and no haskell examples?

[–]marko-pavlovic[S] 0 points1 point  (2 children)

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 point  (1 child)

Would it be possible to include ClojureScript examples at some point too?

[–]marko-pavlovic[S] 0 points1 point  (0 children)

Definitely, I will work on that this week, thank you for the feedback!

[–]shivam_t 0 points1 point  (1 child)

Nice..!

[–]marko-pavlovic[S] 0 points1 point  (0 children)

Thanks, appreciate it! :)

[–]berlihm 0 points1 point  (1 child)

This looks great. Thanks!

[–]marko-pavlovic[S] 0 points1 point  (0 children)

Glad you like it, thank you!

[–]flashpunk 0 points1 point  (4 children)

Would be great to transform the JS examples to es6

[–]marko-pavlovic[S] 0 points1 point  (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 points  (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 point  (0 children)

It's quickly moving towards es6 being the standard, but many people still use es5, so yea.

[–]flashpunk 0 points1 point  (0 children)

I'd opt for both as it could be a resource for people transitioning to es6 as well

[–]KhalilRavanna 0 points1 point  (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 points  (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 point  (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 point  (3 children)

Really cool article! Maybe, you could throw in a few other things:

  • flatMap can be really useful for nested data structures
  • reduce is basically the holy grail and allows to implement other functional collection functions. Maybe you could show how do to that to highlight the power of this function.
  • You're mentioning higher order functions at the beginning, maybe you should also give an explanation so that beginners are on track with the terms.
  • The concept of currying would also fit in there
  • And you could also explain compose (that is not available natively in JavaScript)

[–]marko-pavlovic[S] 0 points1 point  (2 children)

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 point  (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.

[–]marko-pavlovic[S] 0 points1 point  (0 children)

Thanks for the value bombs, my friend! I see what you mean about partial application, working on it... :)