Skip to content

Instantly share code, notes, and snippets.

View etorreborre's full-sized avatar
🏠
Working from home

Eric Torreborre etorreborre

🏠
Working from home
View GitHub Profile
@etorreborre
etorreborre / override-employee-status.hs
Last active May 12, 2019 10:24
override-employee-status
registry' =
genVal (pure Permanent)
<: registry
permanentCompany :: GenIO Company
permanentCompany = make registry'
λ> replicateM 5 $ sampleIO (make @(GenIO EmployeeStatus) registry')
[Permanent,Permanent,Permanent,Permanent,Permanent]
@etorreborre
etorreborre / tagging-constructors.hs
Created May 12, 2019 09:57
tagging-constructors
fun genEmployeeStatus
<: genFun (tag @"permanent" Permanent) -- Gen (Tag "permanent" EmployeeStatus)
<: genFun (tag @"temporary" Temporary) -- Gen (Tag "temporary" EmployeeStatus)
genEmployeeStatus ::
GenIO (Tag "permanent" EmployeeStatus)
-> GenIO (Tag "temporary" EmployeeStatus)
-> GenIO EmployeeStatus
genEmployeeStatus g1 g2 = Gen.choice [fmap unTagg1, fmap unTag g2]
@etorreborre
etorreborre / company-generators-1.hs
Created May 12, 2019 09:44
company-generators-1.hs
import Data.Registry
import Data.Registry.Hedgehog
import Hedgehog.Gen
import Hedgehog.Range
import Protolude hiding (list)
import Test.Data.Registry.Company
registry =
genFun Company
<: genFun Department
@etorreborre
etorreborre / nested-gens.hs
Last active May 12, 2019 09:37
nested-gens.hs
genCompany :: Gen Text -> Gen [Department] -> Gen Company
genCompany g1 g2 = Company <$> g1 <*> g2
genDepartment :: Gen Text -> Gen [Employee] -> Gen Department
genDepartment g1 g2 = Department <$> g1 <*> g2
genEmployee :: Gen Text -> Gen EmployeeStatus -> Gen Int -> Gen (Maybe Int) -> Gen Employee
genEmployee g1 g2 g3 g4 = Employee <$> g1 <*> g2 <*> g3 <*> g4
genDepartments :: Gen Deparment -> Gen [Department]
@etorreborre
etorreborre / nested.hs
Created May 11, 2019 11:14
nested data types
data Company = Company {
companyName :: Text
, departments :: [Department]
} deriving (Eq, Show)
data Department = Department {
departmentName :: Text
, employees :: [Employee]
} deriving (Eq, Show)
@etorreborre
etorreborre / arbitrary-text.hs
Created May 11, 2019 11:11
arbitrary-text.hs
-- reuse the Arbitrary instance for String
-- and make only small Text values
instance Arbitrary Text where
arbitrary = Text.take 5 . toS . arbitrary @String
-- reuse the Arbitrary instance for String
-- and make only small Text values
instance Arbitrary Text where
arbitrary = Text.take 5 . toS . arbitrary @String
@etorreborre
etorreborre / components-io.hs
Created May 4, 2019 08:38
How to use IO with components
data Logger m = Logger {
info :: Text -> m ()
, warn :: Text -> m ()
}
-- | IO is only used to create the Logger
newLogger :: Logger IO
newLogger = Logger {
info = \t -> print ("[INFO] " <> show t)
, warn = \t -> print ("[WARN] " <> show t)
@etorreborre
etorreborre / output-batched.hs
Created April 28, 2019 13:37
output-batched.hs
newBatchedOutput :: (Monad m) => Tag "unbatched" (Output m) -> Output m
newBatchedOutput output = Output {
saveOutputs = concat . saveOutputs (unTag output). batchesOf 500
}
batchesOf :: (Monad m) => Int -> Stream (Of a) m r -> Stream (Of [a]) m r
batchesOf batchSize = mapsM toList . chunksOf batchSize
@etorreborre
etorreborre / output-http.hs
Created April 28, 2019 13:28
output-http
newtype Output m = Output {
saveOutputs :: forall a . (ToJSON a) => Stream (Of a) m () -> Stream (Of a) m ()
}
newHttpOutput :: (Monad m) => Http m -> Output m
newHttpOutput http = Output {
saveOutputs = saveOutputs' http
}