can someone help me with creating a generator, that generates a map that contains keys like this
{
:amount-debit (gen/pos-int)
:amount-credit (gen/pos-int)
}
The problem is it has to be either debit or credit, and never both. And always one of them should have a value, they can't be null both at the same time@ag like this?
(def gen-card (gen/let
[card-type (gen/elements [:amount-credit :amount-debit])
value gen/s-pos-int]
(hash-map card-type value)))
(println "Sample:" (gen/sample gen-card 5))
;; Sample: ({:amount-debit 1} {:amount-debit 1} {:amount-credit 1} {:amount-debit 3} {:amount-credit 2})
@metametadata: hmmm interesting, my current code is slightly different, but I see what you’re suggesting. I’ll try to see if I can use this style without having to refactor a lot
thanks
Although it should generate a map with both debit and credit, yet only one of them should have a value
I started with gen/let
where I’m using is-debit gen/boolean
and then I think I want to try to use something like :amount-debit (g/such-that (fn [_] (= is-debit true)) g/pos-int)
but this fails with Couldn’t satisfy such-that predicate after 10 tries
ah nevermind using regular if
fork with pos-int and return 0 seems worked
eh, actually it didn’t, I need a need to have a let
block scoped insid gen/hash-map
block
or I have to assoc
two gen/hash-maps
somehow
is there a way to “merge” multiple hash-map
generators?
so this:
(g/let [debit? g/boolean]
(g/hash-map
:created-at (timestamp-gen)
:amount-debit (g/fmap #(when debit? %) g/pos-int)
:amount-credit (g/fmap #(when-not debit? %) g/pos-int)))
does not work 😞um
I would do
(g/fmap (fn [[amount key created-at]] (assoc {:amount-debit nil :amount-credit nil :created-at created-at} key amount)) (g/tuple g/pos-int (g/elements [:amount-debit :amount-credit]) (timestamp-gen)))
@ag ↑