Live data from Hacker News

Javascript Arrays and Functional Programming

zabanaa.github.io

51–60 of 92 posts

Re: Javascript Arrays and Functional Programming

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

Reading your blog allowed me to better understand the reduce function for the first time and I want to thank you for that.

Re: Javascript Arrays and Functional Programming

#52

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…

And yet here it is, upvoted, clicked, and commented on by enough people to make it to the front page of a highly competitive news aggregator. It's easy to forget what it's like to not know something I guess, but clearly there are enough beginners still out there who find this type of post useful.

Re: Javascript Arrays and Functional Programming

#53

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…

What's the value of comments like this?

Re: Javascript Arrays and Functional Programming

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

shameless plug, here is my article about ES5 array methods that looks at them in depth : https://medium.com/@sAbakumoff/a-deeper-dive-into-javascript...

Re: Javascript Arrays and Functional Programming

#56
post #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…

Well... for some of us, when you say "functional programming", you say "Haskell". And it certainly had all of those features, and much more, more than 20 years ago.

I really like another poster's recommendation that it be called "combinator-oriented programming". Because, while ES, Java, and even Rust, supports rich combinator-oriented programming, I wouldn't dare call any of them functional programming languages.

Though, one has to admit, with ES6 now even supporting elegant currying, coupled with a good functional programming library like Ramda, and immutable data structures (Immutable JS), ECMAScript comes pretty close if you really want it to.

The difference is, any code anywhere can "drop out of" the functional world, whereas a functional language like Haskell enforces functional purity throughout.

Good times to be a software engineer, nevertheless!

Re: Javascript Arrays and Functional Programming

#57

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…

None of pattern matching, algebraic data types, strong typing, type inference and types that can't be null are specific to functional, nor do they make it any more functional. Purity isn't a language feature, it's a measure of how functional a language is, so it's tautological to say functional languages share purity. Currying is a byproduct of first class functions, not of functional programming, and currying is common in JavaScript and Python and other non-functional languages including C++. Forced recursion instead of loops would just be a byproduct of not having mutable state. And a good alternative to a recursive loop is a call to map() or reduce().

> The Wikipedia quote is really vague.

I think the Wikipedia quote is clearer and FAR more precise than a common feature set. Functional programming "treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data." That is the definition of functional, and it's the whole definition. No mutation, that's it. Including the features you wrote about is not functional programming, it's other things.

That's important to consider the precise definition if you're going to critique someone for talking about functional programming in a certain way. The author was pretty clear that this is functional programming concepts as applied to arrays in JavaScript, he never claimed that this post covers functional programming in general.

Map, filter & reduce are examples of functional programming, and if they were all you used, and you had only const and no var, then you might have a functional program. @zabana didn't claim that this is all you'll ever need.

Re: Javascript Arrays and Functional Programming

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

foreach implies mutation. reduce is a way to aggregate a collection into single result in an immutable way.

Re: Javascript Arrays and Functional Programming

#59

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…

Here's an example of why it's useful for me to see articles like this on HN. I'm an engineering manager, and someone on my team recently joined who wasn't very familiar with these more functional styles. I offered to explain to him benefits of declarative styles like this vs imperative, but he said he learned better by reading. He asked if I had any articles that I'd recommend about the topic, and since I've seen this, I'll send it his way.

Re: Javascript Arrays and Functional Programming

#60

Earlier quoted context omitted.

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.

Sometimes this style is called "origami programming".
Post reply on HN