Reputation: 2993
with Clojure core.spec I can have the following:
(s/conform (s/cat :a even? :b (s/* odd?) :a2 even? :b2 (s/* odd?)) [2 3 5 12 13 15])
=> {:a 2, :b [3 5], :a2 12, :b2 [13 15]}
what I'd like to have is to remove redundancy by externalizing the sub spec:
(s/def ::even-followed-by-odds
(s/cat :a even? :b (s/* odd?)))
but
(s/conform (s/tuple ::even-followed-by-odds ::even-followed-by-odds) [2 3 5 12 13 15])
=> :clojure.spec/invalid
this one works:
(s/conform (s/tuple ::even-followed-by-odds ::even-followed-by-odds) [[2 3 5] [12 13 15]])
=> [{:a 2, :b [3 5]} {:a 12, :b [13 15]}]
So what I'm looking for is a function or macro (say unnest) which would it make work:
(s/conform (s/tuple (unnest ::even-followed-by-odds) (unnest ::even-followed-by-odds)) [2 3 5 12 13 15])
=> [{:a 2, :b [3 5]} {:a 12, :b [13 15]}]
how can I get that?
Upvotes: 0
Views: 171
Reputation: 70239
You need to stay in regex op land:
(s/conform (s/cat :x ::even-followed-by-odds :y ::even-followed-by-odds) [2 3 5 12 13 15])
{:x {:a 2, :b [3 5]}, :y {:a 12, :b [13 15]}}
Upvotes: 3