record-operations-0.0.1: polymorphic record operations
Safe HaskellSafe-Inferred
LanguageHaskell2010

RecordOperations

Synopsis

Documentation

type MapRecord f a b = GenMapRecord f a (Rep b ()) Source #

the constraint MapRecord f a b determines that a can be converted into b, using the newtype f.

class MapField a where Source #

Associated Types

type Mapped a :: * Source #

The type mapping for the type of a record field.

Methods

mapField :: a -> Mapped a Source #

The mapping operation on the record field.

mapRecord :: forall b f a. (Generic b, MapRecord f a b) => (forall c. c -> f c) -> a -> b Source #

map a polymorphic function over a record. The function should be implemented as a newtype which is an instance of the MapField class, with corresponding type family Mapped. Pass the newtype constructor to the mapRecord function. For example to "functorize" all fields:

data Foo = Foo
  { foo :: Int
  , bar :: String
  } deriving (Show, Generic)

data FunctorFoo f = FunctorFoo
  { foo :: f Int
  , bar :: f String
  } deriving (Generic)

newtype ToFunctor (f :: * -> *) a = ToFunctor a

instance Applicative f => MapField (ToFunctor f a) where
  type Mapped (ToFunctor f a) = f a
  mapField (ToFunctor x) = pure x

functorize :: Applicative f => Foo -> FunctorFoo f
functorize = mapRecord ToFunctor

type RecordSubSet a b = GenRecordSubSet a (Rep b ()) Source #

The constraint RecordSubSet a b determines that the fields of b are a subset of the fields of a.

recordSubSet :: forall b a. (Generic b, RecordSubSet a b) => a -> b Source #

get subset of a record. For example:

data Foo = Foo
  { foo :: Int
  , bar :: String
  } deriving (Show, Generic)

data Bar = Bar
  { bar :: String
  } deriving (Show, Generic)

subFoo :: Foo -> Bar
subFoo = recordSubSet