Last active
November 9, 2017 21:23
-
-
Save wheaties/6c94848130d4ff0422757b737da1ebe3 to your computer and use it in GitHub Desktop.
Laziness and partial application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.data.Kleisli | |
import cats.Eval | |
type Partial[A, B] = Kleisli[Eval, A, B] | |
object Partial{ | |
def apply[A, S, B](f: (S, A) => B)(s: Eval[S]): Partial[A,B] = left(f)(s) | |
def right[A, S, B](f: (A, S) => B)(s: Eval[S]): Partial[A, B] = Kleisli(a => s.map(f(a, _))) | |
def left[A, S, B](f: (S, A) => B)(s: Eval[S]): Partial[A, B] = Kleisli(a => s.map(f(_, a))) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//lazy partial application | |
class Partial[S, -A, +B](f: (S, A) => B, s: => S) extends (A => B){ | |
def apply[AA >: A](a: AA) = f(s, a) | |
def map[C](g: B => C) = new Partial({ (s, a) => g(f(s, a)) }, s) | |
def flatMap[C](g: B => Partial[S, A, C]) = new Partial({ (s, a) => g(f(s, a)).apply(a) }, s) | |
} | |
object Partial{ | |
def apply[S, A, B](f: (S, A) => B)(s: => S) = new Partial(f, s) | |
def left[S, A, B](f: (S, A) => B)(s: => S) = apply(s)(f) | |
def right[S, A, B](f: (A, S) => B)(s: => S) = apply{ (s, a) => f(a, s) }(s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment