Live data from Hacker News

Javascript Arrays and Functional Programming

zabanaa.github.io

61–70 of 92 posts

Re: Javascript Arrays and Functional Programming

#61

Earlier quoted context omitted.

From my comment below: "Thank you for this! I've been learning JS for React and .reduce, .every and .some are new to me! Super helpful" I feel like learning is always easier in smaller bitesizes and the person wrote something very clear and digestible. Sounds like it wasn't aimed at more advanced programmers like yourself

The blog post has tremendous value to the right audience, no doubt about that. But it's worse than worthless for its actual target audience because the article makes those people feel like they're supposed to get it, and they probably feel shitty when they don't (I did). Even though they shouldn't. No one expects you to read that blog post and get it if you've never used Map or Reduce before. It's misleading and dish…

When I discovered Array.map, I already knew about map/filter/reduce. The reason it was a discovery is that the bulk of Javascript resources used for(){} loops and in those few places where map/filter/reduce were used, many implemented map/filter/reduce as library functions because Array.map etc were not always part of Javascript...I think Mozilla added them at some point based on how I found out about them.

Re: Javascript Arrays and Functional Programming

#62
post #13
post #5

Some more practical examples // Grab unique [1,1,2,3,4].filter( ( item, index, array ) => array.indexOf( item ) === index ) // => [1,2,3,4] // Flatten [[1,2],[3,4]].reduce( ( result, item ) => result.concat(item), [] ); // => [1,2,3,4] Not sure why the author missed `sort`. // Sort [1,2,4,3].sort( ( a, b ) => a - b )

The first is slow (O(n²) where O(n) will do), and the second is very likely to be slow (probably the same).

If you wanted to do O(n) functionally, you could do

    [1, 1, 2, 3, 4].filter(
      (obj => x => {
        if (obj[x]) {
          return false;
        }
        obj[x] = true;
        return true;
      })({}),
    );
What's happening is that you're passing an object (hashmap / hashset) into a function that returns a filtering function, and that object is used inside the closure to track the dupes. It's still a pure function because even though you're mutating the passed in object, the filtering function is still deterministic and referentially transparent.

Re: Javascript Arrays and Functional Programming

#63
post #5

Some more practical examples // Grab unique [1,1,2,3,4].filter( ( item, index, array ) => array.indexOf( item ) === index ) // => [1,2,3,4] // Flatten [[1,2],[3,4]].reduce( ( result, item ) => result.concat(item), [] ); // => [1,2,3,4] Not sure why the author missed `sort`. // Sort [1,2,4,3].sort( ( a, b ) => a - b )

For unique, I prefer to let javascript do the work:

  // Grab unique
  [...new Set([1,1,2,3,4])]

Re: Javascript Arrays and Functional Programming

#64
post #5

Some more practical examples // Grab unique [1,1,2,3,4].filter( ( item, index, array ) => array.indexOf( item ) === index ) // => [1,2,3,4] // Flatten [[1,2],[3,4]].reduce( ( result, item ) => result.concat(item), [] ); // => [1,2,3,4] Not sure why the author missed `sort`. // Sort [1,2,4,3].sort( ( a, b ) => a - b )

For unique, I prefer to let javascript do the work: // Grab unique [...new Set([1,1,2,3,4])]

Although it wouldn't work if you were using objects and wanted to do a deep comparison e.g.

    [...new Set([{x: 1}, {x: 1}])]

Re: Javascript Arrays and Functional Programming

#65
post #64

Earlier quoted context omitted.

For unique, I prefer to let javascript do the work: // Grab unique [...new Set([1,1,2,3,4])]

Although it wouldn't work if you were using objects and wanted to do a deep comparison e.g. [...new Set([{x: 1}, {x: 1}])]

... an important observation, for sure.

Re: Javascript Arrays and Functional Programming

#66
post #64

Earlier quoted context omitted.

For unique, I prefer to let javascript do the work: // Grab unique [...new Set([1,1,2,3,4])]

Although it wouldn't work if you were using objects and wanted to do a deep comparison e.g. [...new Set([{x: 1}, {x: 1}])]

Yeah, I recently did a project in Javascript (as a longtime C++ programmer) and was bitten by this exact problem.

Javascript is great but sometimes I miss the predicability of the STL.

Re: Javascript Arrays and Functional Programming

#67

I accept that this is a widely used phrase now but I never understood why just using map/filter/reduce and avoiding state is enough for code to be called "functional programming". Most functional languages feature pattern matching, algebraic data types, purity, currying, strong typing, type inference, recursion instead of loops and types that cannot be null. It's a completely different style of coding. Adding map/fil…

I think most of it is the ease (demonstrated in the comment) with which "functional programming" elides into "functional languages." Functional programming is a technique. Programming languages are tools. Some tools generally facilitate functional programming techniques better than other tools.

Robust programs can be written in many languages, including 'many languages' in the sense of heterogeneity as demonstrated when a Haskell program communicates over the internet hopping through routers and switches running embedded and iOT and OS quality C code...and worse.

Functional programming techniques are great, until something else is needed. At some point databases become useful. At some point caches become important. At some point everyone winds up needing random numbers. Relying on statistics and probability isn't a bad idea either, right Siri and Cortana and Alexa?

Re: Javascript Arrays and Functional Programming

#69
post #3

JavaScript has an advantage on Python in this style, because Python lambdas are hideously ugly and JS anonymous functions are less hideously ugly. In Python it tends to be cleaner (and sometimes faster) to write list comprehensions instead of map() and filter(), especially since Python also has lazy generator comprehensions.

Python actually discourages using map anf filter. Which is kind of weird to me but whatever.

Well in some cases it really is more efficient.

    [func(x) if x in collection if cond(x)]
makes a single pass over the data and gives you a list, but

    list(map(func, filter(cond, collection)))
makes two passes and requires some extra function calls.

I think the real reason is perceived legibility on the part of the Python core devs. I seem to recall a document from a while ago in which GvR himself came out in favor of list comprehension style. "Explicit is better than implicit".

Re: Javascript Arrays and Functional Programming

#70
post #43
post #36

In his example Array.prototype.reduce, isn't this almost the same as just doing a foreach loop? I don't understand why doing this, it's pretty much the same number of lines.

In this example it is, but a nice thing about using map, reduce etc is that you can chain them together, ex: let out = ["a", "b", "c", 1, 2, 3] // Turn letters to upper case .map(i => { return typeof i === "string" ? i.toUpperCase() : i; }) // Add one to numbers .map(i => { return typeof i === "number" ? i + 1 : i; }) // split letters and numbers up .reduce((all, i) => { typeof i === "string" ? all.letters.push(i) :…

Or... you can do all of that in a 4-line loop, without any of the harm to readability or additional code-bloat that's especially important for websites.
Post reply on HN