Rubyslava #70

12.04.2018
Moving from imperative to
functional architecture
Jan Herich
Tech stack
• React-Native

• Clojurescript

• Re-frame

• Realm database
Re-frame specifics
• Uses single store for data-model (similar to
idiomatic redux application)

• Provides events to handle data-model
changes

• Provides subscriptions for efficient data-
model querying
Re-frame flow
Asynchronous actions from
“outer world” are modelled
as events changing the app
state.

Subscriptions (reactive
queries) on the app state
provide data input for React
view functions
Anatomy of an event
• Takes data-structure (map) of
so called co-effects
representing everything
needed at the input 

• Returns data-structure (map)
of effects, which represent all
side effects (state changes)
caused by the event
Anatomy of an event
• Events are meant to by
dispatched by asynchronous
actions (like on-click button
listener) and are put on FIFO
queue for further processing. 

• Each time particular event is
being processed, fresh co-
effect map for event handler
function is created and after
effect map result is produced,
distinct effects are
“translated” (eq side effects
being performed)
How to screw up ?
• Obviously, one starts with
good intentions 

• What about the great idea of
generic, re-usable events ?

• Seems very neat and
completely harmless in case
like example on the right
Composing events
What will go wrong ?
What will go wrong ?
How to solve it ?
How to solve it ?
Don’t compose events
Compose functions
Compose functions
Still not ideal
Restoring the nice bits
Restoring the nice bits
Final points
• Using functional language with functional framework
doesn’t automatically mean you can’t screw up hard
exactly where FP is supposed to help you (problems
with state)

• Use events as very high level vocabulary to describe
inputs to your system, don’t try to make them generic
to facilitate code re-use, functions are there for that

• Almost all of the “nice” properties of imperative code
can be achieved using functional combinators, just
don’t be afraid to use them

Rubyslava #70

  • 1.
    Rubyslava #70
 12.04.2018 Moving fromimperative to functional architecture Jan Herich
  • 3.
    Tech stack • React-Native •Clojurescript • Re-frame • Realm database
  • 4.
    Re-frame specifics • Usessingle store for data-model (similar to idiomatic redux application) • Provides events to handle data-model changes • Provides subscriptions for efficient data- model querying
  • 5.
    Re-frame flow Asynchronous actionsfrom “outer world” are modelled as events changing the app state. Subscriptions (reactive queries) on the app state provide data input for React view functions
  • 6.
    Anatomy of anevent • Takes data-structure (map) of so called co-effects representing everything needed at the input • Returns data-structure (map) of effects, which represent all side effects (state changes) caused by the event
  • 7.
    Anatomy of anevent • Events are meant to by dispatched by asynchronous actions (like on-click button listener) and are put on FIFO queue for further processing. • Each time particular event is being processed, fresh co- effect map for event handler function is created and after effect map result is produced, distinct effects are “translated” (eq side effects being performed)
  • 8.
    How to screwup ? • Obviously, one starts with good intentions • What about the great idea of generic, re-usable events ? • Seems very neat and completely harmless in case like example on the right
  • 9.
  • 10.
    What will gowrong ?
  • 11.
    What will gowrong ?
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
    Final points • Usingfunctional language with functional framework doesn’t automatically mean you can’t screw up hard exactly where FP is supposed to help you (problems with state) • Use events as very high level vocabulary to describe inputs to your system, don’t try to make them generic to facilitate code re-use, functions are there for that • Almost all of the “nice” properties of imperative code can be achieved using functional combinators, just don’t be afraid to use them