News for hackers. Stuff that matters.
Re:Advantages of Functional Programming in Java 8 by bad_user on Tuesday September 06, 2016 @07:41AM (#12435234)
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).

Re:Advantages of Functional Programming in Java 8 by marktangotango on Tuesday September 06, 2016 @08:24AM (#12435418)
My 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.

Re:Advantages of Functional Programming in Java 8 by bad_user on Tuesday September 06, 2016 @12:38PM (#12437255)
The 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.

Re:Advantages of Functional Programming in Java 8 by catnaroek on Tuesday September 06, 2016 @08:19AM (#12435391)
> 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.

Re:Advantages of Functional Programming in Java 8 by bad_user on Tuesday September 06, 2016 @12:30PM (#12437197)
You'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.
Re:Advantages of Functional Programming in Java 8 by l_dopa on Tuesday September 06, 2016 @09:50AM (#12435960)
This 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.
Re:Advantages of Functional Programming in Java 8 by bad_user on Tuesday September 06, 2016 @12:07PM (#12437003)
I 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.

Re:Advantages of Functional Programming in Java 8 by l_dopa on Tuesday September 06, 2016 @01:09PM (#12437494)
> 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.

Re:Advantages of Functional Programming in Java 8 by jerven on Tuesday September 06, 2016 @08:15AM (#12435371)
So Lisp is not a functional language...
Re:Advantages of Functional Programming in Java 8 by bad_user on Tuesday September 06, 2016 @12:17PM (#12437076)
Technically speaking, Common Lisp isn't a functional language, yes. Common Lisp is a multi paradigm language and you can do FP with it if you want. But from my very limited experience with it, it's not the norm.

However Lisp, the original, is based on the lambda calculus and that is very much about functional programming. So surely you can do FP with most Lisps, but whether that's the norm or not is another question.

Re:Advantages of Functional Programming in Java 8 by baldfat on Tuesday September 06, 2016 @08:21AM (#12435401)
So R isn't a functional language...

To be fair the vast majority of people would say that it isn't BUT hidden inside is a very Scheme inspired system that can make pretty functional programs.

http://link.springer.com/chapter/10.1007%2F978-3-642-40447-4...

Re:Advantages of Functional Programming in Java 8 by catnaroek on Tuesday September 06, 2016 @09:00AM (#12435626)
Scheme isn't a very good functional programming language either. Even cons cells are mutable objects with identity: just use eq? everywhere instead of equal?, and use set-car! and set-cdr! instead of creating new cons cells, and you're back to JavaScript.

---

Sorry, can't reply properly, I'm “submitting too fast”.

<ignore>I understand neither the form nor the content of your objection. Is Scheme a programming language? Yes. What does this have to do with whether it supports functional programming reasonably well?</ignore>

Scheme is a higher-order programming language: procedures are first-class objects whose identities can be bound to variables just like any other value. I'm not sure about calling it “functional”, though: the type of procedures isn't fully abstract, because you can query the physical distinction between two procedures that have the same behavior when called. In other words, in Scheme, procedures are objects, but they are not values.

Re:Advantages of Functional Programming in Java 8 by baldfat on Tuesday September 06, 2016 @09:23AM (#12435787)
Scheme is a functional programming language yes?

Sorry but I am an old timer and seeing people blast the paths that led us to where we are is very frustrating to me.

Edit added is a functional

Re:Advantages of Functional Programming in Java 8 by catnaroek on Tuesday September 06, 2016 @08:21AM (#12435398)
Most things in Lisp have an object identity that can be directly manipulated by programmers, so it is indeed not a functional language.
Re:Advantages of Functional Programming in Java 8 by baldfat on Tuesday September 06, 2016 @08:34AM (#12435473)
Technically 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-...

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

Re:Advantages of Functional Programming in Java 8 by catnaroek on Tuesday September 06, 2016 @08:35AM (#12435480)
> 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.

Re:Advantages of Functional Programming in Java 8 by bad_user on Tuesday September 06, 2016 @12:24PM (#12437148)
> 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)

I consider FP languages to be those that actively encourage an FP style. We live in a pragmatic world with limits, many times we need to work around those limits and working with totally pure code where side effects are modeled by means of monads is sometimes limiting. However, in FP languages the FP style should be considered the default and breaking out of that style should be done for optimization reasons.

The set of features required is not that big. Of course, for static languages, it would be nice if the language had some form of type classes and higher-kinded types, because that goes well with parametric polymorphism, which blends well with FP. But actually, you can't name a certain feature, other than having first class functions maybe. But the community and the available libraries need to encourage a preponderantly FP style.

I can't say exactly what languages are like this, but I can say that Java does not and it never will.

Re:Advantages of Functional Programming in Java 8 by catnaroek on Wednesday September 07, 2016 @08:12AM (#12442662)
Higher-kinded types aren't an absolute necessity, and, in fact, they force you to pick between modularity (abstract types) and type inference: https://news.ycombinator.com/item?id=12331926 . Not a very pleasant choice, a programmer normally wants both.
Re:Advantages of Functional Programming in Java 8 by bad_user on Wednesday September 07, 2016 @02:06PM (#12445616)
Yes, I already said they aren't a necessity, though many people like having higher-kinded types. But then ML/Ocaml folks seem to be doing fine without. I personally prefer having HKT because you can then neatly express well known type-classes for implementing functors, applicatives, monads and many others.
Re:Advantages of Functional Programming in Java 8 by catnaroek on Wednesday September 07, 2016 @02:12PM (#12445660)
You can recover the power of higher-kinded types with higher-order functors (in the ML sense), without sacrificing the compatibility of abstract types and type inference in the core language.

On the other hand, Haskell's type inference algorithm is simply incompatible with abstract types.

Re:Advantages of Functional Programming in Java 8 by tome on Thursday September 08, 2016 @03:21AM (#12450932)
I didn't fully understand your explanation last time. Can you give an example of the conflict between modularity and type inference with HKTs in Haskell?
Re:Advantages of Functional Programming in Java 8 by baldfat on Wednesday September 07, 2016 @07:49AM (#12442508)
I can say that surpisingly R (The fastest growing language with a TON of hate for its warts) is the closest I have seen for your description. Though it really is not a general purpose language.

The closest general purpose language I have been exposed to that fits your description is Racket. Based off of Scheme and has the ability to really make your own language from it, but also has pragmatic pieces to get things done.

Love your definition: However, in FP languages the FP style should be considered the default and breaking out of that style should be done for optimization reasons.

Re:Advantages of Functional Programming in Java 8 by willtim on Wednesday September 07, 2016 @06:15AM (#12442060)
Programming with Monads, e.g. the IO Monad, is pure functional programming. It is a common misconception that it isn't. You have full referential transparency and sound composition, since no effects are actually performed until the computation is run/interpreted.
Re:Advantages of Functional Programming in Java 8 by aikah on Tuesday September 06, 2016 @11:45AM (#12436831)
> The article isn't about functional programming.

functional programming =/= functional purity. You're talking about functional purity,not functional programming.

Re:Advantages of Functional Programming in Java 8 by bad_user on Tuesday September 06, 2016 @12:01PM (#12436958)
No, I'm talking functional programming: https://en.wikipedia.org/wiki/Functional_programming
Re:Advantages of Functional Programming in Java 8 by aikah on Tuesday September 06, 2016 @01:54PM (#12437878)
fair enough.