View Source Algae.Free (Algae v1.3.2-doma)

A "free" structure that converts functors into monads by embedding them in a special structure with all of the monadic heavy lifting done for you.

Similar to trees and lists, but with the ability to add a struct "tag", at each level. Often used for DSLs, interpreters, or building structured data.

For a simple introduction to the "free monad + interpreter" pattern, we recommend Why free monads matter.

anatomy

Anatomy

pure

Pure

Pure simply holds a plain value.

%Free.Pure{pure: 42}

roll

Roll

Roll resursively containment of more Free structures embedded in a another ADT. For example, with Id:

%Free.Roll{
  roll: %Id{
    id: %Pure{
      pure: 42
    }
  }
}

Link to this section Summary

Functions

Lift a plain functor up into a free monad.

Add another layer to a free structure

Create an Algae.Free.Pure wrapping a single, simple value

Wrap a functor in a free structure.

Link to this section Types

Link to this section Functions

@spec free(Witchcraft.Functor.t()) :: t()

Lift a plain functor up into a free monad.

examples

Examples

iex> free(%Algae.Id{id: 42})
%Algae.Free.Roll{
  roll: %Algae.Id{
    id: %Algae.Free.Pure{
      pure: 42
    }
  }
}
@spec layer(t(), any()) :: t()

Add another layer to a free structure

examples

Examples

iex> 13
...> |> new()
...> |> layer(%Algae.Id{})
%Algae.Free.Roll{
  roll: %Algae.Id{
    id: %Algae.Free.Pure{
      pure: 13
    }
  }
}
@spec new() :: t()
@spec new(any()) :: t()

Create an Algae.Free.Pure wrapping a single, simple value

examples

Examples

iex> new(42)
%Algae.Free.Pure{pure: 42}

Wrap a functor in a free structure.

examples

Examples

iex> wrap(%Algae.Id{id: 42})
%Algae.Free.Roll{
  roll: %Algae.Id{
    id: 42
  }
}