News for hackers. Stuff that matters.
Re:Java’s Cultural Problem by p1necone on Monday September 19, 2022 @07:02AM (#32896717)
You can absolutely write fairly nice functional code in Java already with a decent Either type, streams etc.

I imagine it's not that widespread because most people probably switch to a different JVM language like Scala rather than going all in on functional Java. Your functional Java code is going to be just as unreadable to a standard Java dev anyway, so the appeal of it being a widespread language is somewhat lost.

Re:Java’s Cultural Problem by lf-non on Monday September 19, 2022 @09:46AM (#32898216)
Yes, also kotlin is pretty nice if you want to write oop/procedural code with some functional flavor.

- Lot of inline methods in stdlib alleviate the performance penalty of using higher order functions for tight loops.

- It is nice to have everything as expression

- Extension functions, it parameter, operator overloading, tuples are all micro-conveniences that coherently add up to a very pleasant experience.

I think the author's experience would have been substantially different if they used ktor/http4k/vertx with kotlin.

Re:Java’s Cultural Problem by grumpyprole on Monday September 19, 2022 @07:11AM (#32896785)
> You can absolutely write fairly nice functional code in Java already

You mean it has Lambdas? Yes that's a start, but there's a lot more it needs to claim good FP support. Programming with lambdas is not sufficient to do "functional programming", which is really about composing pure functions (everything else follows from this, e.g. immutable data). Java's roadmap does look promising though.

Re:Java’s Cultural Problem by oaiey on Monday September 19, 2022 @09:06AM (#32897778)
Pure Functions is indeed a factor, but not the only one. Other irrelevant factors are pattern matching, tail call recursion, recursion instead of loops, etc. FP is about treating data differently and dealing with side effects differently. Pure Functions are an implementation strategy of FP, not the core differentiator.

And that data/side effect difference, you can easily handle in Java, C# and other OO languages if you just overcome the culture of OO-only thinking. And despite I think the article is not perfect (who says, that a function cannot have a class/interface wrapper (aka strategy)), there is an element of truth in the spirit of the headline.

Re:Java’s Cultural Problem by grumpyprole on Monday September 19, 2022 @09:33AM (#32898080)
> Pure Functions are an implementation strategy of FP, not the core differentiator.

"Functional Programming" is not a well-defined term. But I would argue that if you are not using pure functions then you have more of a hybrid imperative/functional language, which is fine, especially if you are careful with how you structure your effects. Modern FP languages use monads (Haskell) and/or algebraic effects (Eff) to enable pure functions to describe effects. The pure function is what enables mathematical reasoning and safe function composition.

Re:Java’s Cultural Problem by kaba0 on Monday September 19, 2022 @11:27AM (#32899663)
What about the Lispian way of FP, e.g. Clojure? It is noting like the Haskellian one, yet they are also on the same spectrum.
Re:Java’s Cultural Problem by grumpyprole on Monday September 19, 2022 @01:42PM (#32901477)
Clojure encourages pure functions and immutable data, so definitely a "functional first" language. But it's not a "purely functional" language.
Re:Java’s Cultural Problem by kaba0 on Monday September 19, 2022 @02:09PM (#32901811)
I don’t think there is a boolean pureness flag, rather a spectrum. Haskell can also introduce arbitrary side effects in any code with some unsafe escape hatches, but sure, the common case doesn’t employ that, so it is a bit more pure than Clojure.
Re:Java’s Cultural Problem by Ma8ee on Monday September 19, 2022 @08:37AM (#32897515)
You get quite far with the "final" keyword.
Re:Java’s Cultural Problem by PaulHoule on Monday September 19, 2022 @08:52AM (#32897663)
Actually it’s the other way around. There are very good persistent collection libraries for Java but if you are going to ‘update’ then you can’t store them in a final field since you get a new collection object. On the other hand the only thing final about

   final List<Whatever> that
is the identity of the list.

I also think Either and Optional are cancers. Type erasure takes some of the fun away from Either. Before you had Optional you had one problem, you might have a null. With Optional you have two problems, you can get a null AND you can get an empty.

I worked at a place that used Scala and the engineering manager would talk your ear off like an Amway scam person about how much better monads were for error handling than Exceptions. I started working on non-Scala stuff but once they put me on the Scala side I found out that error handling was just missing in most places even though they allegedly did code reviews. It was definitely a frying pan to fire situation relative to Exceptions.

(The time before that whenI coded in Scala I found that people would spend 3 days writing something concurrent with Actors that only used 2 out of 8 cores and was rife with error conditions that could be done in 20 minutes with an Executor in Java.)

In a a parallel universe where LISP was the dominant language, people would be writing essays about how great COBOL and Cold Fusion and PHP are, about what a genius Noam Chomsky was, how recursive descent parsers are a force multiplier, etc.

Re:Java’s Cultural Problem by grumpyprole on Monday September 19, 2022 @09:25AM (#32897992)
> With Optional you have two problems, you can get a null AND you can get an empty.

That's not really the fault of the Optional. That's Java adding null as value to every type. Java also allows lists of nulls, mappings to null etc. An optional is just a container that stores one item at most. I do agree that it makes optionals less useful in Java. In Haskell and OCaml they are used essentially instead of null.

Re:Java’s Cultural Problem by ryanianian on Monday September 19, 2022 @10:34AM (#32898889)
Static analysis can catch a `null` being used in a place that wants an Optional. With Optional being fully pervasive in a codebase the `null` concept could effectively be eliminated.
Re:Java’s Cultural Problem by PaulHoule on Monday September 19, 2022 @09:57AM (#32898360)
I'd grant that. I'd say the authentic-to-Java approach to the problem is to static import something like a "map" function

   var result = map(x,x=>x.y)
which is equivalent to

   var result = isNull(x) ? null : x.y
where isNull is static imported from Objects.

An approach which is authentic to some other languages is to consistently used a list with 0 or 1 members instead of optionals.

Re:Java’s Cultural Problem by switchbak on Monday September 19, 2022 @10:34AM (#32898884)
"The time before that whenI coded in Scala I found that people would spend 3 days writing something concurrent with Actors that only used 2 out of 8 cores and was rife with error conditions that could be done in 20 minutes with an Executor in Java"

Sounds like you were working with some folks that were just learning the language/idioms (which is admittedly a big hurdle).

I haven't used actors in a very long time. Simple future composition and reactive streams get you very far, at an absolute fraction of the code size and complexity of the equivalent vanilla Java approach (ie: not using Rx).

Re:Java’s Cultural Problem by Skgqie1 on Monday September 19, 2022 @11:36AM (#32899790)
That sounds very close to a "No True Scotsman" type fallacy. I've done a fair bit of consulting in the past working with Scala, and my experience closely matches the one described in the grandparent post - the issues happened with veterans more than newbies in my experience (Although that's a function of mostly working with veterans and few newbies)
Re:Java’s Cultural Problem by switchbak on Tuesday September 20, 2022 @12:23AM (#32907635)
I have seen junior and senior devs (and myself sometimes) struggle with fancier asynchronous trickery. That's true, but I think it's shortsighted to write off that effort as compared to "just using executors" (whatever that means exactly).

For me there was a lot of learning (that often just felt like grinding), and I can confidently say that I'm leagues more productive than trying to mimic this with low level vanilla Java primitives.

I'm sure this would also apply with go channels, async/await, etc. Raising the bar of abstraction in this case can be valuable. As compared to something really high leverage like reactive streams it's just going to take a ton of code and be far less flexible in a naive approach (in most cases).

I've seen this same pattern with others. Is it worth the investment of time to get there? Not sure, maybe Go is a better set of tradeoffs for most, but I wouldn't go back to straight executors, locks, and the rest of the Java 1.5 concurrency offerings (which is what I thought the OP was saying).

Re:Java’s Cultural Problem by SamoyedFurFluff on Monday September 19, 2022 @08:48AM (#32897628)
> Your functional Java code is going to be just as unreadable to a standard Java dev anyway

I’m largely unsure of this. I think the standard Java dev might have difficulty adapting but the ramp up isn’t too hard. A Decent Java shop right now should be adapting functional features from Java into their code already.

Re:Java’s Cultural Problem by switchbak on Monday September 19, 2022 @10:26AM (#32898773)
I've had a good deal of difficulty getting enterprise Java devs on board with functional Java approaches.

In many ways introducing Kotlin was easier because it was clear it was a new language, and they had more openness and excitement for learning a new language.