This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-05-18
Channels
- # aws (3)
- # beginners (18)
- # boot (3)
- # cider (47)
- # clara (54)
- # cljs-dev (62)
- # clojure (104)
- # clojure-berlin (1)
- # clojure-denver (1)
- # clojure-italy (1)
- # clojure-nl (22)
- # clojure-russia (30)
- # clojure-spec (28)
- # clojure-uk (95)
- # clojurescript (31)
- # cloverage (1)
- # cursive (1)
- # datomic (17)
- # duct (4)
- # emacs (27)
- # fulcro (36)
- # graphql (1)
- # hoplon (1)
- # jobs-discuss (1)
- # lein-figwheel (1)
- # lumo (2)
- # off-topic (44)
- # om-next (5)
- # onyx (29)
- # precept (1)
- # re-frame (8)
- # reagent (7)
- # ring (1)
- # ring-swagger (2)
- # schema (4)
- # shadow-cljs (185)
- # spacemacs (21)
- # specter (59)
- # tools-deps (7)
- # vim (15)
- # yada (1)
(def big-integer 1000)
(s/def ::small-integer {:spec (fn [n] (n < big-integer)) :description (str "Smaller than " big-integer)})
#:clojure.spec.alpha{:problems [{:path [],
:pred {:spec (fn [n] (n < big-integer)),
:description (str "Smaller than " big-integer)},
:val 5000,
:via [:xxx.spec/small-integer],
:in []}],
:spec :xxx.spec/small-integer,
:value 5000}
this is not supported
It's easy to hack a (simple) meta registry to do that without abusing the internals of clj.spec.
I did that and a few other horrors here if you re looking for inspiration https://github.com/mpenet/spex
gist of it:
(-> (s/def ::foo any?)
(spex/with-doc "That's a foo"))
(spex/doc ::foo) => "That's a foo"
there's a coll-of
but no corresponding one-of
; i can go from a scalar spec to a collection spec, but i don't think i can go from a collection spec to an element spec. is that intentional?
i'm wondering because i'm currently speccing a text editor, and it's going to have a collection of working documents as well an active document, which would naturally be one of the working documents
i presently have this:
(s/def :world.sometimes.nota.editor/working-notes
(s/coll-of :world.sometimes.nota.app/note
:kind vector?
:distinct true))
(s/def :world.sometimes.nota.editor/active-note
:world.sometimes.nota.app/note)
(s/def :world.sometimes.nota.app/editor
(s/keys :req [:world.sometimes.nota.editor/working-notes
:world.sometimes.nota.editor/active-note]))
but it feels wrong because active-note
isn't just a random note, it's only ever going to be one of the working notes
this would be easier if the working-notes was a set (not sure why it isn’t) but you could s/and an additional check into editor that working-notes contains active-note
it's not a set because the active-note is going to be pulled from the working-notes, and sets would be the wrong data structure for that, right? sets are just about membership, as far as i know
i hadn't thought about putting an s/and on editor, that makes a lot of sense. thanks 🙂
you’re declaring in working-notes that its elements are distinct. that’s the same guarantee sets give you but they can check membership in (near) constant time rather than via a linear search
yeah, i know. i had the :kind as set? before, but then i realised you couldn't pull arbitrary members out of a set; there's going to be a UI that lists all the working notes, and then you click on one to open it. i think a vector is a better choice given that use-case. i suppose i could have a vector and a set, but then i'd have to keep them in sync
what do you mean by “you couldn’t pull arbitrary members out of a set” ? it seems like you can do exactly that
do you mean you need to retain ordering?
i meant that to pull an item out of a set you need the item itself; i thought you couldn't iterate over a set, but i realise i was mistaken. in any case, i think i do need to retain ordering
and that’s a good reason to use a vector, not trying to necessarily change your mind, just probing