5

Is there a library that has n-ary versions of tuple functions like first, ***, etc, through Template Haskell (or using some other method).

Ideally I would like to able to say

$(select 3 [0, 1])

which we make the lambda

\(x, y, z) -> (x, y)

and for a generic *** for functions

$(tapply 3 [(0, "f"), (1, "g"), (2, "h")])

which would make the lambda

\f g h (x, y, z) -> (f x, g y, h z)

Other n-ary functions would also be nice, but those are the two I need currently.

Jonathan Fischoff
  • 1,467
  • 12
  • 22

3 Answers3

3

Here is an example to achieve this using Template Haskell.

ChrisJ
  • 5,161
  • 25
  • 20
3

The tuple library provides lots of these sorts of functions.

John L
  • 27,937
  • 4
  • 73
  • 88
1

Generally, I'd say you want to use a proper ADT instead, and libraries like bifunctor, or, if it gets more complex than that, a proper generics library. (That link could be more up to date... if in doubt, and you don't have especially high performance requirements, just use SYB)

barsoap
  • 3,376
  • 3
  • 23
  • 21