Tag: functional programming

What Happens in Monads Stays in Monads

There are moments in life when you have to take a stance. You know countless people already tried and possibly succeeded in doing something, but you feel the urge to try it yourself.

So yesterday I attended the Milan C++ Meetup by Marco Arena, presented in a very entertaining and well-organized way, what’s new in C++23. Everything was fine until Marco presented the std::expected template and its “monadic operations”. Now it was in the context of a much wider presentation and there was no time to go into details, but I got the impression that the C++ community has a bit of an ad-hoc approach to monads. I mean C++98 failed to recognize that containers are monads, C++11 failed to recognize that std::future is a monad, C++17 failed to recognize that std::optional is a monad, and C++20 failed to recognize that coroutines are monads. You can see a pattern there.

Continue reading “What Happens in Monads Stays in Monads”

Scala Italy 2023 – Scala Blues

It was 2015 when I attended my first Scala Italy conference. I was freshly landed in the functional programming magic world and I was tasked by my boss to asses whether Scala was a technology with a future or just a fade. I sat there, as proved by the picture used over the years as a background for the conference website, and enjoyed the show.

The conference was well organized, with a bunch of sponsors and some hundred people attending. My report, reinforced by attending Scala Days 2016 in Berlin, was that Scala was viable with quite a strong interest and support from the industry.

Over the years the conference grew, I missed the Venice edition in 2016, but I was in Rome in 2017, Florence in 2018, and eventually Bologna in 2019. Both Florence and Bologna were two-day events.

Continue reading “Scala Italy 2023 – Scala Blues”

Unfrightening Catamorphism and Anamorphism

TIL what Catamorphism and Anamorphism, and about a new useful function. Functional programmers are not shy to use mathematical terms that could frighten the casual programmer listening. Functor, monoid, applicative just to name the first that comes to mind. Usually, they turn out to be relatively simple concepts (which get combined together until they are no longer simple).

Continue reading “Unfrightening Catamorphism and Anamorphism”

Lambda Headache for Mere Mortals

A few years ago I attended a talk at a Lambda World Conference about Lambda Calculus. Although not an eye-opener (in fact that level of abstraction is rarely needed, nor advisable, in everyday programming), it was thought-provoking. By wisely crafting mathematical functions you could describe algorithms, fully equivalent to the good old recipe-like imperative programming code.

The point is that those lambda functions are really twisted.

Reading some anecdotes about Alonzo Church it is immediately clear he was quite a guy. And devising lambda calculus required quite a mind.

Since lambda calculus is just functions, no statement, it came to my mind I could use it to devise a solution to my “if-less” programming quiz.

The solution I prepared was too complex to be explained in my previous post, so I decided to write this post.

Continue reading “Lambda Headache for Mere Mortals”

Dealing with Errors in C++ Using a Lightweight Monadic Approach

Managing errors and failures in every programming language is usually a pain. Most programming book authors just show the happy path scenario, sometimes noting down that error handling has to be done, but it has been left out for improving simplicity (and readability).

C++ offers the exception mechanism, which is a clever way to leave the happy path in sight and hide the troubles under the carpet. Even before questioning if this is a good idea or not, C++ abstraction is so delicate that you need to take particular care in making your code exception-safe. Meaning that in case of exception, your program does not leak resources and leaves everything in a useful state so that the exception can indeed be recovered from.

Continue reading “Dealing with Errors in C++ Using a Lightweight Monadic Approach”

What’s wrong with you, std::optional?

“He Who Laughs Last Is At 300 Baud”, is possibly a long-forgotten joke, but sometimes C++ standard is like using a 300 baud modem, discovering “innovations” tens of years after other less committee-centric languages discover and apply them.

Let’s take the std::optional which tries to mimic the Option monad available in other languages. Since 1990 there has been a resurgence of functional programming languages in the mainstream – Haskell (1990), and Scala (2004) just to name two that have Option since their first version.

Continue reading “What’s wrong with you, std::optional?”

As Smart as a Smart Type – Practice (C++)

In theory, practice and theory are the same, in practice they are not. So, after having read how brilliant and smart smart-types are, it is now time to have a closer look at the compiler and figure out what C++ can offer.

After my last post, I found that Smart Types are also known as Refined (or refinement) Types. And here is a notable implementation for Scala.

Simple things first, if you need a type with a bunch of possible values, don’t use int & #defines, don’t use bool either (please), use enum, or, even better enum class.

Now that we’ve done with the trivialities, let’s proceed to something more challenging – numeric types. Ideally, we want some template code that wraps the numeric type and saves us the boredom of writing all the usual +, -, *, /, ==, !=, <… operators, while letting us define the rules of the existence of the represented type.

Continue reading “As Smart as a Smart Type – Practice (C++)”

As Smart as a Smart Type – Theory

Recently I listened to a “Happy Path Programming” podcast episode about Smart Types. And that inspired me for this double post. The first part (this one) is about what a smart type is and why you should employ smart types in your code. The second part (yet to come, hopefully soon) is about the troublesome way I implemented an arithmetic smart type template in C++.

Continue reading “As Smart as a Smart Type – Theory”

Scala Job Interview – FP questions

Welcome to the third installment of the Scala Job Interview Questions series. This time I’ll try to answer functional programming questions, likely my score will be a bit less than the first two editions (General Questions and Language Questions) because I like Functional Programming, but I’m still a traditional programmer (imperial?) who studied Algebra at high school and uni and then consider Algebra as useful (for programmers) as a doorstop in a tent.

Young and foolish I was, but who could imagine, back then that to run the dance of the bits I would ever need monoids?

Let’s not waste other time in void introduction, and start with the questions and my answers.

Continue reading “Scala Job Interview – FP questions”