6 ms·
The article isn't about functional programming. Functional programming is programming with pure (mathematical) functions. As a consequence, functional programm
by bad_user 10y ago
The article isn't about functional programming.
Functional programming is programming with pure (mathematical) functions. As a consequence, functional programming is also programming with (immutable) values (i.e. persistent data-structures, etc).
This isn't pedantry. The whole point of FP is being able to have your code be "referentially transparent", a property that gives you "equational reasoning", making your code more composable, more testable and easier to understand because you can take any function, divorce it from its surrounding context and still make some sense of it. Of course, there's no silver bullet, yada yada, but take those traits away and the definition loses meaning and usefulness almost completely.
And btw I shouldn't need to specify "pure" or "mathematical" if programming wouldn't have overloaded the meaning of the word "function". We could have stuck with "procedure" or "routine" or "sub-routine". As in jumps in code that push and pop the call stack and that have side-effects. And if you want to express the act of programming using side-effectful functions, there's also a perfectly adequate term for it already: "procedural programming".
Yes, it's cool having closures and anonymous functions in your language. It's sort of a prerequisite if you want your FP code to be comfortable. But that's not FP. There's actually nothing resembling FP about the code samples in the article. And the implementation of AccountConverterImpl would make any programmer that likes FP to cringe.
On Java 8: yes, it's becoming possible to do FP, sort of anyway. But it's still painful as hell, with the environment actively working against you. First of all there aren't many libraries that do FP in Java, I only know of functionaljava.org. So you're swimming against the tide, with the standard library itself being actively hostile to FP. And you can start from the fact that Java 8 was released in 2014 and even its new Option type is breaking the Functor laws, because map(x).map(y) != map(x.andThen(y)) and lets not even mention the word monad because it scares people.
But I guess that if you're forced to use Java, well, then you can find some ways to cope with it (especially if you aren't scared of building your own stuff).
- jerven 10y agoSo Lisp is not a functional language...
- catnaroek 10y agoMost things in Lisp have an object identity that can be directly manipulated by programmers, so it is indeed not a functional language.
- baldfat 10y agoTechnically you are defining a purely functional language and not functional programming. John McCarthy might disagree with you if he was still with us saying LISP is not a functional language. Here is a great summary of his "Recursive Functions of Symbolic Expressions and Their Computation by Machine" https://swizec.com/blog/the-birth-of-lisp-a-summary-of-john-mccarthys-original-paper/swizec/5075 https://swizec.com/blog/the-birth-of-lisp-a-summary-of-john-... I really think we need a definition of functional programing language. I feel that many just see Haskell and other "pure" functional languages as the only functional programming language when I really don't see that is the point (Also Haskell isn't very pure looking at Monads) Personal Definition. If you can program in a functional manner and it looks and feel good then it is. R - More then just this reason but yes JavaScript - Sort of but I would say no if my arm was twisted behind my arm Racket / LISP - Absolutely Python - NO WAY First Class Functions alone don't make a functional language
- catnaroek 10y ago> Technically you are defining a purely functional language and not functional programming. No, I'm not defining a purely functional language. ML-family languages (Standard ML, OCaml) aren't purely functional, but they have arbitrary compound values, the physical identity of whose in-memory representation is inaccessible - not a part of the abstraction the programmer is shown. And, FWIW, I only call Haskell “almost purely functional”, because divergence is a kind of effect that its type system doesn't track. In any case, a function is a mapping from values to values, so a decent treatment of a rich collection of values (including compound values) is a prerequisite for a language to be considered “functional”. > I really think we need a definition of functional programing language. Here's a very simple definition: A functional language is a value-oriented language with an abstract type of functions, which can only be distinguished from one another up to extensional equality. > (Also Haskell isn't very pure looking at Monads) Monads (resp. monad transformers) can be used to embed the abstract syntax of an effectful language into a pure (resp. less effectful) language. That isn't news to anyone. > Personal Definition. If you can program in a functional manner and it looks and feel good then it is. I have no idea what you mean by “program in a functional manner”, but, when I do functional programming, I expect to be able to formally reason about my programs without reaching for Hoare logic, which is an insanely complex beast.
- catnaroek 10y ago> As a consequence, functional programming is also programming with (immutable) values I'll go even further: Values don't exist in time (when did the number 2 suddenly come into existence?), so it doesn't even make sense to ask whether they're “mutable” or “immutable”, which is intrinsically about how objects evolve in time. Values exist in the semantics of the programming language in question (a timeless mathematical object!), and a structural operational semantics makes this very clear: a value is a term that a variable can be substituted with. Ergo: (0) In a call-by-value language, a value is the result of a successful computation. (1) In a call-by-name language, a value is a snapshot of a potentially unfinished computation. (2) In a call-by-need language, a value is the identity of a potentially unfinished computation. (3) In an object-oriented language, the only values are object identities, and the few lucky primitives that escaped being treated as objects. > (i.e. persistent data-structures, etc). You can have ephemeral data structures in a purely functional program too, provided the language has substructural types.
- bad_user 10y agoYou're right, which is why I named "immutable" in parens, to go along with "mathematical" for functions. Values are just values, but because they are uncommon in mainstream languages, except for primitives and strings maybe, I personally feel the need to mention immutability in order to be precise and not have people confuse them with objects that have identity.
- marktangotango 10y agoMy personal experience has been that Java 8 lambda plus deep class hierarchies can lead to some truly impenetrable code (to read that is). In practice mixing oop and the meager functional tools (as you note) in Java can be really, really bad, and makes me weep at the future of Java development. I think teams need to decide what kind of code they're going to write, either 'functional' or oo. And if you are using Java 8 lambda's (and googles immutable collections for example) teams should strive to keep class/interface hierarhies shallow. I've been doing enterprise java development for 15 years, and the code I'm seeing now is kinda the nail in the coffin for Java for me. I don't want write or maintain that stuff, the next time I switch jobs I'll be looking at a pay cut, and python/django most likely. Or going into management.
- bad_user 10y agoThe problem with working with closures is that stack-traces tend to become very hard to read, close to unusable sometime. Add asynchrony/concurrency to the mix and you've got a wonderful cocktail :-) Partial blame is on Sun/Oracle for not adding mutual tail-recursive calls optimizations to the JVM ;-) This is one of the challenges of FP. It makes debugging harder. But then FP code tends to be tested by means of laws described with QuickCheck, a superb automated testing tool that has ports to Scala, Ocaml, Clojure (that I know of) and it raises the quality of the code a lot.
- l_dopa 10y agoThis is a really annoying meme in "pop" FP. Equational reasoning isn't limited to "pure" languages. Effectful functions, as you probably are aware, are just functions of type A -> T B.
- bad_user 10y agoI haven't claimed that equational reasoning is limited to pure languages, that's more a property of the code, rather than the language. But you can only achieve equational reasoning with referential transparency and that means the code itself needs to be pure. TFA does not show pure code. > Effectful functions, as you probably are aware, are just functions of type A -> T B Not sure how to parse that. But if T in your example is meant to be some IO type, then it is pure along with any transformations on it, until you do unsafePerformIO or something equivalent and you only need to do that once, at the end of the program.
- l_dopa 10y ago> I haven't claimed that equational reasoning is limited to pure languages Sorry, I may have read that into your comment. > the code itself needs to be pure But that's also incorrect. Just because you can't replace an effectful computation with a value doesn't mean you can't reason equationally. You can also have benign effects that are unobservable, for example, memoization. > Not sure how to parse that I meant that functions with effects are just functions with computation type at the meta level. I.e. there's nothing more "mathematical" about only allowing nontermination in your language. It's just the simplest case.
- aikah 10y ago> The article isn't about functional programming. functional programming =/= functional purity. You're talking about functional purity,not functional programming.