Skip to Content
Mastering Functional Programming
book

Mastering Functional Programming

by Anatolii Kmetiuk
August 2018
Intermediate to advanced
380 pages
10h 2m
English
Packt Publishing
Content preview from Mastering Functional Programming

Applicative type class

We would like to define a new primitive for the preceding scenario. We can call this method zip:

type Fx[A] = Either[List[String], A]def zip[A, B](f1: Fx[A], f2: Fx[B]): Fx[(A, B)] = (f1, f2) match {  case (Right(r1), Right(r2)) => Right((r1, r2))  case (Left(e1), Left(e2)) => Left(e1 ++ e2)  case (Left(e), _) => Left(e)  case (_, Left(e)) => Left(e)}

The method will take two computations as its arguments, and it will output a combined result of two of its supplied inputs as a tuple under their common effect type.

Also notice that we are dealing with specific cases of Left being a list of strings. This is to combine multiple error strings for multiple failed computations into one error report.

The way it works is that if ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Functional Programming

Learning Functional Programming

Jack Widman

Publisher Resources

ISBN: 9781788620796Supplemental Content