FreeAst v0.3.2 FreeAst.Interpreter View Source

Helpers to work with interpreters.

Link to this section Summary

Functions

Composes interpreters for different effect kinds

Empty effect interpreter; may be used to start effect interpreter composition

Link to this section Functions

Link to this function

compose(interpreter, kind, interpreter_for_kind)

View Source

Composes interpreters for different effect kinds

Suppose you have two interpreters for two kinds of actions:

defmodule MyApp.FileEffects do
  def interpret(_, :ls, [path]), do: File.ls(path)
end

defmodule MyApp.EnvEffects do
  def interpret(_, :some_variable, []) do
    Application.get_env(:my_app, :some_variable)
  end
end

Now you can compose it like this:

defmodule MyApp.Effects do
  alias MyApp.{EnvEffects, FileEffects}

  def interpret(kind, action, attrs) do
    Effect.noop()
    |> Effect.compose(:file, &FileEffects.interpret/3)
    |> Effect.compose(:env, &EnvEffects.interpret/3)
    |> apply([kind, action, attrs])
  end
end

Empty effect interpreter; may be used to start effect interpreter composition

See compose/3 for more details.