Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Real World Functional Programming in Scala

Soooo...

Semigroups, Monoids, Monads, Functors, Lenses, Catamorphisms, Anamorphisms, Arrows... These all sound good, and after an exercise or two (or ten), you can grasp their essence. And with Scalaz, you get them for free...

However, in terms of real-world programming, I find myself struggling to find usages to these notions. Yes, of course I always find someone on the web using Monads for IO or Lenses in Scala, but... still...

What I am trying to find is something along the "prescriptive" lines of a pattern. Something like: "here, you are trying to solves this, and one good way to solve it is by using lenses this way!"

Suggestions?


Update: Something along these lines, with a book or two, would be great (thanks Paul): Examples of GoF Design Patterns in Java's core libraries

like image 621
Hugo Sereno Ferreira Avatar asked Dec 11 '11 02:12

Hugo Sereno Ferreira


People also ask

Is Scala good for functional programming?

Scala is a language that features full support of Functional Programming as well as the Object-Oriented Paradigm. It is one of the most widely utilized languages by the Functional community, up to par with F#, and Haskell, Clojure, among others.

Which Scala library is used for functional programming?

Currently, the cats library and its ecosystem is the cornerstone of functional programming in Scala. For many, they will be the first choice when choosing stack for the new functional project.

Is Scala really functional?

Scala is not a functional programming language. It is a statically typed object oriented language with closures.

Is Scala popularity declining?

Scala's popularity, on the other hand, is starting to decline, driven by long compile times, non-essential complexity in the language, binary and source compatibility problems, the looming threat of obsolesce that is Dotty, brain drain to the Rust and Haskell communities, and fractures in the Scala community that are ...


1 Answers

The key to functional programming is abstraction, and composability of abstractions. Monads, Arrows, Lenses, these are all abstractions which have proven themselves useful, mostly because they are composable. You've asked for a "prescriptive" answer, but I'm going to say no. Perhaps you're not convinced that functional programming matters?

I'm sure plenty of people on StackOverflow would be more than happy to try and help you solve a specific problem the FP way. Have a list of stuff and you want to traverse the list and build up some result? Use a fold. Want to parse XML? hxt uses arrows for that. And monads? Well, tons of data types turn out to be Monads, so learn about them and you'll discover a wealth of ways you can manipulate these data types. But its kind of hard to just pull examples out of thin air and say "lenses are the Right Way to do this", "monoids are the best way to do that", etc. How would you explain to a newbie what the use of a for loop is? If you want to [blank], then use a for loop [in this way]. It's so general; there are tons of ways to use a for loop. The same goes for these FP abstractions.

If you have many years of OOP experience, then don't forget you were once a newbie at OOP. It takes time to learn the FP way, and even more time to unlearn some OOP tendencies. Give it time and you will find plenty of uses for a Functional approach.

like image 132
Dan Burton Avatar answered Oct 16 '22 16:10

Dan Burton