clojure - Simplest way to ensure var is vector -
what "simplest"/shortest way ensure var vector? self-written like
(defn ensure-vector [x] (if (vector? x) x (vector x)) (ensure-vector {:foo "bar"}) ;=> [{:foo "bar"}]
but wonder if there core function this? many of them (seq
, vec
, vector
, list
) either fail on maps or apply.
i wonder best name function. box
, singleton
, unit
, v
, cast-vector
, to-vector
, ->vector
, !vector
, vector!
, vec!
?
i further wonder if other languages, haskell, have function built-in.
i think function want use when value collection vec
turns collection vector. vector
function receives items of resulting vector arguments, use when value neither vector or collection.
this possible approach:
(defn as-vector [x] (cond (vector? x) x (sequential? x) (vec x) :else (vector x))) (map as-vector [[1] #{2 3} 1 {:a 1}])
i chose name function based on ones coercions
protocol in clojure.java.io
(as-file
, as-url
).
Comments
Post a Comment