Re: clojure.spec - suggestion on how to simplify :pre and :post conditions by using specs.

2016-09-16 Thread joakim . tengstrand
I think a natural place of the :post condition shold be after the argument 
brackets (and that should be possible to implement I think):

(defn user-name [user :core/user] :user/name
  (-> user :user/name))


On Friday, September 16, 2016 at 1:34:47 PM UTC+2, joakim.t...@nova.com 
wrote:
>
> (ns spec-test.core
>   (:require [clojure.spec :as s]))
>
> (s/def :user/name string?)
> (s/def :core/user (s/keys :req [:user/name]))
>
> ; A helper method to get better error messages.
> ; Also imagine that clojure.spec has a similar s/check
> ; function that looks similar to this one
> ; (used in our user-name function):
> (defn check [type data]
>   (if (s/valid? type data)
> true
> (throw (AssertionError. (s/explain type data)
>
>
> ; ...how about if we could write our :pre and :post conditions like this:
> (defn user-name [user :core/user]
>   (-> user :user/name)) :user/name
>
> ; ...so that they expands into this:
> (defn user-name [user]
>   {:pre [(s/check :core/user user)]}
>   {:post [(s/check :user/name user)]}
>   (-> user :user/name))
>
>
>
> ; And if you have other :pre or :post conditions,
> ; then extend the existing ones:
> (defn user-name [user :core/user number]
>   {:pre [pos? number]}
>   (-> user :user/name))
>
> ; ...becomes:
> (defn user-name [user number]
>   {:pre [pos? number
>  (s/valid? :core/user user)]}
>   (-> user :user/name))
>
> ; a call to the function
> (user-name {:user/name "Bill"} 3)
>
>
> ; Maybe it will be hard to find a nice syntax that works for the post 
> condition,
> ; but the :pre condition should be doable I think!
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


clojure.spec - suggestion on how to simplify :pre and :post conditions by using specs.

2016-09-16 Thread joakim . tengstrand


(ns spec-test.core
  (:require [clojure.spec :as s]))

(s/def :user/name string?)
(s/def :core/user (s/keys :req [:user/name]))

; A helper method to get better error messages.
; Also imagine that clojure.spec has a similar s/check
; function that looks similar to this one
; (used in our user-name function):
(defn check [type data]
  (if (s/valid? type data)
true
(throw (AssertionError. (s/explain type data)


; ...how about if we could write our :pre and :post conditions like this:
(defn user-name [user :core/user]
  (-> user :user/name)) :user/name

; ...so that they expands into this:
(defn user-name [user]
  {:pre [(s/check :core/user user)]}
  {:post [(s/check :user/name user)]}
  (-> user :user/name))



; And if you have other :pre or :post conditions,
; then extend the existing ones:
(defn user-name [user :core/user number]
  {:pre [pos? number]}
  (-> user :user/name))

; ...becomes:
(defn user-name [user number]
  {:pre [pos? number
 (s/valid? :core/user user)]}
  (-> user :user/name))

; a call to the function
(user-name {:user/name "Bill"} 3)


; Maybe it will be hard to find a nice syntax that works for the post condition,
; but the :pre condition should be doable I think!

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.