impl: add `Applicative` instance on `Generator`
authorJulien Moutinho <julm@sourcephile.fr>
Thu, 17 Feb 2022 01:36:20 +0000 (02:36 +0100)
committerJulien Moutinho <julm@sourcephile.fr>
Thu, 17 Feb 2022 01:36:20 +0000 (02:36 +0100)
src/Webc/Generator.hs

index 1d9becc19f0c5bd016b5632967135fa9fb53063b..fbfbe6545fdfcdf72dab3015208e8877bda0cb5b 100644 (file)
@@ -1,4 +1,5 @@
 {-# LANGUAGE DeriveFunctor #-}
+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
 {-# OPTIONS_GHC -fno-warn-orphans #-}
 
 module Webc.Generator where
@@ -6,7 +7,7 @@ module Webc.Generator where
 import Control.Applicative (Applicative (..), liftA2)
 import Data.Either (Either (..))
 import Data.Eq (Eq (..))
-import Data.Function (($))
+import Data.Function (($), (.))
 import Data.Functor (Functor (..), (<$>))
 import Data.Ord (Ord (..))
 import Data.Semigroup (Semigroup (..))
@@ -24,6 +25,10 @@ newtype Generator a = Generator
   }
   deriving (Functor, Show)
 
+instance Applicative Generator where
+  pure = Generator . pure . pure
+  Generator f <*> Generator x = Generator ((<*>) <$> f <*> x)
+
 generate :: Generator a -> [Gen a]
 generate = unGenerator
 
@@ -35,9 +40,11 @@ instance Applicative Gen where
   f <*> x = Gen (genSlugs f <> genSlugs x) (genValue f (genValue x))
 
 instance IsoFunctor Generator where
-  Iso{..} <%> x = a2b <$> x
+  (<%>) Iso{..} = (a2b <$>)
 instance ProductFunctor Generator where
-  Generator x <.> Generator y = Generator (liftA2 (,) <$> x <*> y)
+  (<.>) = liftA2 (,)
+  (<.) = (<*)
+  (.>) = (*>)
 instance SumFunctor Generator where
   x <+> y =
     Generator $