Interpreter Basics

views

Interpreter is a language (or program) to evaluate expressions and execute code. The intent is to keep the evaluation lazy and pure.

There are 3 different kinds of methods in the interpreter pattern / Algebra:

Constructors

  • Takes something that is not an algebra type (not a Response) and produces a Response
  • Usually found on the companion object
  • E.g. Response.ok: takes something that is not a Response (an Entity) and produces a Response (Entity => Response)

Combinators

  • Takes an algebra (Response) and some other stuff (which may be an algebra / Response or something else entirely) and produces a Response / algebra
  • orNotFound: Response[A] => Response[Option[A]]
  • Build up more complex descriptions of what we want to do

Interpreters

  • Takes an algebra (a Response) and produces something else (an IO[Http4sResponse[IO]] )
  • E.g. respond method
  • Does the actual work we have described in the algebra / Response