Live data from Hacker News

Javascript Arrays and Functional Programming

zabanaa.github.io

11–20 of 92 posts

Re: Javascript Arrays and Functional Programming

#11

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…

Wiki sums up the term well enough:

"In computer science, functional programming is a programming paradigm — a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data."

To that end map-reduce fits and can be referred to as 'functional programming'. The other things you mention are just great language features, but not specific to functional languages. Imperative C++ also has type safety, Swift has non-null types, etc.

Re: Javascript Arrays and Functional Programming

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

Re: Javascript Arrays and Functional Programming

#14

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 co…

While it may not be valuable to the vast majority of people, it was probably valuable to the author at least.

The article is not ground breaking or even front page worthy, but that doesn't mean it's the litter of the Internet. At absolute worst it's an exercise in thinking about a subject and trying to explain it to others.

Re: Javascript Arrays and Functional Programming

#15

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 co…

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

Re: Javascript Arrays and Functional Programming

#16

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…

Wiki sums up the term well enough: "In computer science, functional programming is a programming paradigm — a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data." To that end map-reduce fits and can be referred to as 'functional programming'. The other things you mention are just great langu…

> The other things you mention are just great language features, but not specific to functional languages. Imperative C++ also has type safety, Swift has non-null types, etc.

All those features in combination are shared by the vast majority of languages called functional programming languages. They're pretty much the defining features of functional languages was my point. Other languages can have a subset of the features but it doesn't make them functional languages. See: https://wiki.haskell.org/Functional_programming

The Wikipedia quote is really vague.

Re: Javascript Arrays and Functional Programming

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

That's the GP's point though: it already is impure! If you rewrite as

    let acc = {...}
    footballPlayers.reduce(...,acc)
It should become clear: acc is an external variable that got mutated by the reduce.

Re: Javascript Arrays and Functional Programming

#18

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…

Wiki sums up the term well enough: "In computer science, functional programming is a programming paradigm — a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data." To that end map-reduce fits and can be referred to as 'functional programming'. The other things you mention are just great langu…

they can be referred to as techniques often found in functional programming, techniques that can obviously be used in non-functional manners.

Re: Javascript Arrays and Functional Programming

#19

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 co…

The future is already here -- it's just not very evenly distributed.

When I first started reading Hacker News, I had never heard of functional programming...or Python.

Re: Javascript Arrays and Functional Programming

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

Author here, thanks for your feedback ! I didn't mention sort because I thought I (personally) rarely use it and wanted to provide examples of more widely used array methods (namely filter reduce and map).
Post reply on HN