A small monad library for dart/flutter project 🎯

Common practice : example 1

  1. In getUser function, we are not precise about kind of error that happens when we try to fetch user. Simple null is return
  2. We hold a detail about possible null instance and add a guard
if(user!=null){ ... }

Common practice : example 2

  1. We are not precise about kind of error that happens when we try to fetch user
  2. We must read the content of getUser function body to know where exceptions are thrown. In case of important project maintained by many developers, these details can be difficult to follow

A better solution

  1. We are precise about kind of error that happens when we try to fetch user
  2. We don’t need to read the getUser function body to understand its behavior
  3. All informations about the getUser function are resume in the type signature
  4. We are safe from runtime error because we don’t use null and don’t throw any exception

Why Monad?

Initially, a monad is an abstraction which come from the functional programming paradigm. This abstraction encapsulate data and allows easy composition of functions applied to those data. In practice we use it for error handling, concurrency management, IO composition and others in a totally safe way.

Examples of monads

Option<A>

Option represents a value of one of two possible types. By convention we consider missing value as an instance of None and expected success value in an instance of Some.

Either<L,R>

Either represents a value of one of two possible types. By convention we put missing or error value in an instance of Left and expected success value in an instance of Right. It’s like an Option, but with a more general and specific on kind of error.

What is a Monad?

Basically, a monad is a generic class, simple container (or wrapper type) where we can put an element into and satisfies some properties.

  • a type constructor: from a type A return the monadic type M[A]
final Option<int> value = Option.of(10); // Some(10)
  • a map method: from a monadic type M[A] transform A to an another type B and return the monadic type M[B]
int doubleValue(int value) => value*2;Option<int> mapResult = value.map(doubleValue); //Some(20)
  • a flatMap method: from a monadic type M[A] transform A to a monadic type M[B] and return the monadic type M[B]
Option<int> tripleValue(int value) => Option.of(value*3);Option<int> flatMapResult = value.flatMap(tripleValue); //Some(30)

Implementation of monads

Option<A>

Unfortunately, Dart doesn’t have pattern matching. A solution to avoid using if…else instruction to know if we are a None or Some instance and what to do is to define an abstract fold method:

Z fold<Z>(Z Function() onNone, Z Function(A a) onSome)

Either<L,R>

  • More details implementations here
  • Flutter library 📦 is available on pub.dev here

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
cbyad

cbyad

Functional Programming & AI/ML enthusiast 🤓🇨🇮