io.randomseed.utils

added in 1.0.0

Random support functions and macros.

add-spc-b

(add-spc-b s)

Adds space characters to the beginning and end of the given string s if it is not an empty string. Otherwise it returns a string with a single space.

add-spc-l

(add-spc-l s)

Prepends space character to the given string s if it is not an empty string.

add-spc-r

(add-spc-r s)

Appends space character to the given string s if it is not an empty string or a single space.

ask

(ask & {:keys [ask-fn allow-empty? empty-nil? empty-quits? empty-quits-nil? prompt confirm-prompt not-match-msg empty-msg retries confirmation?], :or {confirm-prompt "Repeat text: ", not-match-msg "Texts do not match.", empty-msg "Text is empty.", empty-nil? false, confirmation? true, empty-quits-nil? true, ask-fn read-line-with-prompt, prompt "Enter text: ", allow-empty? false, empty-quits? false}})

Ask user for a string with optional confirmation using ask-fn to get a string (or nil). Repeats until two entered strings are the same and are not empty. Keyword arguments can be given to configure behavior: prompt (message displayed when asking for first string), confirm-prompt (message displayed when asking for the same string again), not-match-msg (message displayed when strings do not match), empty-msg (message displayed when the entered string is empty), retries (number of retries before quitting the loop; when set to nil or not given, it will continue indefinitely), confirmation? (requires string to be re-entered for confirmation, defaults to true), allow-empty? (allows the entered string to be empty; defaults to false), empty-nil? (returns nil instead of an empty string; defaults to false), empty-quits? (short-circuits on any empty string and returns nil; defaults to false), empty-quits-nil? (returns nil when quitting on empty string; defaults to true). Returns the entered string or nil.

atom?

(atom? v)

Returns true if v is an instance of clojure.lang.Atom.

bytes-concat

(bytes-concat)(bytes-concat bary)(bytes-concat bary & byte-arys)

Concatenates byte arrays.

bytes-to-string

(bytes-to-string b)

Converts bytes into a string

bzero

Returns zeroed array of bytes.

char-ranges->set

added in 1.0.0

(char-ranges->set & ranges)

Returns a set of characters defined as a collection of collections with start and stop character, e.g.: \A \Z

const-form-no-ident?

(const-form-no-ident? x)

Returns true when x is nil or is of one of the following types: string or boolean or number or character. Otherwise returns false.

const-form?

(const-form? x)

Returns true when x is nil or is of one of the following types: string or keyword or boolean or number or character. Otherwise returns false.

contains-some?

(contains-some? s1 s2)

Takes two indexed collections and returns true if at least one element is shared in both. Otherwise it returns false.

count-digits

added in 1.0.0

(count-digits n)

Returns a number of digits in a decimal number n.

current-thread

(current-thread)

Returns the current Thread object.

current-thread-id

(current-thread-id)

Returns the ID of the current thread.

current-thread-name

(current-thread-name)

Returns the name of the current thread.

defdoc!

macro

(defdoc! v docstr)

Replaces documentation string of a Var.

empty-ident?

(empty-ident? v)

Returns true if v is an empty identifier. Will throw an exception when s is not an identifier.

empty-string?

(empty-string? s)

Returns true if s is an empty string. Will throw an exception when s is not a string.

ensure-ident-keyword

(ensure-ident-keyword id)

Returns id unchanged if it is an identifier. Otherwise converts it to a keyword via ensure-keyword.

ensure-keyword

(ensure-keyword id)

Takes an identifier id and tries to convert it to a keyword. If it is not an identifier, it calls keyword.

ensure-keyword-having-ns

(ensure-keyword-having-ns id ns)

Converts id to a keyword and ensures it has namespace ns using must-have-ns.

ensure-namespaced-keyword

(ensure-namespaced-keyword id ns)

Converts id to a keyword and ensures it is namespace-qualified, adding ns as namespace only if one is not already present.

ensure-ns

(ensure-ns id ns)

Takes an identifier id and a namespace ns (a string), and tries to set a namespace in the identifier. If a namespace already exists in identifier, it is not changed.

ensure-str

(ensure-str v)(ensure-str v & more)

Converts v to a string, preserving namespaces for identifiers. Returns an empty string when v is not valuable. With multiple arguments, concatenates results of some-str applied to each.

ensure-str-simple

added in 1.2.36

(ensure-str-simple v)(ensure-str-simple v & more)

Like ensure-str but removes namespace from identifiers, using only the name part. With multiple arguments, concatenates results of some-str-simple applied to each.

exception?

(exception? v)

Returns true if v is an instance of java.lang.Exception.

find-first

(find-first f coll)(find-first f coll not-found)

Returns the first item from coll for which (f item) returns true or nil if no such item is present. If the given not-found value is supplied, it will return it instead of nil.

fn-name

(fn-name f)

Tries to obtain symbolic function name for the given function f. Uses metadata and Java reflection as fallback.

gen-digits

added in 1.0.0

(gen-digits num)(gen-digits num rng)

Generates the given number of random digits and converts all into a single string. When the second argument is present it should be an instance of random number generator used to get the digits.

get-rand-int

added in 1.0.0

(get-rand-int n)(get-rand-int n rng)

Like rand-int but optionally uses random number generator.

inferred-contains?

added in 1.0.0

(inferred-contains? coll k)

Just like the contains? but if the keyword is namespace-qualified it also checks if the collection contains the same keyword as its key but without a namespace.

inferred-get

added in 1.0.0

(inferred-get coll k)(inferred-get coll k default)

Just like the get function but if the keyword is namespace-qualified it first attempts to look for the value associated with it. If that fails it uses the variant of the keyword without any namespace.

insert-at

(insert-at index coll element)

Takes an index number index, a collection coll and an element element, and inserts element’s value under the given index number. Uses sequential operations: split-at, concat and cons.

instant?

(instant? v)

Returns true if v is an instance of java.time.Instant.

is

macro

(is pred val & body)

Takes a predicate pred, a value val and a body. Evaluates val and passes to pred. If the result is truthy it evaluates all expressions from body in an implicit do. Otherwise it returns the value.

is-not

macro

(is-not pred val & body)

Takes a predicate pred, a value val and a body. Evaluates val and passes to pred. If the result is truthy it returns the value. Otherwise it evaluates all expressions from body in an implicit do.

juxt-seq

(juxt-seq & functions)

Like clojure.core/juxt but produces lazy sequence of results instead of a vector.

lazy-iterator-seq

added in 1.0.0

(lazy-iterator-seq coll)(lazy-iterator-seq coll iter)

Returns a lazy sequence as an interface to the given iterable Java object.

mapply

(mapply f & args)

Like apply but works on named arguments. Takes function f and a list of arguments to be passed, were the last argument should be a map that will be decomposed and passed as named arguments.

Returns the result of calling f.

must-have-ns

(must-have-ns id ns)

Ensures that the identifier id has namespace ns. If id already has the given namespace, returns it unchanged. Otherwise re-creates it with ns as its namespace.

named-to-str

(named-to-str v)

Converts a value v to a string. If keyword is given, it will have : character removed.

named-to-str-simple

added in 1.2.36

(named-to-str-simple v)

Converts a value v to a string. If ident is given, namespace will be removed. Keywords will have the : character removed.

named-to-str-trim

(named-to-str-trim v)

Converts a value v to a string and trims its both sides. If keyword is given, it will have : character removed.

named-to-str-trim-simple

added in 1.2.36

(named-to-str-trim-simple v)

Converts a value v to a string and trims its both sides. If keyword is given, it will have : character removed. Idents will have namespace removed.

nil-or-empty-str?

(nil-or-empty-str? x)

Returns true if the given x is nil or an empty string.

nil-spc-or-empty-str

(nil-spc-or-empty-str s)

Takes a string or nil object s and returns an empty string if it is nil, empty string or a string containing a single space only.

nil-spc-or-empty-str?

(nil-spc-or-empty-str? s)

Returns true if the given value s is nil, an empty string or a string containing a single space only.

normalize-name

(normalize-name some-name)(normalize-name some-name default-name)

Takes a name expressed as a string or an identifier. If the object is an identifier (a symbol or a keyword) then it converts it to a string using name function. If the second argument is present then it uses it when the given name or a name derived from identifier is empty.

normalize-name-with-ns

(normalize-name-with-ns some-name)(normalize-name-with-ns some-name default-name)

Takes a name expressed as a string or an identifier. If the object is an identifier (a symbol or a keyword) then it converts it to a string using namespace and name functions. If the second argument is present then it uses it when the given name or a name derived from identifier is empty.

normalize-to-bytes

(normalize-to-bytes t)

Normalizes t via normalize-name and then converts the result to a byte array.

not-empty-ident?

(not-empty-ident? v)

Returns true if v is not an empty identifier. Will throw an exception when s is not an identifier.

not-empty-string?

(not-empty-string? s)

Returns true if s is not an empty string. Will throw an exception when s is not a string.

not-valuable

macro

(not-valuable & more)

Evaluates expressions from more and returns the result of the last one only when it is not valuable (nil or empty). Returns nil otherwise.

not-valuable?

(not-valuable? x)

Returns true if x not valuable: is nil or empty.

ns-infer

added in 1.0.0

(ns-infer ns-name k)(ns-infer ns-name k use-infer)

Takes a string of namespace name and a keyword. If the given keyword is not namespace-qualified it returns a new keyword with the given namespace added. If the given keyword is already equipped with a namespace it returns it.

or-some

macro

(or-some)(or-some x)(or-some x & next)

Same as or but returns first value which is strictly not nil.

parse-long

(parse-long s)(parse-long s default)

Alias for some-long. Parses a value to long, with optional default.

parse-num

(parse-num n default)(parse-num n)

Parses n to a number. Returns bigdec for strings longer than 15 chars or containing a decimal point, otherwise returns a long. Returns nil when n is not valuable, or default when supplied.

parse-percent

(parse-percent n default)(parse-percent n)

Parses n as a number and divides it by 100 to produce a fractional value. Returns nil when n is not valuable, or default when supplied.

parse-re

(parse-re v)

Compiles the string v into a regex pattern. Returns nil when v is not a valuable string.

parse-url

(parse-url u)

Parses URL into a map.

percent

Alias for parse-percent.

pos-val

(pos-val x)

Returns the given value x if it is a positive number. Otherwise it returns nil.

qsome

macro

(qsome pred coll)

Same as clojure.core/some but when coll is a constant form then a source code with or expression is generated instead of some with recurrent predicate application. Short-circuits when pred is nil or false, returning nil.

qstrb

macro

(qstrb)(qstrb a)(qstrb a & more)

Calls strb but checks if the first and only argument is a string, and if it so, returns its literal form without calling strb.

random-digits-len

added in 1.0.0

(random-digits-len x iteration shrink-now)(random-digits-len x iteration shrink-now rng)

For 0 or 1 it returns its argument. For other positive numbers it returns a random natural number from 1 to this number (inclusive) in 50% cases. In other 50% cases it returns its argument.

random-uuid

(random-uuid)

Generates a random UUID. Delegates to clojure.core/random-uuid when available, otherwise uses java.util.UUID/randomUUID directly.

read-line-with-prompt

(read-line-with-prompt)(read-line-with-prompt prompt)

Reads a line of text from console with optional prompt. Returns a string or nil when the entered string is empty.

replace-first

(replace-first s c r)

Replaces the first appearance of a character c in the given string s with a character r.

safe-parse-long

(safe-parse-long v default)(safe-parse-long v)

Like some-long but catches any Throwable and returns nil (or default) instead of throwing.

safe-parse-num

(safe-parse-num v default)(safe-parse-num v)

Like parse-num but catches any Throwable and returns nil (or default) instead of throwing.

safe-parse-percent

(safe-parse-percent v default)(safe-parse-percent v)

Like parse-percent but catches any Throwable and returns nil (or default) instead of throwing.

sanitize-base-url

(sanitize-base-url url)

Trims and normalizes a base URL string: prepends https:// when no http scheme is present and appends a trailing / when missing. Returns nil for empty input.

simple-keyword-up

(simple-keyword-up v)

Converts v to a simple (unqualified), upper-cased keyword. Returns nil when v is not valuable.

simple-quote-form?

(simple-quote-form? x)

Returns true when x is a list or is an instance of clojure.lang.Cons, has 2 elements or less and its first element is the quote symbol, plus its second element is a symbol or const-form? returns true for it.

simple-symbol-up

(simple-symbol-up v)

Converts v to a simple (unqualified), upper-cased symbol. Returns nil when v is not valuable.

some-fn*

(some-fn* p)(some-fn* p1 p2)(some-fn* p1 p2 p3)(some-fn* p1 p2 p3 p4)(some-fn* p1 p2 p3 p4 & preds)

Same as clojure.core/some-fn but multiple arguments are passed to each predicate function and nullary variant is not returning nil but calls each predicate without passing any arguments.

Takes a set of predicates and returns a function that returns the first truthy value (not nil and not false) returned by one of its composing predicates against all of its arguments, else it returns a value returned by the last predicate given (which may be false or nil).

some-keyword

(some-keyword v)

Converts v to a keyword, preserving namespace. Returns v unchanged if already a keyword, nil when v is not valuable.

some-keyword-down

(some-keyword-down v)

Converts v to a lower-cased keyword, preserving namespace. Returns nil when v is not valuable.

some-keyword-down-tr

(some-keyword-down-tr v)

Converts v to a lower-cased, trimmed keyword, preserving namespace. Returns nil when v is not valuable.

some-keyword-simple

(some-keyword-simple v)

Converts v to a simple (unqualified) keyword, stripping namespace. Returns nil when v is not valuable.

some-keyword-up

(some-keyword-up v)

Converts v to an upper-cased keyword, preserving namespace. Returns nil when v is not valuable.

some-long

(some-long s default)(some-long s)

Converts s to a long. If s is already a number, coerces it; otherwise parses it as a string. Returns nil when s is not valuable, or default when supplied.

some-str

(some-str v)

Converts the given value v to a string. Keywords are transformed to strings without the : prefix. Empty string or nil will result in nil being returned.

some-str-down

(some-str-down v)

Converts the given value v to a lowercase string. Keywords are transformed to strings without the : prefix. Empty string or nil will result in nil being returned.

some-str-simple

(some-str-simple v)

Converts the given value v to a string. If v is an identifier only its name part is used and namespace is ignored. Keywords are transformed to strings without the : prefix. Empty string or nil will result in nil being returned.

some-str-simple-down

(some-str-simple-down v)

Converts the given value v to a lowercase string. If v is an identifier only its name part is used and namespace is ignored. Keywords are transformed to strings without the : prefix. Empty string or nil will result in nil being returned.

some-str-simple-up

(some-str-simple-up v)

Converts the given value v to an uppercase string. If v is an identifier only its name part is used and namespace is ignored. Keywords are transformed to strings without the : prefix. Empty string or nil will result in nil being returned.

some-str-spc

(some-str-spc s & more)

Takes one or more strings or other objects convertable to strings and concatenates them with spaces. Keywords are transformed to strings without the : prefix.

some-str-spc-simple

added in 1.2.36

(some-str-spc-simple s & more)

Takes one or more strings or other objects convertable to strings and concatenates them with spaces. Keywords are transformed to strings without the : prefix. Namespaces are removed from idents.

some-str-squeeze-spc

(some-str-squeeze-spc s & more)

Takes one or more strings or other objects convertable to strings and concatenates them with spaces and squeezes spaces in a resulting string which is returned. Keywords are transformed to strings without the : prefix.

some-str-squeeze-spc-simple

added in 1.2.36

(some-str-squeeze-spc-simple s & more)

Takes one or more strings or other objects convertable to strings and concatenates them with spaces and squeezes spaces in a resulting string which is returned. Keywords are transformed to strings without the : prefix. Namespaces are removed from idents.

some-str-up

(some-str-up v)

Converts the given value v to an uppercase string. Keywords are transformed to strings without the : prefix. Empty string or nil will result in nil being returned.

some-string

(some-string s)

Takes a string s and returns it unless its value is nil or it is an empty string. A bit more performant but will throw an exception when s is not a string nor nil.

some-symbol

(some-symbol v)

Converts v to a symbol, preserving namespace. Returns v unchanged if already a symbol, nil when v is not valuable.

some-symbol-simple

(some-symbol-simple v)

Converts v to a simple (unqualified) symbol, stripping namespace. Returns nil when v is not valuable.

some-symbol-up

(some-symbol-up v)

Converts v to an upper-cased symbol. Returns nil when v is not valuable.

str-spc

(str-spc s & more)

Takes one or more strings or other objects convertable to strings and concatenates them with spaces.

str-squeeze-spc

(str-squeeze-spc s & more)

Takes one or more strings or other objects convertable to strings and concatenates them with spaces and squeezes spaces in a resulting string which is returned.

strb

(strb a)(strb a b)(strb a b c)(strb a b c d)(strb a b c d e)(strb a b c d e & more)

Like clojure.core/str but faster. Be aware that all arguments except first must be of type String or nil.

strs

macro

(strs)(strs a)(strs a & more)

Converts all arguments to strings and concatenates them with keywords being converted to strings without the : prefix. Neighbouring literal strings and known constant forms will be concatenated at compile time.

strs-quick

macro

(strs-quick)(strs-quick a)(strs-quick a & more)

Converts all arguments to strings and concatenates them. Neighbouring literal strings and known constant forms will be concatenated at compile time.

strs-simple

macro

added in 1.2.36

(strs-simple)(strs-simple a)(strs-simple a & more)

Converts all arguments to strings and concatenates them with keywords being converted to strings without the : prefix. Namespaces will be removed from keywords and symbols. Neighbouring literal strings and known constant forms will be concatenated at compile time.

strspc

macro

(strspc)(strspc a)(strspc a & more)

Converts all arguments to strings and concatenates them with keywords being converted to strings without the : prefix. Neighbouring literal strings and known constant forms will be trimmed on both ends and concatenated at compile time with space characters.

For consecutive non-constant forms (like symbols) simple wrappers will be generated to ensure they are properly trimmed and separated with spaces depending on their values (only single space and empty string are detected).

strspc-simple

macro

added in 1.2.36

(strspc-simple)(strspc-simple a)(strspc-simple a & more)

Converts all arguments to strings and concatenates them with keywords being converted to strings without the : prefix. Namespaces will be removed from symbols and keywords. Neighbouring literal strings and known constant forms will be trimmed on both ends and concatenated at compile time with space characters.

For consecutive non-constant forms (like symbols) simple wrappers will be generated to ensure they are properly trimmed and separated with spaces depending on their values (only single space and empty string are detected).

strspc-squeezed

macro

(strspc-squeezed)(strspc-squeezed a)(strspc-squeezed a & more)

Converts all arguments to strings and concatenates them with keywords being converted to strings without the : prefix. Neighbouring literal strings and known constant forms will be trimmed on both ends and concatenated at compile time with space characters.

For consecutive non-constant forms (like symbols) simple wrappers will be generated to ensure they are properly trimmed and separated with spaces depending on their values (only single space and empty string are detected).

Moreover, spaces will be squeezed for detected constant forms at compile time. No squeezing will be performed at run-time.

strspc-squeezed-simple

macro

added in 1.2.36

(strspc-squeezed-simple)(strspc-squeezed-simple a)(strspc-squeezed-simple a & more)

Converts all arguments to strings and concatenates them with keywords being converted to strings without the : prefix. Namespaces will be removed from keywords and symbols. Neighbouring literal strings and known constant forms will be trimmed on both ends and concatenated at compile time with space characters.

For consecutive non-constant forms (like symbols) simple wrappers will be generated to ensure they are properly trimmed and separated with spaces depending on their values (only single space and empty string are detected).

Moreover, spaces will be squeezed for detected constant forms at compile time. No squeezing will be performed at run-time.

text-to-bytes

(text-to-bytes t)

Converts t to a byte array. Returns t unchanged when it is already a byte array, returns bzero when t is nil.

throwable?

(throwable? v)

Returns true if v is an instance of java.lang.Throwable.

to-bytes

(to-bytes obj)

Converts object to bytes by converting it to a string first with UTF-8 encoding.

to-lisp-simple-str

(to-lisp-simple-str v)

abc/ip_address –> ip-address

to-lisp-simple-str-replace-first

added in 1.2.36

(to-lisp-simple-str-replace-first v c r)

a/ipCaddress_to –> ipRaddress-to

to-lisp-slashed-str

(to-lisp-slashed-str v)

ip_address_is –> ip/address-is

to-lisp-str

(to-lisp-str v)

ip_address –> ip-address

to-lisp-str-replace-first

(to-lisp-str-replace-first v c r)

ipCaddress_to –> ipRaddress-to

to-long

(to-long s default)

Safely parses s to a long, returning default on failure. Asserts that default is an integer.

to-snake-simple-str

(to-snake-simple-str v)

abc/ip-address –> ip_address

to-snake-simple-str-replace-first

added in 1.2.36

(to-snake-simple-str-replace-first v c r)

a/ipCaddress-to –> ipRaddress_to

to-snake-slashed-str

(to-snake-slashed-str v)

ip-address-is –> ip/address_is

to-snake-str

(to-snake-str v)

ip-address –> ip_address

to-snake-str-replace-first

added in 1.2.36

(to-snake-str-replace-first v c r)

ipCaddress-to –> ipRaddress_to

to-uuid

(to-uuid)(to-uuid s)

Converts the given value to UUID. If it’s empty or nil, returns nil.

try-null

macro

added in 1.0.0

(try-null & body)

Evaluates body and if NullPointerException exception is caught it returns nil. Otherwise it returns the value of last expression in the body.

try-require

(try-require n)

Tries to require namespace n and returns the given argument. If the file does not exists, returns nil.

uuid

Parses or generates a UUID. Delegates to clojure.core/uuid when available, otherwise falls back to to-uuid.

valuable

macro

(valuable & more)

Evaluates expressions from more and returns the result of the last one only when it is valuable (not nil and not empty). Returns nil otherwise.

valuable?

(valuable? x)

Returns true if x valuable: is not nil nor empty.

when-not-empty

macro

added in 1.0.0

(when-not-empty val & body)

Evaluates body when the given value is a non-empty collection.

when-not-valuable

macro

(when-not-valuable v & more)

Evaluates expressions from more in an implicit do when v is nil or empty.

when-valuable

macro

(when-valuable v & more)

Evaluates expressions from more in an implicit do when v is not nil nor empty.

with-not-empty

added in 1.0.0

(with-not-empty obj)

Returns the collection if it’s not empty. Otherwise returns nil.