recollections
Safe HaskellNone
LanguageGHC2021

Data.Recollections.TH

Synopsis

Collections generator

mkCollection :: Name -> Q [Dec] Source #

Generate a Collection a type from a enum-like type.

Every constructor is represented by a field. Reserved words like type get a ' suffix.'

data Things = This | That
  deriving (Eq, Ord, Show, Enum, Bounded)

mkCollection ''Things

-- resulting splice
data Collection a = { this, that :: a}
  deriving (Eq, Show, Generic, Generic1, Functor, Foldable, Traversable)
  deriving Applicative via (Generically1 Collection)

mkIndices :: Name -> Q [Dec] Source #

Generate a value filled with the indices matching the fields.

indices :: Collection Things
indices = Collection { this = This, that = That }

This is useful with the Applicative instance to provide indexed operations:

indexed c :: Collection a -> Collection (Things, a)
indexed c = (,) <$> indices <*> c

Distributive

mkDistribute :: Name -> Q [Dec] Source #

Generate a dual of sequenceA.

distribute :: Functor f => f (Collection a) -> Collection (f a)
distribute f = Collection
  { this      = this      <$> f
  , that      = that      <$> f
  , something = something <$> f
  , else'     = else'     <$> f
  , entirely  = entirely  <$> f
  }

Representable

mkIndex :: Name -> Q [Dec] Source #

Read a collection field using an index value.

index :: Collection a -> Things -> a
index c = \case This -> this c; That -> that c; …

mkTabulate :: Name -> Q [Dec] Source #

Generate a collection with a function from its indices

tabulate :: (Things -> a) -> Collection a
tabulate k = Collection { this = k This, that = k That }