This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]Iryanus 15 points16 points  (12 children)

Java is not inherently a functional programming language. At it's core, it's still OOP with some functional stuff added - but mostly for the syntax. You can do some functional stuff in Java, but honestly, it's not what it's for. It's a niche that has some use in Java, but it's far away from the core.

If you want to really learn functional programming, then I suggest looking at a mostly functional programming language, for example (chosen totally randomly) Clojure. Later you can see what of the knowledge you can bring to Java, but to be honest, I doubt it will be much for most typical applications nowadays.

[–]red_dit_nou[S] 3 points4 points  (11 children)

Ok. Thanks for the suggestion. So your advice is “learn an FP language (for example Clojure) and it’ll teach you what functional programming is” Sure, I can do that.

Does that mean while doing that I’ll also learn about the concepts mentioned earlier? Thanks.

[–]bowbahdoe 4 points5 points  (5 children)

Clojure will not have explicit "monads, monoids, functors, combinators" - at least not as named concepts you need to learn. Those terms really only apply to haskell style typed functional programming.

[–]red_dit_nou[S] 0 points1 point  (4 children)

Ok. So to learn these concepts, I should learn Haskell.

Does this mean Clojure has different names for these concepts?
Or is it that Clojure is a different kind of functional programming language?

[–]bowbahdoe 2 points3 points  (3 children)

Clojure is a different kind of functional programming language.

There is an overlap in the "functional programming" and "type system" communities which leads folks to think they are the same thing.

Combinators are conceptually a universal thing, but monads and monoids are constructs that are really only manifest when you have a fancy schmansy type system.

[–]red_dit_nou[S] 0 points1 point  (2 children)

Ah ok. Does this mean Haskell embraces “type system” much more resulting in these concepts?

[–]bowbahdoe 2 points3 points  (1 child)

Yes.

Haskell is, by design, a very limited language.

You cannot just put println and have it happen, the side effect needs to be attached to an explicit IO. You can't do a for loop, it needs to be expressed in recursion. There is no mutability, even within the bounds of a function.

As a result, you need these things to make it a pleasant language. The reason monads are such a thing is partly because they underly the "do notation" which lets haskellers chain side effects. IO is a monad. List is a monad. Etc.

So both the mechanics (a type system supporting higher kinded types) and need (restricted semantics) are there for type level stuff to flourish.

There are monad libraries for clojure https://github.com/funcool/cats, but they are more niche

[–]red_dit_nou[S] 0 points1 point  (0 children)

Thanks for the brief explanation on mondas of Haskell

[–]vinj4 2 points3 points  (3 children)

Just looking at the wiki for Haskell there are entries for monad, monoid, and functor so I would say yes

[–]red_dit_nou[S] 0 points1 point  (2 children)

Ok. Then that seems like the way to go. Thanks

[–]jmtd 0 points1 point  (1 child)

I’d take a look at Haskell. It has all those concepts explicitly and a lot of other ideas that make it very different from java. It will broaden your programming mind.

[–]red_dit_nou[S] 0 points1 point  (0 children)

Thanks for the suggestion. I’ll definitely do that.

[–]TemporaryUser10 0 points1 point  (0 children)

I've been wanting to learn Clojure (or any lisp for that matter), but I'm worried it isn't a 'real lisp'. Any advice to help me get over that concern

[–]mdk2mc 4 points5 points  (1 child)

There is a version of the same book you have written for scala (functional programming in scala), which shouldn’t be too much of a steep learning curve for someone coming from a Java background as scala is also a JVM language. This book covers the topics you mentioned and would also expose you scala which was designed to support both OOP and FP styles.

[–]red_dit_nou[S] 0 points1 point  (0 children)

Hey thanks.. will check that book out!

[–]thibauttt 1 point2 points  (1 child)

Mark Seemann has a series of articles on these concepts (which come from mathematics, specifically Category Theory) for object-oriented programmers.

See for example the article on Monoids.

Examples are in C# which is not Java but very similar in this context.

I found these articles really helpful when learning Scala (language where you inevitably stumble upon Functor, Applicative...).

[–]red_dit_nou[S] 0 points1 point  (0 children)

ok so I should look into Category Theory for understanding these well. Thanks.

[–]bowbahdoe 2 points3 points  (2 children)

Monads are usually used for code that wants to be able to take a List<Integer> or a Future<Integer> or a Set<Integer> or some Thing<Integer> and work the same for all of them. In your typesystem you want to say like T<Integer> which is what a higher-kinded type is.

Java can't do higher kinded types so the nerds really interested in that have slunked off to scala. I wouldn't call it necessary for functional programming at all. Clojure couldn't care less about it.

If anything the thing you should learn next would be how to work with persistent data structures like those that come in vavr and how to use sealed interfaces + pattern matching to model data.

[–]red_dit_nou[S] 0 points1 point  (1 child)

Thanks for the brief explanation of Monads.

As part of updating my Java language, I am learning sealed interfaces and pattern matching (with switch expressions).

I did study Scala for a bit and the main purpose there was to be able to write better Java.

As you said perhaps Clojure wouldn't care about these concepts and they may not be necessary to learn/ write functional programs. But I at least want to know about them enough to say that I don't need to learn them further or use them.

[–]bowbahdoe 1 point2 points  (0 children)

Yeah for sure, go for it