Skip to content

Instantly share code, notes, and snippets.

View wennergr's full-sized avatar

Tobias Wennergren wennergr

View GitHub Profile
G H I J
\ / \ /
D E F
\ | / \
\ | / |
\|/ |
B C
\ /
\ /
A
@wennergr
wennergr / vali.scala
Last active December 5, 2015 20:34
Intellij validated problem
import cats.std.all._
import cats.syntax.option._
import cats.syntax.apply._
import cats.data.Validated._
val a = 1
val b = 2
// Intellij(15) are happy, no red red squiggly.
val ab = (a.some |@| b.some) map { _ + _ }
@wennergr
wennergr / keybase.md
Created December 29, 2015 00:21
keybase.md

Keybase proof

I hereby claim:

  • I am wennergr on github.
  • I am wennergr (https://keybase.io/wennergr) on keybase.
  • I have a public key whose fingerprint is 585F 4750 72B2 5C2F 0463 71CC 88CE 2E10 D296 5051

To claim this, I am signing this object:

@wennergr
wennergr / program.scala
Created March 21, 2018 13:33
Problem Statement
case class Name(firstName: String, lastName: String)
case class Address(street: String, zip: String, state: String)
case class User(name: Name, address: Address)
def getName(id: Long): IO[Name] = /* ... */
def getAddress(id: Long): IO[Address] = /* ... */
val user: IO[User] = ???
@wennergr
wennergr / example.scala
Created March 21, 2018 13:47
Monad example
for {
x <- List(1,2,3)
y <- List(4+x,5+x,6+x)
} yield(y) // List(5, 6, 7, 6, 7, 8, 7, 8, 9)
for {
firstName <- Some("Bob")
lastName <- Some("Axel")
} yield firstname + " " + lastName // Some("Bob Axel")
@wennergr
wennergr / ex.scala
Last active March 26, 2018 13:40
Monad example
// Dependent programs. I need to know `x` to calculate `y`
for {
x <- List(1,2,3)
y <- List(4+x,5+x,6+x)
} yield(y) // List(5, 6, 7, 6, 7, 8, 7, 8, 9)
// Independent programs. No need to know `firstName` to get `lastName`
for {
firstName <- Some("Bob")
lastName <- Some("Axel")
@wennergr
wennergr / ex.scala
Last active March 21, 2018 15:29
Applicative defintion
trait Applicative[F[_]] extends Functor[F] {
def pure[A](x: A): F[A]
def ap[A, B](ff: F[A => B])(fa: F[A]): F[B]
}
@wennergr
wennergr / ex.scala
Last active March 21, 2018 15:29
Applicative with map2
trait Applicative[F[_]] extends Functor[F] {
def pure[A](x: A): F[A]
def ap[A, B](ff: F[A => B])(fa: F[A]): F[B]
def map2[A, B, Z](fa: F[A], fb: F[B])(f: (A, B) => Z): F[Z] =
ap(map(fa)(a => (b: B) => f(a,b)))(fb)
}
// Monad version
for {
firstName <- Some("Bob")
lastName <- Some("Axel")
} yield firstname + " " + lastName // Some("Bob Axel")
// Applicative functor version
Applicative[Option].map2(Some("Bob"), Some("Axel"))((a,b) => a + " " + b) // Some("Bob Axel")
@wennergr
wennergr / ex.scala
Last active March 21, 2018 15:48
Real world example
import cats._, cats.implicits._, cats.effect.IO
case class Name(firstName: String, lastName: String)
case class Address(street: String, zip: String, state: String)
case class User(name: Name, address: Address)
def getName(id: Long): IO[Name] = /* Call third party API */
def getAddress(id: Long): IO[Address] = /* Call third party API */
val userId = 100L