This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-12-14
Channels
- # adventofcode (20)
- # arachne (11)
- # beginners (53)
- # boot (342)
- # cider (54)
- # cljs-dev (39)
- # cljsrn (4)
- # clojure (78)
- # clojure-brasil (2)
- # clojure-italy (5)
- # clojure-nl (4)
- # clojure-quebec (1)
- # clojure-russia (90)
- # clojure-sanfrancisco (4)
- # clojure-spec (55)
- # clojure-uk (27)
- # clojurescript (170)
- # core-async (1)
- # core-logic (1)
- # css (1)
- # cursive (8)
- # datomic (83)
- # dirac (5)
- # hoplon (24)
- # lambdaisland (1)
- # lein-figwheel (23)
- # midje (2)
- # off-topic (1)
- # om (4)
- # om-next (7)
- # onyx (74)
- # proton (1)
- # protorepl (22)
- # rdf (2)
- # re-frame (105)
- # reagent (15)
- # ring-swagger (3)
- # rum (4)
- # slack-help (17)
- # spacemacs (14)
- # untangled (62)
- # vim (4)
- # yada (18)
@ag that does a bunch of unnecessary shuffling on each generation
I'd do (gmap/fmap #(clojure.string/join " " %) (gen/vector (gen/elements (clojure.string/split lorem-ipsum #" ")) 3 5))
no need for a shuffle at all
damn it.. now I’m struggling with turning it into a spec
(def ^:private l-ipsum-gen
(gen/fmap #(clojure.string/join " " %)
(gen/vector (gen/elements (clojure.string/split lorem-ipsum #" ")) 3 5)))
(s/gen (s/with-gen (s/and string?) l-ipsum-gen))
ok… I think I’ve figured it:
(def ^:private l-ipsum-gen
(gen/fmap #(clojure.string/join " " %)
(gen/vector (gen/elements (clojure.string/split lorem-ipsum #" ")) 3 5)))
(gen/sample (s/gen (s/with-gen (s/and string?) (fn [] l-ipsum-gen))))
Hello. Where I should put specs: next to functions, next to tests, one global file with all specs for my project. Once I’ve seen [module-name]-spec.clj separate file for each module.
@pyr are you familiar with gen/let
and/or gen/fmap
?
I was toying earlier with implementing split-with
using spec
user=> (require '[clojure.spec :as s])
nil
user=> (defn s-split-with [pred?] (s/cat :left (s/* pred?) :right (s/* any?)))
#'user/s-split-with
user=> (s/conform (s-split-with int?) (range 1e5))
StackOverflowError clojure.lang.RT.get (RT.java:751)
It's greedy so I'd expect all 1e5 to go left
@alexmiller yes, I'd expect that to happen, not at stack overflow
Yeah just thinking through
What if you don't have right?
the regex derivative is keeping track of all possible solutions - in the last case the split could occur anywhere so there are a large number of those (the others do not have that ambiguity)
so these are (algorithmically) very different specs
what is ident?
Keyword or symbol
folks I’m a bit confused by something right now, and apologies if this is documented somewhere that I missed:
If I have a spec keyword in namespace x
(`(def ::foo …)`) and then I refer to it via a ref in a separate namespace (`[x :as y]`), I can’t seem to use that ref’ed keyword in a defmethod
(`(defmethod foo :y/foo …)`)
does that make sense? Am I doing something obviously wrong?
This is not working because s/keys is a http://macro.Is there any way to do this?
josesanch: i don’t really think spec wants to be used in that way dynamically. i think it’s somewhat designed to ensure some things are static at useful times.... can you do get-mandatory-fields in a more static way and then validate with a dynamic spec NAME rather than a dynamic spec?
otherwise, your choices are eval (probably not a good idea) or macros (which are going to be static anyway)
Thank you @bbloom. I was thinking about that. In this case I'm validating each field individually
in that case, i suggest using the per-field specs (which presumably already exist?) and a simple loop or map or reduce on top
nice. this is one of the great features of spec vs a traditional type system: you can use it as a building block for other dynamic validations
also, (s/keys) will validate all keys
you could combine that with a check for whether a map contains all the required keys
that could be a custom predicate that built from a set of keys
hm, are you saying like you’d make a wrapper object {:required #{::foo ::bar}, :object {::foo … ::bar …}}
and then validate that with a custom predicate?
I’m saying you could (s/and (s/keys) #(every? req-keys-set (keys %)))
I probably typoed that, but you get the idea
@hiredman yeah I figured it out after a little while…PBKAC. Thanks!