Live data from Hacker News

Javascript Arrays and Functional Programming

zabanaa.github.io

21–30 of 92 posts

Re: Javascript Arrays and Functional Programming

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

[deleted]

Re: Javascript Arrays and Functional Programming

#22

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 they are very straightforward and practical examples of functions as first class objects and higher-order functions, which are more fundamental to any definition of 'functional programming' than things like type inference or pattern matching.

Re: Javascript Arrays and Functional Programming

#23

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…

Man, I had just worked up the nerve to start a personal dev blog but you've just scared me straight off.

Re: Javascript Arrays and Functional Programming

#24

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…

> why just using map/filter/reduce and avoiding state is enough for code to be called "functional programming"

A better term would be "programming with functions as first class citizens", but many years ago that got shorted to "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.

That's a list of what many functional languages now contain, although most don't contain all of those. But that isn't the list of "common functional programming language features" from 10 years ago, and isn't necessarily what the list will be in 10 more years, either.

Re: Javascript Arrays and Functional Programming

#25

Earlier quoted context omitted.

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

> and expected of functional languages

Clojure uses dynamic typing, yet it is still considered a functional language. I agree though that the combination of features results in a better experience when using functional primitives, but not having them doesn't mean this isn't functional programming still.

Re: Javascript Arrays and Functional Programming

#26

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…

I'm sorry to say that I completely agree. As a "here's how you do X in javascript" this is a great post. However, the conclusion clearly shows that this post was not aimed at people who already know how to use map and reduce: "Functional programming is a great tool and will drastically improve your productivity...."

It's like someone showing you multiplication and saying. See look how useful it is! See look how simple and easy it is to understand! I still don't know multiplication, or even addition for that matter.

Map and reduce are things you build up to by doing recursive functions over and over again. They are not something you learn from reading an article. Just like you can't read an article and suddenly know how to solve problems with long division.

Re: Javascript Arrays and Functional Programming

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

The good old question about sacrificing performance for readability. Anyway these days filter is not so bad.

[1] https://jsperf.com/array-uniq-filter

Re: Javascript Arrays and Functional Programming

#28
Author here: I wrote this article mainly as a note to self that I could refer to in the future and as an exercise to help me learn the concept and force me to structure my thoughts into a coherent blog post. I thought I'd share it for those of us who are more intermediate and would like to pick up a few tricks along the way. Thank you all for the positive (and not so positive feedback)

Re: Javascript Arrays and Functional Programming

#29

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…

Man, I had just worked up the nerve to start a personal dev blog but you've just scared me straight off.

Dont let idiots scare you away. Go ahead and do it. If you meed help ping me (check profile).

Re: Javascript Arrays and Functional Programming

#30

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…

Man, I had just worked up the nerve to start a personal dev blog but you've just scared me straight off.

You should do it, even if it's just for your own benefit!

(It's unlikely it'll benefit arkadiytehgraet, but he probably finds that with a great many things.)

Post reply on HN