
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 topics








Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)


Other popular topics










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