
StevenNunez
Real-World Event Sourcing: Command Tense (page 6)
In v2 of the calculator, does it make sens for the command tenses to be present?
defmodule EventSourcedCalculator.V2 do
def handle_command(%{value: _val}, %{cmd: :add, value: v}) do
%{event_type: :value_added, value: v}
end
def handle_command(%{value: _val}, %{cmd: :sub, value: v}) do
%{event_type: :value_subtracted, value: v}
end
def handle_command(%{value: _val}, %{cmd: :mul, value: v}) do
%{event_type: :value_multiplied, value: v}
end
def handle_command(%{value: _val}, %{cmd: :div, value: v}) do
%{event_type: :value_divided, value: v}
end
def handle_event(%{value: val},
%{event_type: :value_added, value: v}) do
%{value: val + v}
end
def handle_event(%{value: val},
%{event_type: :value_subtracted, value: v}) do
%{value: val - v}
end
def handle_event(%{value: val},
%{event_type: :value_multiplied, value: v}) do
%{value: val * v}
end
def handle_event(%{value: val},
%{event_type: :value_divided, value: v}) do
%{value: val / v}
end
end
For instance the command to add a value is value_added
, however, making it add_value
does 2 things. Communicates that the value has not been added yet and also allows consumers to more naturally respond to a command to add a value. It makes sense for handlers upon adding the numbers to emit value_added
as an event.
Am I misunderstanding something?
First Post!

StevenNunez
I wonder if a better example would have been to model the actions from a calculator.
commands = [
%{action: "press button", value: 1},
%{action: "press button", value: :plus},
%{action: "press button", value: 1},
%{action: "press button", value: :equal},
]
When you run through these commands, the event handler for a button press with the value of equal would attempt to run the calculation. All other events would build a buffer.
This isn’t perfect either though since you’d need to introduce taking input into the example with complicates things in addition to holding state.
Popular Pragmatic Bookshelf topics










Other popular topics










Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /scala
- /zig
- /debian
- /nixos
- /lisp
- /agda
- /sublime-text
- /textmate
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /spring
- /django
- /manjaro
- /diversity
- /nodejs
- /lua
- /c
- /julia
- /slackware
- /neovim