This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-11-08
Channels
- # 100-days-of-code (1)
- # admin-announcements (1)
- # aleph (1)
- # announcements (9)
- # beginners (125)
- # cider (1)
- # cljs-dev (80)
- # cljsrn (2)
- # clojure (82)
- # clojure-czech (1)
- # clojure-dev (5)
- # clojure-finland (1)
- # clojure-italy (16)
- # clojure-nl (6)
- # clojure-spec (24)
- # clojure-uk (39)
- # clojurescript (35)
- # community-development (49)
- # core-async (3)
- # cursive (31)
- # data-science (17)
- # datomic (21)
- # emacs (5)
- # fulcro (92)
- # graphql (1)
- # jobs (2)
- # lambdaisland (1)
- # leiningen (19)
- # luminus (9)
- # off-topic (21)
- # parinfer (6)
- # pedestal (1)
- # portkey (2)
- # re-frame (12)
- # reagent (8)
- # reitit (4)
- # shadow-cljs (117)
- # spacemacs (5)
- # specter (4)
- # sql (2)
- # testing (2)
- # tools-deps (3)
- # vim (1)
Can anyone suggest how i might spec that a vector should contain maps in a particular sort order? (based on a key)
It might contain any number of maps, but they should be in a particular order based on a :type
key.
@danielcompton use this predicate?
(defn sorted-by? [coll k]
(and (reduce (fn [prev m]
(if (pos? (compare (get prev k) (get m k)))
(reduced false)
m)) coll)
true))
Wrong @ 😉
I’m not sure if predicates could give more information to spec to present a more helpful error than ::s/invalid
Yeah, i'll need a different comparator but the basic idea would work. Thanks!
(sorted-by? [{:type 2} {:type 3} {:type 3}] :type) ;; true
(sorted-by? [{:type 2} {:type 3} {:type 2}] :type) ;; false
Type is a string and the order is quite specific. But I can easily work that part out, thanks.
I'll use a higher order function like
(defn correctly-sorted? [sort-order k]
(fn [coll] ...))
(defn correctly-sorted? [sort-order k]
(fn [coll]
(reduce
(fn [prev m]
(if (> (.indexOf sort-order (get prev k))
(.indexOf sort-order (get m k)))
(reduced false)
m))
coll)))
Does it matter if the returned value is simply truthy rather than true?note that you’re using >
which doesn’t work for the case when you have equal elements with regards to k
Thanks, you're right on all fronts..
Problem is sorted?
is a core function
sorted-accordingly?
😛
sorted-by?
is just fine
Hi, I'm trying to spec a list of different maps, based on the docs I think I'm supposed to use a multi-spec, which i've done in the past without trouble. However I want to dispatch on more than one key/value, which I haven't seen an example of. Is this supported?
I'm doing this, and valid?
works correctly (returns true).
But explain
returns something unexpected so wanted to check im not doing something wrong
(defmulti foo
(fn [x]
[(:bar x) (:baz x)]))
(defmethod foo [1 2] [_]
#{{:bar 1 :baz 2 :biff 3}})
(defmethod foo [:a :b] [_]
#{{:bar :a :baz :b :biff :c}})
(s/explain (s/multi-spec foo :foo/type) {:bar :a :baz :b :biff :c}) => val: {:bar :a, :baz :b, :biff :c} fails at: [[:a :b]] predicate: foo
(s/valid? (s/multi-spec foo :foo/type) {:bar :a :baz :b :biff :c}) => true
I suspect its because I've just put something random :foo/type
in the retag arg.So it appears that multispec doesn't conform the set to a spec for you, which was causing the inconsistency.
Returning (s/spec #{{:bar :a :baz :b :biff :c}})
from the methods, now keeps both explain and valid happy.
On a slightly unrelated note, is it considered back practice to transform data before checking it against a spec?
I have the same data but in 2 different formats and i'd prefer to not to spec each one individually.
I think I can write some fairly simple code to transform 1 into the other and then check it against a single spec. [a b c] => {:foo a :bar b :baz c}