A Clojure library that contains various utility functions that I use.
- Latest stable release is 0.4.1
Leiningen dependency information:
[co.grubb/clj-utils "0.4.1"]
Maven dependency information:
<dependency>
<groupId>co.grubb</groupId>
<artifactId>clj-utils</artifactId>
<version>0.4.1</version>
</dependency>
seq* Return a sequence of the given argument, even if it is a scalar value.
(seq* :a)
; => (:a)
(seq* [1 2 3])
; => (1 2 3)
to-str Convert an arugment to its name or string reprensentation.
(to-str 'a)
; => "a"
(to-str :a)
; => "a"
(to-str {:a 1})
; => "{:a 1}"
munge-string
Replace space and _
characters with a single -
(munge-string "abc def")
; => "abc-def"
(munge-string "abc_def")
; => "abc-def"
(munge-string "abc _def")
; => "abc-def"
(munge-string "abc-def")
; => "abc-def"
keywordize Transform the argument into a keyword by lower-casing and munging it.
(keywordize "abc")
; => :abc
(keywordize "abc_def")
; => :abc-def
(keywordize "Abc Def")
; => :abc-def
map-entry? Returns true if argument is a IMapEntry
(map-entry? (first {:a 1}))
; => true
map-entry Create a MapEntry object
(map-entry :k "value")
; => [:k "value"]
apply-to-keys Apply a function to all the keys in a nested map
(apply-to-keys #(-> % name (str "-x") keyword) {:a {:b 2}})
; => {:a-x {:b-x 2}}
apply-to-leaves Apply a function to all the leaves in a nested map
(apply-to-leaves (partial * 2) {:a {:b 2, :c 3}, :d 4})
; => {:a {:b 4, :c 6}, :d 8}
stringify-keys Just like clojure.walk/stringify-keys
except it will stringify any key type
(stringify-keys {:a {'b {[1 2] :c}}})
; => {"a" {"b" {"[1 2]" :c}}}
keywordize-keys Just like clojure.walk/keywordize-keys
except it uses clj-utils.string/keywordize
instead of keyword
.
(keywordize-keys {"a_x" {"b x" :c}, "d _x" :e})
; => {:a-x {:b-x :c}, :d-x :e}
keys-in Return a vector of vectors containing the key paths in a map
(def m {:a {:b {:c 1, :d 2}, :e 3}, :f 4, :g {:h 5, :i {:j 6}}})
(keys-in m)
; => [[:g :h] [:g :i :j] [:f] [:a :e] [:a :b :c] [:a :b :d]]
(map (partial get-in m) (keys-in m))
; => (5 6 4 3 1 2)
apply-java-new Calls the contstructor for class c
with arguments args
.
(def m {:scheme "http" :host "example.com" :port -1 :path "/index.html"})
(apply-java-new java.net.URI
((juxt :scheme :user-info :host :port :path :query :fragment} m)))
;=> #<URI http://example.com/index.html>
Copyright © 2015 Michael Grubb. All Rights Reserved.
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.