org.clojars.salvatore-tosti/clj-kit0.1.0-SNAPSHOTA library which extends the standard Clojure functions. dependencies
| (this space intentionally left almost blank) | |||
(ns clj-kit.coll-kit) | ||||
Returns true if element is present in given collection, acts like 'contains?' for list and vector. | (defn coll-contains? [coll item] (cond (list? coll) (not (neg? (.indexOf coll item))) (vector? coll) (not (neg? (.indexOf coll item))) :else (contains? coll item))) | |||
Removes nth element from a collection, returns a map with keys :target and :new-coll. | (defn remove-nth [coll index] (cond (nil? coll) nil (empty? coll) nil (< index 0) (remove-nth coll 0) (>= index (count coll) ) (remove-nth coll (dec (count coll))) :else (let [target (nth coll index) head (take index coll) tail (drop (inc index) coll)] {:target target :new-coll (concat head tail)}))) | |||
Removes a random element from a given collection. | (defn remove-random [coll] (remove-nth coll (rand-int (count coll)))) | |||
Helper function used to recursively select a random number of items from a given collection. | (defn- select-random-rec [coll selected number] (if (or (not (pos? number)) (empty? coll)) selected (let [removal (remove-random coll)] (select-random-rec (:new-coll removal) (conj selected (:target removal)) (dec number))))) | |||
Selects a random number of items from a given collection. | (defn select-random [coll number] (select-random-rec coll '() number)) | |||
Splits collections at the nth element, returns a map with keys :target, :head, and :tail. | (defn split-nth [coll index] (cond (nil? coll) nil (empty? coll) nil (< index 0) (split-nth coll 0) (>= index (count coll) ) (split-nth coll (dec (count coll))) :else (let [target (nth coll index) head (take index coll) tail (drop (inc index) coll)] {:target target :head head :tail tail}))) | |||
Removes a random item from a given list, returns a map with keys :target and :new-coll. | (defn remove-random [coll] (remove-nth coll (rand-int (count coll)))) | |||
Returns a random key from a given map. | (defn rand-nth-keys [coll] (rand-nth (keys coll))) | |||
Returns a random value from a given map. | (defn rand-nth-values [coll] (coll (rand-nth-keys coll))) | |||
(ns clj-kit.string-kit) | ||||
Determines if a string only contains letters. | (defn is-only-alpha? [st] (cond (nil? st) false (empty? st) false :else (->> (map #(Character/isLetter %) st) (every? true?)))) | |||
Determines if a string contains at least 1 letter. | (defn contains-alpha? [st] (cond (nil? st) false (empty? st) false :else (->> (map #(Character/isLetter %) st) (some true?) (nil?) (not)))) | |||
Determines if all characters in a string are lowercase. | (defn is-lower? [st] (cond (nil? st) false (empty? st) false :else (->> (map #(Character/isLowerCase %) st) (every? true?)))) | |||
Determines if all characters in a string are uppercase. | (defn is-upper? [st] (cond (nil? st) false (empty? st) false :else (->> (map #(if (Character/isLetter %) (Character/isUpperCase %) true) st) (every? true?)))) | |||
Determines if a given string is an integer. | (defn is-integer? [st] (try (when (Integer/parseInt st) true) (catch Exception e false))) | |||
Determines if a given string is a double. | (defn is-double? [st] (try (when (Double/parseDouble st) true) (catch Exception e false))) | |||