Skip to content

Instantly share code, notes, and snippets.

@MateuszKubuszok
Created March 14, 2020 14:57
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 MateuszKubuszok/4872c358805060004513bb2031fb6515 to your computer and use it in GitHub Desktop.
Save MateuszKubuszok/4872c358805060004513bb2031fb6515 to your computer and use it in GitHub Desktop.
Sometimes I missed .uncurried cuntionality in Scala
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
@MateuszKubuszok
Copy link
Author

Alternative to Function.uncurried

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment