Fork me on GitHub
#clojure-spec
<
2017-11-18
>
qqq00:11:32

does spec have a way to capture: this object is an ATOM and when you deref the ATOM, it should satisfy this spec

taylor00:11:03

I haven’t seen anything like that. I suppose you could wrap an atom and hook spec on derefs?

qqq00:11:29

We can set a validator-function on the atom, so it'll auto reject on swap! / reset! if it fails the spec.

taylor00:11:42

interesting

qqq00:11:49

However, I now want a way to get the validator function of an atom, so we can say things like "assert that this atom has FOOBAR validator"

danielcompton01:11:35

You also have the same problem for things like manifold deferreds

qqq01:11:29

wtf are manifold deferreds? 🙂

qqq01:11:48

ELI-don't-have-math-PhD

qqq01:11:04

@danielcompton: this looks interesting ; does it also have cljs support ? [ I can't find mention of it anywhere on the page ]

qqq17:11:46

@U0FT7SRLP: thanks for the manifold-cljs link; much appreciated

qqq03:11:36

1. Please ignore the camelCase. 2

[(s/assert ::appState 23) 
 (s/valid? ::appState 23)]
(comment
 [23 false])

3. How is this possible? if 23 is not a valid ::appState, shouldn't assert throw an exception ?

qqq03:11:39

Resolved: I forgot to use (s/check-asserts .... )

danielcompton07:11:39

There’s a CLJS port of it somewhere

mattiasw07:11:20

I have a simple function that takes data, and creates test data from it. Currently, it uses (rand-int ..), what minimum changes do I need to do to this code, in order to use the same seed for random as the rest of the generators.

(defn create-update-operation
  "Change or delete or add a new key to `m`.
   In 30% of the operations, then value will be nil, i.e. delete."
  ;; Do not change or delete any of the pre-defined keys
  ([m] (let [res (create-update-operation (filter #(not (#{:id :tenant :entity :k :updated :version} (key %))) m) {})]
         ;; (println (count res))
         res))
  ([m changes]
   (if (<= (count m) 3) changes
       ;; find the next key to change (update or delete)
       (let [nxt      (rand-int (count m))
             next     (nth m nxt)
             rest     (nthrest m (min 1 nxt)) ;0 just mean that I am regenerating the first key.
             k        (key next)
             _        (assert (some? k))
             v        (update-or-delete-value k)
             changes2 (assoc changes k v)]
         ;; see if we should add a new key to the map
         (if (< (rand-int 10) 2)
           (recur rest (let [k2 (keyword (random-string))
                             v2 (update-or-delete-value k2)]
                         (assoc changes2 k2 v2)))
           (recur rest changes2))))))
where update-or-delete-value also uses rand-int