Hi Sean,
unfortunately the problem is more complex. The "default" predicate i am
using is a second case which sometimes overlaps with the first pred.
For Example:
(spec/alt :strings (spec/* string?)
:length (spec/* #(< 1 (count (str %))))
on input ["string" 1 ] matches only "string" but on ["string" 11] it
matches both.
Am Donnerstag, 2. August 2018 23:05:49 UTC+2 schrieb Sean Corfield:
>
> I would assume it’s a “maximal munch” strategy at play here: (spec/*
> string?) will only match one string in your input but (spec/* identity)
> will match all three elements so it “wins” as the best match.
>
>
>
> Perhaps :default (spec/* (complement string?)) will do what you need?
> (depending on exactly what you want to happen if you aren’t matching at
> least one string?)
>
>
>
> Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
> An Architect's View -- http://corfield.org/
>
> "If you're not annoying somebody, you're not really alive."
> -- Margaret Atwood
>
>
> ------------------------------
> *From:* [email protected] <javascript:> <[email protected]
> <javascript:>> on behalf of J <[email protected] <javascript:>>
> *Sent:* Thursday, August 2, 2018 6:31:15 AM
> *To:* Clojure
> *Subject:* Order of preds in clojure.spec/alt
>
> Hi there,
>
> right now i am using spec to match some input data with something like
> this
>
> (require '[clojure.spec.alpha :as spec])
>
> (spec/conform (spec/cat :stuff (spec/alt
> :strings (spec/* string?)
> ;:default (spec/* identity)
> )
> :rest (spec/* identity))
> ["some string" 2 2])
>
>
> => {:stuff [:strings ["some string"]], :rest [2 2]}
>
>
>
> But if i add a "default" to alt it returns something unexpected
>
> (spec/conform (spec/cat :stuff (spec/alt
> :strings (spec/* string?)
> :default (spec/* identity))
> :rest (spec/* identity))
> ["some string" 2 2])
>
> => {:stuff [:default ["some string" 2 2]]}
>
>
> The docs
> https://clojure.github.io/spec.alpha/clojure.spec.alpha-api.html#clojure.spec.alpha/alt
>
> says:
> Returns a regex op that returns a map entry containing the key of the
>
> first matching pred and the corresponding value.
>
>
> I would expect that (spec/* string?) is the first matching pred and not the
> (spec/* identitiy).
>
> Is this behavior correct?
>
>
> Thanks J
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected] <javascript:>
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected] <javascript:>
> 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 [email protected] <javascript:>.
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.