1.9 KiB
Effects system in Clojure
tags :: clojure source ::
Warning foreword. The Effect system (see type à la carte) from the Freer Monad article takes in root in a precise definition. Here the notion of Effect system will probably feel totally skewed from the perspective of this article. But I will talk about how to use the same ideas in a dynamicly typed programming language. And in particular Clojure.
On a high level perspective the end goal of an Effect system is to be able to write program such that the semantic of the program will depend on the context the program is executed in.
So for example, let's say you write a program like this:
(defn my-action []
(let [x (first-action)
y (second-action)
z (third-action x y)]
(if z
(fourth-action x)
(fifth-action y))))
Generally you expect every *-action
to be a well defined function.
You can take a look at that function and see what code will be execute.
Within an effect system, you would be able to write something like this:
(with-effects
[logs-eff
db-eff]
(my-action))
(with-effects
[test-logs-eff
test-db-eff]
(my-action))
You can imagine that logs-eff
and db-eff
to be Effects the first
could be used to write logs and the second one will be able to access a DB.
And in the second bloc test-logs-eff
would be a completely pure
implementation of the logs
and a fake db
implementation.
So that way of writting your code is extremely appealing because it will help a lot to test it. Mainly you only need to change the list of effects used in your main function and you will have the same code you will use in production to only use pure effect. And thus will be completely reproductible and easy to test.
But