Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

N-Ary Versions of Tuple Functions

Tags:

haskell

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.

like image 668
Jonathan Fischoff Avatar asked Feb 22 '11 22:02

Jonathan Fischoff


3 Answers

Here is an example to achieve this using Template Haskell.

like image 131
ChrisJ Avatar answered Nov 15 '22 11:11

ChrisJ


The tuple library provides lots of these sorts of functions.

like image 2
John L Avatar answered Nov 15 '22 13:11

John L


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)

like image 1
barsoap Avatar answered Nov 15 '22 12:11

barsoap