spec/conform: + vs * differ sometimes, not sure why

149 views
Skip to first unread message

Cole Frederick

unread,
Jul 30, 2017, 2:33:45 AM7/30/17
to Clojure
Hi to anyone reading,
I'm trying to understand a difference in behavior when using clojure.spec/conform on two different regex ops: + and *.
They often conform the same when there is at least one item, but not always.

One small case I've found is:
(s/def ::evens-or-odds (s/alt :evens (s/+ even?)
                              :odds  (s/+ odd?)))

(defn conform-test [nums]
  (let [star (s/conform (s/* ::evens-or-odds) nums)
        plus (s/conform (s/+ ::evens-or-odds) nums)]
    [(= star plus)
     star
     plus]))

(conform-test [1 2 3]) =>
[true
 [[:odds [1]] [:evens [2]] [:odds [3]]]
 [[:odds [1]] [:evens [2]] [:odds [3]]]]

(conform-test [1 2 2]) =>
[false
 [[:odds [1]] [:evens  [2 2]]]
 [[:odds [1]] [[:evens [2 2]]]]]

(conform-test [1 2 2 3]) =>
[false
 [[:odds [1]] [:evens  [2 2]] [:odds [3]]]
 [[:odds [1]] [[:evens [2 2]] [:odds [3]]]]]

They conform the same for the first, but on the second and third, the + regex wraps all but the first group in a vector. I believe it has something to do with nested regex ops, but I haven't figured out the pattern.

Same conform value:
[1 2]
[1 2 3]
[1 1 2 3]

Different conform values:
[1 2 2]
[1 2 2 3]
[1 2 3 3]

I appreciate your time and any insight or ideas about why these sets of cases conform differently.
Thanks,
Cole

Alex Miller

unread,
Jul 30, 2017, 8:55:04 AM7/30/17
to Clojure
This is a bug, similar to https://dev.clojure.org/jira/browse/CLJ-2105 and http://dev.clojure.org/jira/browse/CLJ-2003. I've spent a little time on it but have not figured out the exact problem. If you'd like to file it and reference those and/or drop it as a comment on one of those, would be happy to have more cases to verify when it's fixed.

Cole Frederick

unread,
Jul 31, 2017, 1:00:54 PM7/31/17
to Clojure
Awesome. Can do!
Thank you.

Peter Hull

unread,
Aug 10, 2017, 4:40:28 AM8/10/17
to Clojure

On Sunday, July 30, 2017 at 6:55:04 AM UTC-6, Alex Miller wrote:
This is a bug, similar to https://dev.clojure.org/jira/browse/CLJ-2105 and http://dev.clojure.org/jira/browse/CLJ-2003. I've spent a little time on it but have not figured out the exact problem. If you'd like to file it and reference those and/or drop it as a comment on one of those, would be happy to have more cases to verify when it's fixed.

I notice that Tyler Tallman has posted a patch for CLJ-2003 recently (https://dev.clojure.org/jira/browse/CLJ-2003). Would be worth a look to see if this solves your problem and also CLJ-2105.
Pete
Reply all
Reply to author
Forward
0 new messages