-- | QoL / Utility functions
module Language.Haskell.TH.Natural.Syntax.Utils (apply, applyT) where

import qualified Language.Haskell.TH as TH
import Language.Haskell.TH.Gen

-- | Applies the given list of arguments to the expression
apply :: (GenExpr f, GenExpr arg) => f -> [arg] -> TH.Q TH.Exp
apply :: forall f arg. (GenExpr f, GenExpr arg) => f -> [arg] -> Q Exp
apply f
f = (Q Exp -> arg -> Q Exp) -> Q Exp -> [arg] -> Q Exp
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Q Exp
rest arg
arg -> [|$Q Exp
rest $(arg -> Q Exp
forall a. GenExpr a => a -> Q Exp
genExpr arg
arg)|]) (f -> Q Exp
forall a. GenExpr a => a -> Q Exp
genExpr f
f)

-- | Applies the given list of type arguments to the type
applyT :: (GenType f, GenType arg) => f -> [arg] -> TH.Q TH.Type
applyT :: forall f arg. (GenType f, GenType arg) => f -> [arg] -> Q Type
applyT f
f = (Q Type -> arg -> Q Type) -> Q Type -> [arg] -> Q Type
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl (\Q Type
rest arg
arg -> [t|$Q Type
rest $(arg -> Q Type
forall a. GenType a => a -> Q Type
genTy arg
arg)|]) (f -> Q Type
forall a. GenType a => a -> Q Type
genTy f
f)