The author here provides the following example usage of a do-monad to combine test generators:
(require '[clojure.test.check.generators :as gen])
(require '[clojure.algo.monads :as m])
(m/defmonad gen-m
[m-bind gen/bind
m-result gen/return])
(def vector-and-elem
(m/domonad gen-m
[n (gen/choose 1 10)
v (gen/vector gen/int n)
e (gen/element v)]
[v, e]))
(gen/sample vector-and-elem)
([[0 -1 1 0 -1 0 -1 1] 0]
[[1 1 3 3 3 -1 0 -2 2] 3]
[[8 4] 8]...
There commentator here asserts that this is a great example of monads not just for their own sake, but for providing a genuine value-add.
To me this doesn't seem that different from what a let-block is doing. Indeed - Brian Marick here compares the do-monad to a let block.
My question is: Could this do-monad be replaced by a let block?
As of test.check
0.9.0
there is a macrogen/let
that supports this sort of thing.In the context of
test.check
the answer is no, thisdo-monad
can't be replaced by alet
block. But you can use thegen/bind
andgen/return
manually like this:This is what the
Monad
is doing under the covers for you.Trying to write this as a
let
:Doesn't work because the functions:
choose
,vector
, andelements
returns a generator not the result of the generator. So for examplegen/vector
expects aInteger
as the second argument not agenerator
and thislet
doesn't even compile.