This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-26
Channels
- # admin-announcements (1)
- # alda (44)
- # aws-lambda (6)
- # beginners (8)
- # boot (187)
- # capetown (5)
- # cider (25)
- # cljs-dev (24)
- # cljsrn (93)
- # clojure (45)
- # clojure-austin (9)
- # clojure-canada (2)
- # clojure-greece (1)
- # clojure-mexico (3)
- # clojure-poland (3)
- # clojure-russia (1)
- # clojure-spec (12)
- # clojure-uk (13)
- # clojurescript (86)
- # cursive (9)
- # datascript (3)
- # datomic (32)
- # defnpodcast (4)
- # devcards (23)
- # editors (3)
- # emacs (5)
- # hoplon (27)
- # immutant (3)
- # lein-figwheel (9)
- # leiningen (4)
- # luminus (10)
- # om (32)
- # onyx (2)
- # other-languages (1)
- # perun (1)
- # protorepl (8)
- # re-frame (13)
- # reagent (2)
- # remote-jobs (2)
- # ring (3)
- # spacemacs (4)
- # spirituality-ethics (3)
- # test-check (16)
- # untangled (65)
- # yada (50)
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
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)
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 😞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)))