Fl4m3Ph03n1x

Fl4m3Ph03n1x

Interpreter / composite pattern in Elixir

Background

I am trying to find a consistent way of implementing the Functional Architecture as described in a previous thread Functional Architecture in Elixir.

To summarize the post, that architecture can be understood with the following picture:

Some of you may also recognize this as the Functional core, Imperative shell, from destroyallsoftware:

The way forward

Now, that post identifies some shortcomings of this type of architecture. This post aims to explore a possible solution.

In specific the Interpreter pattern as described in this Ruby talk:

This pattern is built using another common patter, the Composite pattern:

Encapsulation issues

Basically, what this model encourages, is to have a purely functional core. This core never deals with external services, instead, each call to the core returns a tree of actions for the imperative shell to run. Then, this imperative shell runs those commands and invokes the next function on the functional core.

To me, this already has a problem: breaking encapsulation.
Let’s say you have a set of actions in your functional core:

  1. fetch data from DB
  2. do calculations
  3. fetch data from URL
  4. perform evaluation
  5. return result

Normally, under a well designed API, an external user would call P1, and see P5. A service calling our core would invoke P1 and wait for P5.

However, with this pattern, the service calling our core would have to:

  • call P1
  • wait for P1
  • call P2
  • wait for P2
  • call P4 and take P4’s result as final

This means that P2-P4 (implementation details) are now exposed, so the service layer can call them because that’s the only way to continue the computation (Since the core doesn’t actually call anything, the external shell must know what do to next).

This break in encapsulation means a bloated API and problems maintaining in the long term.
Bloated APIs are a recipe for disaster.

Am I missing something?

I watched the full video and I am somewhat familiar with destroyallsoftware, having seen some of their talks as well. But this sacrifice one makes just for the sake of a functional core looks like a time bomb waiting to explode.

  • Am I missing something?
  • Have I misunderstood any concepts?
  • What are your takes on this?

Most Liked Responses

slashdotdash

slashdotdash

The functional core, mutable shell architecture is how I recommend using Commanded, an open source library for event sourcing.

Aggregates and process managers are the building blocks which are used to host the functional core. These are where you write pure functions:

  • Aggregate:
    • f(state, command) => list(events)
    • f(state, event) => state
  • Process Manager:
    • f(state, event) => list(commands)
    • f(state, event) => state

Decisions are always recorded as domain events (as an immutable list of domain-specific facts).

Commanded provides the imperative shell to host the above pure functions in GenServer processes and takes care of all IO, such as fetching and persisting events.

Event handlers are used for side-effects where impure functions can be implemented which react to events and call out to the external world. This could be sending an email, interfacing with a payment gateway, or updating a read-only projection of an aggregate’s events. They can also feed back into the functional core by sending commands to aggregates.

One benefit of the above approach is that aggregates (containing your business logic) can be tested free from any IO concerns as:

  • Given: initial event(s)
  • When: command
  • Expect: resultant event(s)
LostKobrakai

LostKobrakai

I feel the part you’re missing is that your functional core alone is not your domain API. It can’t be if the domain you’re handling is in any shape or form stateful. What the architecture suggests is separating the pure domain logic (how are discounts applied to prices of an order) from stateful/external dependencies (discount code in the DB).

Your functional core cannot answer the question of “is this discount code valid on the current date”. What it can do is “is this discount code valid given this lists of discount code availabilities and a date of usage”.

madlep

madlep

Event sourcing is another great way of implementing this pattern too. Instead of returning values and a callback function (ie a free monad), return an event, then an event handler runs to actually execute the side effect.

If you squint hard enough, they’re the same thing, just different (AKA they’re “isomorphic”). The emitted event =~ the returned value, and the event handler =~ the “next” function.

In real world coding, I’ve used event sourcing many times to handle use cases with side effects mixing with pure code like the one we’re talking about here - but not the free monad/interpreter pattern I’m talking about in that talk. Would be interesting to, but for a lot of teams would be too “out there”

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement