Category Theory
This package implements several category theory concepts, following this book by Bartosz Milewski.
gleam add cat
Monoid Example
import cat.{type Either, Left, Right}
import cat/monoid as mono
pub fn main() {
let either_sum_monoid =
mono.Monoid(
mempty: Left(0),
mappend: fn(e1: Either(Int, String), e2: Either(Int, String)) -> Either(Int, String) {
case e1, e2 {
Right(s), _ -> Right(s)
_, Right(s) -> Right(s)
Left(a), Left(b) -> Left(a + b)
}
}
)
either_sum_monoid
|> mono.mconcat([Left(2), Left(3), Left(4)])
|> io.debug()
// -> Left(9)
either_sum_monoid
|> mono.mconcat([Left(2), Right("error"), Left(4)])
|> io.debug()
// -> Right("error")
}
Functor Example
import cat/functor as fun
import gleam/option
pub fn main() {
option.Some([1, 2, 3])
|> fun.functor_compose(fun.list_functor(), fun.option_functor())(fn(x) {
x % 2 == 0
})
|> io.debug()
// -> option.Some([True, False, True])
}
Further documentation can be found at https://hexdocs.pm/cat.
Development
gleam run # Run the project
gleam test # Run the tests