Skip to content

Instantly share code, notes, and snippets.

@xenophobia
Created April 13, 2012 12:42
Show Gist options
  • Save xenophobia/2376657 to your computer and use it in GitHub Desktop.
Save xenophobia/2376657 to your computer and use it in GitHub Desktop.
{-# LANGUAGE TemplateHaskell, QuasiQuotes #-}
module THUtility where
import Language.Haskell.TH
import Control.Monad
import Control.Applicative
mapT :: Int -> ExpQ -> ExpQ
mapT count f = do
vars <- replicateM count $ newName "x"
lamE [tupP $ map varP vars] (tupE $ map (appE f . varE) vars)
mapMT :: Int -> ExpQ -> ExpQ
mapMT count f = do
vars <- replicateM count $ newName "x"
let ops = '(<$>):repeat '(<*>)
lamE [tupP $ map varP vars] $ foldl (\x -> \(op, y) -> (appE (appE (varE op) x) (appE f (varE y)))) (conE $ tupleDataName count) (zip ops vars)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment