Youâre already using all that stuff, whatâs intimidating is that you need some background to know their formal names.
For example, say you want to perform some task in parallel, so you need to split the work, but in order to split it and make it the same as doing it serially, you need to satisfy that splitting [abc]
into [ab][c]
is the same as [a][bc]
under the operation you want to perform. In other words, you can arbitrarily split the task and itâs not order dependent.Thatâs a practical application of a monoid. Thereâs a Rich Hickey talk about this but I canât recall which one, either the one about transducers or the one about reducers, he mentions this property about parallelizable work and says something like âand this is a monoidâ and chuckles.
Or you have stuff inside some context, like [a, b]
or {:ok, a}
, and you want a function that can apply a function to these wrapped values. Thatâs map, and the map+context is what defines a functor. If the context can be 2 values, like {:ok, a}
and {:error, b}
, and map takes a function for each case, then itâs a bifunctor.
If you have a wrapped value {:ok, a} and a function that takes a value a and returns {:ok, b}
, applying map twice would return {:ok, {:ok, b}},
so you want a function that applies the function and unwraps the result, thatâs flat_map/bind, and itâs what characterizes a monad.
Foldables are comparable to an Enum
that you can reduce, and itâs why in Erlang itâs named :lists.foldl
.
And so on and so forth. If you know the background for these names, then itâs simple and useful. If you donât, then you end up implementing ad-hoc versions of them all the time.
What IS weird about them in Elixir is that they are mostly formalized with regards to functions, not data, so a lot of it is about composing functions, and more so in environments where functional composition is the only way you have to write a program(Haskell works this way). In Elixir we have higher order functions but we donât have auto currying so it becomes weird to properly implement that programming style.