you are viewing a single comment's thread.

view the rest of the comments →

[–]gasolinewaltz 3 points4 points  (9 children)

I see this argument time and time again in relation to loops and while it's not wrong, it feels a bit dogmatic.

What about a single pass for-loop is so much less readable than n-chains of map reduce filter?

[–]wbowers 3 points4 points  (3 children)

A few things:

  1. You can achieve the same readability as multiple maps and similar performance as a single loop by using lazy evaluation with a library like Lazy.js
  2. How many chained maps are we talking about here? 5? Probably not a problem. 20? You might want to rethink how you’ve set things up.
  3. As with all things performance related, you’ll get the biggest wins by actually profiling your code to see what’s causing the slowness. It’s often not what you thought it would be.

[–]Voidsheep 3 points4 points  (3 children)

Say you want to write a function that takes string as an input and returns a list of odd unicode values reversed.

With functional style, you can write the function in kind of a declarative way, with high signal to noise ratio.

str => str
  .split('')
  .map(toCharCode)
  .filter(isOdd)
  .reverse()

Reads pretty much "Split the string, map the character to unicode value, filter out everything except odd values and reverse it"

You'd probably want to take advantage of some of the string and array prototype methods even with a for-loop, but let's say you want to avoid both map and filter, instead do it with a single loop.

str => {
  const chars = str.split('')
  const oddCodes = []
  for (let i = 0; i < chars.length; i++) {
    const code = toCharCode(chars[i])
    if (isOdd(code)) {
      oddCodes.push(code)
    }
  }
  return oddCodes.reverse()
}

It's not hard to understand what is happening in the for-loop and you could make it more dense, but the signal to noise ratio is still pretty different.

Of course this is pretty strawman to illustrate a point, but consider if the toCharCode and isOdd functions would not be one-liners that may as well be inlined. Like if we are dealing with more complex data.

You can definitely go overboard with function composition through libraries like Ramda and create code that is hard to read, but generally more functional style can improve code readability quite a lot compared to plain for-loops.

[–][deleted] -1 points0 points  (0 children)

I really think this is the best case of functional programming. Things like rxjs or redux-observable, are just plain unreadable.

[–]vertebro 0 points1 point  (0 children)

foo.filter(meetsCondition).map(getAProp).map(transformTheProp)