Live data from Hacker News

Javascript Arrays and Functional Programming

zabanaa.github.io

1–10 of 92 posts

Re: Javascript Arrays and Functional Programming

#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.

Re: Javascript Arrays and Functional Programming

#4
Great article, but questionable formatting: `return {name: player.name, age: player.age }`

It also seems wrong to mutate the accumulator in the reduce example, it would just be simpler to use `forEach` and use a variable defined outside if you're going to write code this way?

Re: Javascript Arrays and Functional Programming

#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 )

Re: Javascript Arrays and Functional Programming

#6

Great article, but questionable formatting: `return {name: player.name, age: player.age }` It also seems wrong to mutate the accumulator in the reduce example, it would just be simpler to use `forEach` and use a variable defined outside if you're going to write code this way?

Wouldn't do it with variable, since it will make your function impure [1]

1 : http://www.nicoespeon.com/en/2015/01/pure-functions-javascri...

Re: Javascript Arrays and Functional Programming

#7
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/filter/reduce to an imperative language doesn't get you close to the level of robustness and ease of implementation for that safety you'd get in a functional language.

Re: Javascript Arrays and Functional Programming

#8

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…

Because (a) we don't have an unambiguous definition of what functional programming is and (b) we don't have a better term for what this style of programming is.

I guess it could be called combinator-oriented programming or similar.

Re: Javascript Arrays and Functional Programming

#9

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…

Because (a) we don't have an unambiguous definition of what functional programming is and (b) we don't have a better term for what this style of programming is. I guess it could be called combinator-oriented programming or similar.

Sounds more like "coding with map, reduce and filter" to me. It beats complex for-loops where mapping, reducing and filtering are all mixed in together but I wouldn't call it functional programming.

Re: Javascript Arrays and Functional Programming

#10
TL;DR author provides an example of map, filter and reduce functions in JS.

I have been wondering lately what is the value of articles like this. They do not convey any meaningful idea, they do not offer any useful insight on underlying matters; why do people even write something like this, except to litter the Internet even more? Anyone, who is distinctly familiar with functional programming or even just with the concept of higher order functions, surely knows those three basic primitives; those, who are not, would benefit much more from the basic concepts of first-class functions etc, than this random abrupt post.

Post reply on HN