Skip to content

Instantly share code, notes, and snippets.

@wheaties
Last active November 9, 2017 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wheaties/6c94848130d4ff0422757b737da1ebe3 to your computer and use it in GitHub Desktop.
Save wheaties/6c94848130d4ff0422757b737da1ebe3 to your computer and use it in GitHub Desktop.
Laziness and partial application
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)))
}
//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