Skip to content

Instantly share code, notes, and snippets.

View Tvaroh's full-sized avatar
😻
Happy programming

Aliaksandr Siamionau Tvaroh

😻
Happy programming
View GitHub Profile

Applied Functional Programming with Scala - Notes

Copyright © 2016-2018 Fantasyland Institute of Learning. All rights reserved.

1. Mastering Functions

A function is a mapping from one set, called a domain, to another set, called the codomain. A function associates every element in the domain with exactly one element in the codomain. In Scala, both domain and codomain are types.

val square : Int => Int = x => x * x
@Tvaroh
Tvaroh / collectFirst.scala
Last active January 11, 2018 13:26 — forked from Odomontois/collectFirst.scala
collectFirstM using cats
import cats.implicits._
object CollectFirstM {
implicit class CollectFirstMSyntax[F[_], A](val xs: Seq[A]) extends AnyVal {
def collectFirstM[B, M[_]](f: A => M[Option[B]])(implicit M: Monad[M]): M[Option[B]] =
M.tailRecM(xs) {
case x +: rest => f(x).map {
case some@Some(_) => Right(some)