This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-09-05
Channels
- # 100-days-of-code (1)
- # announcements (9)
- # aws (1)
- # beginners (195)
- # braveandtrue (60)
- # calva (3)
- # cider (36)
- # cljs-dev (3)
- # clojure (124)
- # clojure-canada (13)
- # clojure-dev (18)
- # clojure-germany (2)
- # clojure-italy (2)
- # clojure-losangeles (1)
- # clojure-nl (9)
- # clojure-russia (1)
- # clojure-spec (20)
- # clojure-uk (109)
- # clojurescript (49)
- # core-logic (29)
- # cursive (7)
- # datomic (62)
- # defnpodcast (1)
- # devcards (11)
- # docker (3)
- # duct (6)
- # figwheel (13)
- # figwheel-main (57)
- # fulcro (2)
- # graphql (11)
- # hyperfiddle (3)
- # jobs (5)
- # jobs-discuss (9)
- # leiningen (2)
- # lumo (1)
- # off-topic (12)
- # onyx (2)
- # pedestal (1)
- # portkey (2)
- # rdf (1)
- # re-frame (39)
- # reitit (13)
- # remote-jobs (2)
- # rum (5)
- # shadow-cljs (82)
- # tools-deps (48)
- # unrepl (3)
- # vim (12)
- # yada (1)
^ makes sense. Thanks for the explanations @seancorfield @alexmiller
cross-posting here too: a sample app with clojure.spec & ring, with conforming-based coercion: https://github.com/metosin/reitit/blob/master/examples/ring-spec-swagger/src/example/server.clj
in the example, the ::x
and ::y
specs are coerced either from strings (the get-endpoint with query-parameters) or from json (the post-endpoint if client sends json), or just validated (the post-endpoint if client sends edn or transit).
Is s/fspec the proper way to define a spec for a higher order function? When I declare something with s/fspec and then try to generate a value, it appears to be called many times (perhaps by conform?) is there a way to declare a higher order function without it being invoked with conform?
Example:
`(s/def ::post-download-fn (s/fspec :args (s/cat :size nat-int?) :ret any? :gen
(fn [] (gen/return (fn post-dl-fn [size] (log/info (str "Post Download fn called! n: " size)))))))`
(gen/generate (s/gen ::post-download-fn))
Gives a bunch of these before returning the single value generated:
Post Download fn called! n: 0
Post Download fn called! n: 1
Post Download fn called! n: 1
....
Am I missing something obvious?I believe it to be instrumentation validating conformance - but it's odd that we can't turn it off in the case of a side effecting higher order function.
@chris547 there might be a better way, but there's a *fspec-iterations*
dynamic binding you can use to sidestep that behavior:
(defn foo [f] (f 1))
(s/fdef foo :args (s/cat :f (s/fspec :args (s/tuple int?))))
(st/instrument `foo)
(binding [clojure.spec.alpha/*fspec-iterations* 0]
(foo #(doto % prn inc)))
np, I'm sure there's better advice/guidance on how to handle this for higher-order functions that take side-effecting functions, and I'm sure it's something that must be considered as clojure.core gets specs. One obvious workaround is to sacrifice specificity by just using e.g. ifn?
as a predicate
Hi, is it possible to nest specs, rather than creating for each tiny bit a new s/def
?
So an inline version of e.g.
(s/def ::key1 string?)
(s/def ::my-map (s-keys [::key1]))
?for s/keys
specs, you need registered specs for the keywords. For other non-`s/keys` types of specs, you can nest spec definitions
Oh I rather meant to define the spec for ::key1 in the ::my-map definition. I don’t want to reuse ::key1
Do you know if there is a way for a recursive spec to nest uniquely? So the same tag can’t repeat across a branch e.g. NO ❌
[:paragraph
[:paragraph "foo"]]
or ❌
[:link
[:link "foo"]]
or ❌
[:italic
[:bold
[:italic "foo"]]]
but repeating siblings are fine ✅
[:paragraph
[:bold "foo"]
[:bold" "bar"]]
Here’s the spec I’m currently using
(s/def ::attrs (s/map-of #{:url} string? :gen-max 2))
(s/def ::hiccup (s/or :string string?
:element (s/cat :tag #{:paragraph :link :bold}
:attrs (s/? ::attrs)
:content (s/* ::hiccup))))
Generally, I would say no
You can of course write any arbitrary predicate you want and include it