Fork me on GitHub
#clojure-spec
<
2016-11-25
>
triss00:11:17

hi all. I’ve been looking at extracting the minimum and maximum values from specs created with s/int-in and s/double-in...

triss00:11:43

What I’ve done is create a spec that recognises the form’s produced those functions...

triss00:11:40

and then created seperate functions to pull the mins and maxs from the conformed maps that I create with these.

triss00:11:08

can anyone tell me if this is a sane approach?

triss00:11:11

or am I totally bonkers?

Alex Miller (Clojure team)02:11:12

So those will s/form back to their original form in the future (I know they are a mess now). That plus a spec on those forms makes this just a conform away.

ikitommi07:11:47

@yonatanel plan is just to test (and built utilities) out things that we are using with Schema. For Schema we have the schema-tools library (https://github.com/metosin/schema-tools) having modified versions of the core functions for maps. In top there are special walkers, transformers and matchers. Using those both at design & runtime. At runtime, have used schemas for example for frontend form validation, and the forms/schemas can change based on the user input. With Spec, there could be either more functions/macros to manipulate the Specs (like s/merge) or a new MapSpec record which could be used like a regular map. Maybe someone has tried the map-approach already? What use cases / requirements do you have?

slipset09:11:16

Sorry if this has been asked/answered already, but initial googling showed nothing

slipset09:11:27

I have a map like

slipset09:11:56

{:timestamp <jodatime instance> :value number?}

slipset09:11:27

How do I write a spec for the :timestamp that s/exercise understands?

slipset09:11:18

My first attempt is

(s/def ::timestamp (partial instance? DateTime))

slipset09:11:04

but s/exercise doesn’t know how to create instances of this.

slipset09:11:17

ok, so I see there is a inst? which works on java.util.Date. Might get that to work.

slipset09:11:01

which is part of clojure-1.9 but not of future-spec 😞

slipset10:11:11

This is what I ended up with:

slipset10:11:16

(s/def ::timestamp (s/with-gen (partial instance? DateTime)
                     (fn [] (gen/fmap #(DateTime. %)
                                      (gen/large-integer*
                                       {:min (c/to-long
                                              (t/minus (t/now)
                                                       (t/days 365)))})))))

Alex Miller (Clojure team)13:11:21

in 1.9, you can extend the (new) Inst protocol to DateTime - if you do so, you should be able to use s/inst-in

Alex Miller (Clojure team)13:11:48

but what you have should work

slipset13:11:27

Yes, I saw that, but I’m just playing with future-spec by @tonsky.

slipset13:11:50

It’s missing some parts, like eg the much debated any?

slipset13:11:28

But it was a good exercise writing ones own generator.

slipset14:11:07

I’m playing around with higher-ordered functions, so I created this:

slipset14:11:10

(s/fdef foo
        :args (s/fspec :args any?
                       :ret any?)
        :ret 3)
(defn foo [f] (f 3))

slipset14:11:33

So I do (stest/instrument `foo)

slipset14:11:14

and running (foo identity) gives me something like:

slipset14:11:15

ExceptionInfo Call to #‘bar/foo did not conform to spec:
val: (#function[clojure.core/identity]) fails at: [:args] predicate: ifn?
:clojure.spec/args  (#function[clojure.core/identity])
:clojure.spec/failure  :instrument
:clojure.spec.test/caller  {:file "form-init100394334971998293.clj", :line 917, :var-scope bar/eval55924}
  clojure.core/ex-info (core.clj:4617)

slipset14:11:44

what am I missing?

slipset14:11:27

(s/fdef foo
        :args (s/cat :function ifn?)
        :ret 3)
(defn foo [f] (f 3))

slipset14:11:42

works, but leaves stuff to be desired

slipset14:11:56

ok, got it

slipset14:11:59

(s/fdef foo
        :args (s/cat :function (s/fspec :args (s/cat :arguments number?)
                                        :ret any?))
        :ret 3)
(defn foo [f] (f 3))

leongrapenthin21:11:54

http://dev.clojure.org/jira/browse/CLJ-2013 - Are there plans to fix this for 1.9? It would greatly increase error message quality. Is work on it welcome?