2

I wrote this goofy tuplize function:

fun foo(x: Int, y: Int) = 3 * x + 2 * y + 1

fun <T, U, R> tuplize(f: (T, U) -> R): ((Pair<T, U>) -> R) = { (a, b): Pair<T, U> -> f(a, b) }

val xs = listOf(Pair(1, 2), Pair(42, 23))

val f = tuplize(::foo)

val ys = xs.map(f)

It works, but I guess arrow-kt already has something nice build-in, and I just can't find it. Can you help me out? :)

(Sure, I could just use val ys = xs.map { (a, b) -> foo(a, b) }, but in this example, the goal is to express it in point-free style.)

Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
  • Why not this `val ys = xs.map{(a,b) -> foo(a,b)}` instead of this this `val ys = xs.map(tuplize(::foo))` – Vencat Aug 28 '22 at 17:48
  • @Vencat Sure, this would do the trick. But in this example, the goal is to express it in point-free style. I've edited my question accordingly. Thanks. – Tobias Hermann Aug 29 '22 at 05:08
  • This is something closer I see, `val f = { pair:Pair -> pair }.andThen {(first, second) -> foo(first, second)} val vs = xs.map(f)` andThen will compose series of functions – Vencat Sep 01 '22 at 09:33

1 Answers1

1

arrow used to have such utility functions in older versions (check https://github.com/arrow-kt/arrow-core/blob/master/arrow-syntax/src/main/kotlin/arrow/syntax/function/tupling.kt)

however they are deprecated and were removed

abendt
  • 675
  • 5
  • 9