Skip to content

Instantly share code, notes, and snippets.

@sharat87
Created April 24, 2012 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sharat87/2477120 to your computer and use it in GitHub Desktop.
Save sharat87/2477120 to your computer and use it in GitHub Desktop.
Operating System dependent dispatch of multimethods in clojure

This is me trying out clojure's multimethods for a problem I'm trying to solve. See this Stack Overflow question for follow up.

Note that implementation namespaces are named utils-linux and utils-windows instead of utils.linux and utils.windows because Gists can't have subdirectories.

(ns main
(:use utils))
(require (case current-os
:linux 'utils-linux
:windows 'utils-windows))
(where-am-i)
(ns utils)
(def current-os (-> (System/getProperty "os.name")
.toLowerCase
keyword))
(defmulti where-am-i
(fn [& _] current-os))
(ns utils-linux
(:use utils))
(defmethod where-am-i :linux
[]
(prn (str "I am running on " (System/getProperty "os.name"))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment