Packages

Category theory concepts in gleam.

Retired package: Renamed - cat

Current section

Files

Jump to
category_theory src category_theory@monad.erl
Raw

src/category_theory@monad.erl

-module(category_theory@monad).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([fish/2, return/1]).
-export_type([writer/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" This module contains monad definitions.\n").
-type writer(FMK) :: {writer, FMK, binary()}.
-file("src/category_theory/monad.gleam", 32).
?DOC(
" `Composition` for the embellished functions that return the Writer type.\n"
" ```haskell\n"
" (>=>) :: (a -> Writer b) -> (b -> Writer c) -> (a -> Writer c)\n"
" m1 >=> m2 = \\x -> \n"
" let (y, s1) = m1 x\n"
" (z, s2) = m2 y\n"
" in (z, s1 ++ s2)\n"
" ```\n"
" ### Examples\n"
" ```gleam\n"
" let up_case = fn(s: String) { Writer(string.uppercase(s), \"upCase \") }\n"
" let to_words = fn(s: String) { Writer(string.split(s, \" \"), \"toWords \") }\n"
" let process = fish(up_case, to_words)\n"
" process(\"Anna has apples\")\n"
" // -> Writer([\"ANNA\", \"HAS\", \"APPLES\"], \"upCase toWords \")\n"
" ```\n"
).
-spec fish(fun((FML) -> writer(FMM)), fun((FMM) -> writer(FMO))) -> fun((FML) -> writer(FMO)).
fish(M1, M2) ->
fun(X) ->
{writer, Y, S1} = M1(X),
{writer, Z, S2} = M2(Y),
{writer, Z, <<S1/binary, S2/binary>>}
end.
-file("src/category_theory/monad.gleam", 51).
?DOC(
" The `identity morphism` for the Writer category.\n"
" ### Examples\n"
" ```gleam\n"
" return(2)\n"
" // -> Writer(2, \"\")\n"
" return(\"abcd\")\n"
" // -> Writer(\"abcd\", \"\") \n"
" ```\n"
).
-spec return(FMR) -> writer(FMR).
return(X) ->
{writer, X, <<""/utf8>>}.