Git Product home page Git Product logo

clj-utils's Introduction

co.grubb.clj-utils

A Clojure library that contains various utility functions that I use.

Releases and Dependency Information

  • 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>

Usage

clj-utils.seq

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)

clj-utils.string

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

clj-utils.map

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)

clj-utils.java

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>

License

Copyright © 2015 Michael Grubb. All Rights Reserved.

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.