Learning Cats

December 09, 2015

These are my notes from learning Cats.

build.sbt:

libraryDependencies += "org.spire-math" %% "cats" % "0.3.0"

Kleisli

import cats.data._

type Id[A] = A

def id[A](x: A): Id[A] = x

def get[A] = Kleisli { x: A => id(x) }

val greeting =
  for {
    name <- get[String]
  } yield s"hello, ${name}"

val reminder =
  get[String] map { x => s"your name is ${x}" }

val p =
  for {
    greetingS <- greeting
    reminderS <- reminder
  } yield s"${greetingS}, ${reminderS}"

println(p.run("James"))