Created
March 14, 2020 14:57
-
-
Save MateuszKubuszok/4872c358805060004513bb2031fb6515 to your computer and use it in GitHub Desktop.
Sometimes I missed .uncurried cuntionality in Scala
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
trait Uncurry[C, U] { | |
def uncurried(curried: C): U | |
} | |
object Uncurry { | |
def instance[C, U](body: C => U) = new Uncurry[C, U] { | |
def uncurried(curried: C): U = body(curried) | |
} | |
implicit def uncurry2[A, B, Out]: Uncurry[A => B => Out, (A, B) => Out] = | |
instance[A => B => Out, (A, B) => Out] { curried => curried(_)(_) } | |
implicit def uncurry3[A, B, C, Out]: Uncurry[A => B => C => Out, (A, B, C) => Out] = | |
instance[A => B => C => Out, (A, B, C) => Out] { curried => curried(_)(_)(_) } | |
implicit def uncurry4[A, B, C, D, Out]: Uncurry[A => B => C => D => Out, (A, B, C, D) => Out] = | |
instance[A => B => C => D => Out, (A, B, C, D) => Out] { curried => curried(_)(_)(_)(_) } | |
implicit def uncurry5[A, B, C, D, E, Out]: Uncurry[A => B => C => D => E => Out, (A, B, C, D, E) => Out] = | |
instance[A => B => C => D => E => Out, (A, B, C, D, E) => Out] { curried => curried(_)(_)(_)(_)(_) } | |
} | |
implicit class UncurryOps[C, U](curried: C)(implicit uncurry: Uncurry[C, U]) { | |
def uncurried: U = uncurry.uncurried(curried) | |
} | |
((i: Int, j: Int) => i.toString + j.toString).curried.uncurried |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative to
Function.uncurried