Last active
May 31, 2016 21:29
-
-
Save gzm0/5767539 to your computer and use it in GitHub Desktop.
Scala code to reverse the argument list of a curried function.
This file contains 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
trait PopLast[A, Last, Rem] { | |
def pop(f: A, v: Last): Rem | |
} | |
trait LowPrioPopLast { | |
implicit def popEnd[A,B] = new PopLast[A => B, A, B] { | |
def pop(f: A => B, v: A) = f(v) | |
} | |
} | |
object PopLast extends LowPrioPopLast { | |
implicit def popOne[A, B, C, Last, IRem]( | |
implicit iPop: PopLast[B => C, Last, IRem]) = | |
new PopLast[A => B => C, Last, A => IRem] { | |
def pop(f: A => B => C, v: Last) = { a: A => iPop.pop(f(a), v) } | |
} | |
} | |
trait Inverter[A] { | |
type Out | |
def invert(f: A): Out | |
} | |
trait LowPrioInverter { | |
implicit def invertEnd[A,B] = new Inverter[A => B] { | |
type Out = A => B | |
def invert(f: A => B) = f | |
} | |
} | |
object Inverter extends LowPrioInverter { | |
implicit def invertStep[A, Last, Rem](implicit pop: PopLast[A, Last, Rem], | |
inv: Inverter[Rem]) = new Inverter[A] { | |
type Out = Last => inv.Out | |
def invert(f: A) = { a: Last => inv.invert(pop.pop(f, a)) } | |
} | |
} | |
def invert[A](f: A)(implicit inv: Inverter[A]) = inv.invert(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!
Just used it to reverse the curried function needed here: https://groups.google.com/forum/#!topic/scalaz/-Zu-krayTFI
Then the method crankMan doesn't need to have it's argument reversed compared to the chained calls to <*>