5 ms·
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 "funct
by seanwilson 9y ago
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.
- grabcocque 9y agoBecause (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.
- seanwilson 9y agoSounds 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.
- twii 9y agoAs mentioned in one of the comments, this is nowhere near the definition of functional programming. > It beats complex for-loops Nvm. I still prefer for loops in most cases, in some way it's easier to understand for me if I read it some months later. For example the reduce function I would write something like this: getPlayersByCountry= ( footballPlayers= [], players= {} ) -> for player in footballPlayers if players.hasOwnProperty player.country players[ player.country ].push player return players playersByCountry= getPlayersByCountry footballPlayers, {France: [], England: [], Spain: []} I guess it's mere a matter of taste.
- seanwilson 9y agoWhat I mean is when you have a 200 line for loop that includes other for loops, "continue" statements, if statements and modifying state it gets really confusing quickly. If it's short it's probably going to be OK to understand anyway but if you can decompose a long for loop into several map, reduces and filters it's much easy to understand what's going on because it's more linear. For that example, you should really have a generic "group by" function to use anyway that plays nice with your map, filter and reduce functions.
- louthy 9y agoEven with a small loop like that there are potential problems. Should there be an 'else'? You might know because you wrote it, but the compiler/runtime doesn't, and neither do other programmers on your team without taking time to analyse your intention. Using map/reduce/filter/etc. is declarative and less likely to result in spurious errors. That's the benefit of this declarative approach, it removes the relentless boilerplate, works solely with intent, and reduces cyclomatic complexity.
- deleted 9y ago[deleted]
- danidiaz 9y agoSometimes this style is called "origami programming".
- hacker_9 9y agoWiki 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.
- seanwilson 9y ago> 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 https://wiki.haskell.org/Functional_programming The Wikipedia quote is really vague.
- hacker_9 9y ago> 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.
- dahart 9y agoNone 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.
- pvg 9y agoBecause 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.
- mcphage 9y ago> 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.
- dawidloubser 9y agoWell... 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!
- mcphage 9y ago> Well... for some of us, when you say "functional programming", you say "Haskell". Haskell is a single, functional programming language. It is not the definition of what a functional programming language is. If you mean Haskell, say Haskell. But don't try and redefine "functional programming" to mean "this one specific functional programming language that I happen to like".
- dragonwriter 9y ago> 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 only fairly recently that the set of functional programming languages for which that is even approximately true became the center of FP attention. For quite a long time, the flagship family of “functional programming” languages was the Lisp family, members of which for the most part feature none of those except possibly idiomatic recursion over iteration (though many Lisps include constructs that work like iterative loops, even though they may be implemented under the covers with recursion.)
- brudgers 9y agoI 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?
- trevor-e 9y agoVery true, and it reminds me of the push to use "for each" statements in Java instead of looping with an index. Really it's just another "how to avoid for loops" movement.
- delhanty 9y ago>but I never understood why just using map/filter/reduce and avoiding state is enough for code to be called "functional programming" Maybe that's enough to count as first-order functional programming ? The Wikipedia article on Spreadsheet [1] seems to suggest that even a spreadsheet counts as first-order functional program: >The formula may rely on the value of other cells, but those cells are likewise restricted to user-entered data or formulas. There are no 'side effects' to calculating a formula: the only output is to display the calculated result inside its occupying cell. There is no natural mechanism for permanently modifying the contents of a cell unless the user manually modifies the cell's contents. In the context of programming languages, this yields a limited form of first-order functional programming.[2] [1] https://en.wikipedia.org/wiki/Spreadsheet https://en.wikipedia.org/wiki/Spreadsheet [2] https://www.cambridge.org/core/journals/journal-of-functional-programming/article/forms3-a-firstorder-visual-language-to-explore-the-boundaries-of-the-spreadsheet-paradigm/66F1ACF22742EA28DD52608A5122E70F https://www.cambridge.org/core/journals/journal-of-functiona...