The example for multi-spec provided in the spec guide assumes a key that 
denotes a "type". (This key is used as the dispatch fn). In my situation, 
I'm assuming I don't have control over what maps are generated. In other 
words, I explicitly do not want to have to encode types into the input map. 
Instead, I want to rely on the "shape" (key set), since that is the most 
flexible. While I could create a dispatch function that analyzes the input 
and produces a "type", that offers no advantages over what I presented 
earlier. Adding more cases is not extensible since it requires redefining 
the dispatch fn. In other words, I'd be just copying pasting my presented 
function as the dispatch fn.

Alex, did I address your point? Is the approach you are suggesting 
different?

Alternatively, my previous example might have been misleading since I used 
keys to represent useful work. Perhaps it would have been better written as:

(fn [m]
  (condp clojure.spec.alpha/valid? m
      ::foo-map-spec ... do foo stuff ...
      ::bar-map-spec ... do bar stuff ...))

On Wednesday, September 13, 2017 at 3:46:43 PM UTC-4, Alex Miller wrote:

> You might want to look at s/multi-spec which lets you create a variable 
> open spec based on a multimethod, which would in this case be based on key 
> availability.
>
>
> On Wednesday, September 13, 2017 at 11:54:31 AM UTC-5, Brent Millare wrote:
>>
>> I have several maps with different combinations of keys for each map. I 
>> want to process each map but do different work depending on the set of keys 
>> available, basically dispatch on key availability. I thought clojure.spec 
>> might be a good fit for doing the classification step. So for each key, I 
>> could define a spec. Next, I would use clojure.spec.alpha/keys to define a 
>> spec for each set of keys I'd like to match. Finally, I would dispatch like 
>> so:
>>
>> (fn [m]
>>   (condp clojure.spec.alpha/valid? m
>>       ::foo-map-spec :do-foo-stuff
>>       ::bar-map-spec :do-bar-stuff))
>>
>> Does this seem reasonable?
>>
>> The advantage in my mind is its thorough, explicit, and easy to read. 
>> Possible downsides is performance if it mattered.
>>
>> What are the advantages/disadvantages to this approach compared to other 
>> methods?
>>
>

-- 
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.

Reply via email to