Because of popular demand, and because I’ll probably be busy the next couple of days, so it would need to wait a lot longer if I didn’t publish it now, here it is:
FunLand: This is a package that adds a couple behavours to your Elixir application, which you can use to define Algebraic Data Types.
What exactly are Algebraic Data Types?
They are basically containers for simpler types, in all kinds and shapes.
Some common examples are:
- Lists (as use already every day in Elixir)
- Tuples
- Trees
- Maybe, which either contains a single value or nothing. This allows for propagation of failures in more complex operations.
- Writers, which allow you to keep track of something (such as a log) in the background while passing it through multiple operations that work on simple values.
Why are Algebraic Data Types useful?
Algebraic Data Types are useful in the same way that using loops is useful: They let you re-use a set of operations you already had on a much larger set of inputs/problems.
For instance, Mappable.map
lets you re-use any function that works on a single simple type, to tranform the contents of a collection of things of that type:
Mappable.map(input, fn x -> x*2 end)
would transform the list [1,2,3]
into the list [2,4,6]
, the tuple {3,1,4}
into {6,2,8}
, Maybe.just(6)
into Maybe.just(12)
, etc.
This pre-release of FunLand is mainly because I would like some feedback, and to find out if the design choices I have made so far are sound. To implement Abstract Data Types turned out to be a larger endeavour than I had expected. I hope that FunLand will be able to explain to newcomers how ADTs work and why they are useful, and make it easy for people to define their own.
also, Pull Requests are very welcome!
Sincerely,
~Wiebe-Marten/Qqwy