Skip to content

Instantly share code, notes, and snippets.

@francoisdevlin
Created December 18, 2009 02:24
Show Gist options
  • Select an option

  • Save francoisdevlin/259237 to your computer and use it in GitHub Desktop.

Select an option

Save francoisdevlin/259237 to your computer and use it in GitHub Desktop.
(defn- same-dispatch [& args]
(class (last args)))
(defmulti
#^{:doc
"same is a mutlimethod that is designed to \"undo\" seq.
It expects a seq-fn that returns a normal seq, and the
appropraite args. It converts the resulting seq into the same
type as the last argument. If it is a sorted seq, the
comparator is preserved.
This operation is fundamentally eager, unless a
clojure.lang.LazySeq is presented. In this case laziness is
preserved, as it is not possible to infer the original data
type."
:arglists '([seq-fn & args])}
same same-dispatch)
(defmethod same String
[hof & args]
(apply str (apply hof f args)))
(defmethod same clojure.lang.LazySeq
[hof & args]
(apply hof args))
(defmethod same :default
[hof & args]
(into (empty (last args)) (apply hof args)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment