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:
- fetch data from DB
- do calculations
- fetch data from URL
- perform evaluation
- 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
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
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
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”
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #podcasts
- #code-sync
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









