2017-02-08 23:14 GMT+01:00 Rastko Soskic <[email protected]>:
> (let [fns [(fn ([] :empty) ([x] "x")) (fn ([] :format) ([y] "y"))] val
> "field value"]
>   (reduce #(assoc % (%2) (%2 val)) {} fns))

The advantage in this approach is that you can get the keys from a set
of validators, but you still need some outside logic to get at the
result (the reduce).

Since in map validators one wouldn't expect to read any mutable state
to get at the keys (which would necessitate a possibly zero-arg fn
call), you could compile the map validator into a closure and keep the
keys as values in metadata:

(defn juxt-vals [& {:as kvs}]
  (with-meta
    (let [ks (keys kvs)
          vs (vals kvs)]
      (fn [& args]
        (apply hash-map
               (interleave ks (map #(apply % args) vs)))))
    {:validator/map kvs}))

(def ef-validator
  (juxt-vals
    :empty (fn [x] "x")
    :format (fn [y] "y")))

(ef-validator nil) => {:format "y", :empty "x"}
(meta ef-validator) => #:validator{:map {:format
#function[user/fn--21690], :empty #function[user/fn--21688]}}

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

Reply via email to