Achieving Functional Programming in Java
https://youtu.be/VUH_HhAaNpc?list=PLEx5khR4g7PLIxNHQ5Ze0Mz6sAXA8vSPE[removed] — view removed post
16
u/jonhanson Aug 06 '19 edited Jul 24 '23
Comment removed after Reddit and Spec elected to destroy Reddit.
1
u/shponglespore Aug 07 '19
I believe polymorphic functions in Haskell are implemented with witness objects, or at least they could be; every type constraint in the function's signature corresponds to a hidden patented parameter used to pass the witness object. I believe you can do the same thing in Scala by passing the witness(es) as implicit parameters, but you do still have to create them manually. (I may be wrong about that part; it's been a long time since I used Scala.)
It doesn't seem too terrible to do it manually in the case where there's only one witness object, but I can see how it would quickly become tedious and error-prone when there are a lot of them and different functions need different witness parameters. My experience with using type classes in Haskell leads me to believe that would be rather common in practice, but OTOH Java's subtype polymorphism covers a lot of the use cases where Haskell relies on type classes.
Sometimes I wonder if things like applicative style are just an excessively magical gimmick for solving a class of problems that would be better solved with good macro system. Lisp languages seem to do just fine without anything comparable to type classes.
1
u/bjpbakker Aug 07 '19
Lisps (and also many other languages) lock the implementation of common functions (eg map) to specific data structures. With macros they tend to work around that a bit but to me that almost always feels very clumsy.
A good example of that are Clojure’s core/async macros. Simple generic type classes would make for a much simpler and extensible implementation.
1
u/TheStrangeDarkOne Aug 07 '19
This is just wrong - pure should be a static function, which clearly isn't possible in an interface
I'm not sure I understand your problem, but...
public interface Foo { static <T> T identity(T item) { return item; } }... compiles without a problem.
2
u/jonhanson Aug 07 '19 edited Mar 07 '25
chronophobia ephemeral lysergic metempsychosis peremptory quantifiable retributive zenith
1
0
Aug 06 '19
[removed] — view removed comment
2
u/___catalyst___ Aug 07 '19
Does anyone know when Value Types will become official? What JDK?
2
u/lbkulinski Aug 07 '19 edited Aug 07 '19
As the architects say, “when they’re ready”. The talk here gives some updates. You can also keep up with the progress by subscribing to the valhalla-spec-observers mailing list.
3
u/BoyRobot777 Aug 07 '19
You gave a link to amber specs, which Valhala is not part of. This is a correct link if you want to reach Valhalla
2
u/lbkulinski Aug 07 '19 edited Aug 07 '19
Yes! Thanks for catching that! I had sent the Amber mailing list link to someone earlier and mindlessly copied and pasted.
13
u/mto96 Aug 06 '19
This is a 45 minute talk from GOTO Chicago 2019 by John Napier, who works with developers on software fundamentals for DRW. I've pasted the talk abstract below for a read below:
For years, the conventional wisdom around attempting pure functional programming in Java has been "don't", and for plenty of good reasons: lambdas weren't supported until the eighth major release of the language specification, creating value types is a verbose, ceremony-riddled exercise devoid of any first-class syntax, and neither are the majority of the data structures shipped in the JDK immutable, nor are interfaces a proper substitute in general for type classes.
Still, it's a shame for developers to have to seemingly choose between the myriad benefits of functional programming and the maturity of the java compiler, the investments made into the ecosystem by major organizations, and the ease of hireability especially when some of these short-comings can be addressed externally.
In this talk I'll present Lambda, a Java library that offers:
A first-class model for functors, applicative functors, monads, coproducts, and more algebraic and category theoretic structures
Lazy evaluation
Profunctor optics
Semigroups and Monoids
Type-safe heterogeneous structures like HLists and HMaps
and more...
The goal of this talk is to present this library as a viable option for experienced Java development teams that wish they could use some of the functional programming capabilities available in languages like Haskell?, and to show how the trading infrastructure teams of an algorithmic-trading firm in Chicago use Lambda to write correct-by-construction software and accelerate their development process.
Who should attend this talk: Experienced Java developers who want to program using algebraic data types, compositional patterns like functors and monads, lazy evaluation strategies, and a strong emphasis on type-level transformations, but are daunted by Java's current limitations in these areas
Academic level: Advanced to Expert
What is the take away in this talk: While Java has a long way to go, libraries like Lambda enable developers who want to push their Java programming much further towards functional idioms that previously seemed unachievable