Skip to content

Instantly share code, notes, and snippets.

@mtnygard
Created August 23, 2018 16:00
Show Gist options
  • Save mtnygard/a0ff9365ce8c1508adbc1c76160a91ea to your computer and use it in GitHub Desktop.
Save mtnygard/a0ff9365ce8c1508adbc1c76160a91ea to your computer and use it in GitHub Desktop.
A clojure.spec.gen.alpha generator that picks a multimethod implementation from the known set.
(defn multimethod-selector
"Returns a generator that picks one dispatch value from the known
dispatch values of a multimethod. Defers the lookup of dispatch
values until sampling time, so any defmethods evaluated after the
generator is created may still be selected."
[s]
#(sgen/bind
(sgen/int)
(fn [i]
(let [ms (keys (methods s))]
(sgen/return (nth ms (mod i (count ms))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment