Closed stevebuik closed 5 months ago
thanks to chatgpt, here's the fdef equivalent for comparison
(s/def ::height int?)
(s/def ::min-height int?)
(s/def ::person (s/keys :req-un [::height]))
(s/def ::tall-people-args (s/keys :req-un [::min-height]))
(s/def ::tall-people-result (s/coll-of ::person :kind vector?))
(defn tall-people [args]
(let [{:keys [min-height]} args]
[{:height 200}
{:height 150}]))
(s/fdef tall-people
:args (s/cat :args ::tall-people-args)
:ret (s/coll-of ::person :kind vector?)
:fn (fn [{:keys [args ret]}]
(let [{:keys [min-height]} (:args args)]
(every? #(>= (:height %) min-height) ret))))
(stest/instrument)
(tall-people {:min-height 180})
although return values are not checked even when instrumented I believe
I hope Malli can be more terse. what other improvements can we make on this?
this can be made to work:
(def Input [:map [:min-height :int]])
(defn guard [{:keys [input output]}]
(every? #(>= (:height %) (:min-height input)) output))
(mx/defn kikka :- [:vector Input]
{:guard guard}
[_ :- Input]
;; body
[{:height 200}
{:height 150}])
Looks good. Can the Input and other schemas be provided by the usual options with a registry with this design?
We have moved to using registries everywhere
On Thu, 24 Aug 2023, 8:50 pm Tommi Reiman, @.***> wrote:
this can be made to work:
(def Input [:map [:min-height :int]])
(defn guard [{:keys [input output]}] (every? #(>= (:height %) (:min-height input)) output))
(mx/defn kikka :- [:vector Input] {:fn guard} [_ :- Input] ;; body [{:height 200} {:height 150}])
— Reply to this email directly, view it on GitHub https://github.com/metosin/malli/issues/936#issuecomment-1691450857, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFZDB3EFMQ7XU3QNH44MULXW4WWDANCNFSM6AAAAAA34PJC24 . You are receiving this because you authored the thread.Message ID: @.***>
I think this is related to / dup of #764
It would be nice if something like this could work. For this, named params are awesome.
(defn area-of-a-square
[len]
{:malli/schema [:=> [:catn [:len nat-int?]]
[:catn [:area
[:and
nat-int?
[:fn (fn [{:keys [area len]}]
(>= area len))]]]]]}
(* len len))
With clojure.spec, I would write it in this way:
;; len --> area
(defn area-of-square
[len]
(* len len))
(s/fdef area-of-square
:args (s/cat :len nat-int?)
:ret nat-int?
:fn (fn [{:keys [ret len]}]
(>= ret len))]]]]]}
quick poke on this.
fixed in #1000.
=> should throw exception when instrumented
spec1 s/fdef can do this so parity or better would be ideal. what are strengths/weaknesses of s/fdef ?