fun_land v0.5.0 FunLand

FunLand defines many different Algebraic Data Types.

An Algebraic Data Type is nothing more, than a ‘container’ for some other data type.

Lists are ADTs. And so are Maps. And Sets. And Tuples. And many other things.

Algebraic Data Types have no intrinsic value of their own. They get value, once you fill them with something, and have operations you can perform on their contents.

There are many similarities in the way the different ADTs work. This allows us to define behaviours which generalize to all ADTs. Any of your custom data types that you can implement one or multiple of these behaviours for, is an ADT, and will receive the benefits that the implemented ADTs give.

Another nice thing about this generalization, is that there is no ‘learning a new API’ necessary when switching to one thing-that-is-an-ADT to the next.

To easily use FunLand in your code, call use FunLand, which will alias for you:

  • Mappable -> A structure is Mappable if there is a way to map a function over it: transforming the contents but keeping the structure.
  • Appliable -> A structure is Applibable if it is Mappable and, given two of them where the first contains a partially-applied function, you can apply them together.
  • Applicative -> A structure is Applicative if it is Appliable and you can create a new one by wrapping any value.
  • Chainable -> A structure is Chainable if it is Appliable and you can chain multiple operations after each other, resulting in just a single structure.
  • Monad -> A structure is a Monad if it is both Applicative and Chainable.
  • Semicombinable -> A structure is Semicombinable if there is a way to combine two structures into one.
  • Combinable -> A structure is Combinable if it is Semicombinable and there is a clearly defined ‘neutral’ element.
  • CombinableMonad -> A structure is a CombinableMonad if it is both Combinable and a Monad.
  • Reducable -> A structure is reducable if you can fold/reduce it to a single value, when giving a Combinable or function+default.
  • Traversable -> A structure is Traversable if it is Reducable and there is a way to flip the ???

It will also import the following operators:

  • ~> Shorthand for Mappable.map/2
  • <~> Shorthand for Appliable.apply_with/2
  • ~>> Shorthand for Chainable.chain/2
  • <> Shorthand for Combinable.combine/2. This operator still works the same for binaries, but will now also work for any other Chainable.

If you want, you can also call use FunLand, operators: false to not import these operators.

Summary

Types

adt :: [any] | {} | map | struct

Functions

a <~> b

Infix version of FunLand.Appliable.apply_with/2

a ~> b

Infix version of FunLand.Appliable.map/2

a ~>> b

Infix version of FunLand.Chainable.chain/2

Macros

left <> right

Infix version of FunLand.Combinable.combine/2.

Note that binary strings are Combinable, so “foo” <> “bar” still works.

<>/2 can still be used in pattern-matches and guard clauses, but it will fall back to the behavior of Kernel.<>/2, which means that it will only work with binary strings.