proletariat.core

Library of the Commons. A hard-working library of common utilities.

acat

(acat s)

Fully lazy equivalent of (apply cat ...)

bytes->string

(bytes->string byte-array)

Convert a byte array to string.

characters?

(characters? x)

Returns true if x is an array of Character’s

chars?

(chars? x)

Returns true if x is an array of char’s

conj-when

(conj-when coll f x)

Conjoins x to coll per the predicate f, otherwise returns coll unchanged.

conjv

(conjv coll x)

Add element x to a vectorized version of coll and return the new vector

deep-merge

(deep-merge & maps)

Performs a deep merge on maps, resolves conflicts by choosing the last value. See deep-merge-with for custom conflict resolution.

deep-merge-with

(deep-merge-with func & maps)

Performs a deep merge on maps using func to resolve conflicts.

delete-directory!

(delete-directory! directory-path)

Deletes a directory

delete-file!

(delete-file! file-path)

Deletes a file at the path.

do-until

macro

(do-until test & body)

Executes the body until test returns true.

do-while

macro

(do-while test & body)

Executes the while test returns true.

dofor

macro

(dofor seq-exprs & body)

Behaves like a combination of clojure.core/for and clojure.core/doseq. Intended for side-effects, but will return a realized seq of the results. Has the following behaviors:

  • Not-lazy, will cause the entire seq to reside in memory at one time
  • Supports the for modifers :let, :while, :when
  • wraps body in an implicit do (unlike for)
  • returns a seq of the results from each iteration (unlike doseq)

Unlike for or doseq, provides support for Hara Events, by catching any exceptions and instead using event/raise with name :exception and map containing a single element :e with the exception that was thrown. This allows users to leverage all of Hara Event’s utilities for managing Exceptions via conditional restarts (eg: using on/continue to provide a default in the event of an exception)

flatten-map

(flatten-map m)

Takes a map, possibly nested, and ‘flattens’ it by removing any nesting and placing all keys in the root map. Any keys that refer to nested maps will be removed. Duplicate keys will be overwritten based on the order of reduce, where later keys will overwrite earlier keys.

flip

(flip f)

Returns a function with reversed arity of f

get-host-address

(get-host-address)

Return the local host IP address

get-zipper-path

(get-zipper-path loc)

Returns the path of a location in a zipper. loc should not be a node.

group-by-with

(group-by-with f bucket coll)

Like clojure.core/group-by except adds an additional parameter allowing the user to specify the collection that will be used for collecting the grouped items instead of defaulting to vector.

instant-generator

No-arg function that returns a generator for java.time.Instant

interruptable

macro

(interruptable f & body)

Wrapper for catching InterruptedException and running the provided function. The function will receive the exception as input and the response will be returned to the caller.

interruptable->ex

macro

(interruptable->ex m & body)

Wrapper for catching InterruptedException and returning an ExceptionInfo with provided map m as data.

keep-sc

(keep-sc f messages)(keep-sc f messages exp)

Works like clojure.core/keep except will short circuit if an exception is thrown by the processing function and return the list of results processed prior to the exception. Optional arity allows the user to pass in a (atom nil) which will be reset! with the exception that was thrown. Note: since map-sc is lazy, the atom will not contain the exception until the sequence is realized.

Has same performance enhancement for chunked-seq’s as clojure.core/keep but will guarantee that no elements after the exception will be processed even if in the middle of a chunk when the exception is thrown.

leap-year?

(leap-year? year)

Is the given year a leap year

map-sc

(map-sc f messages)(map-sc f messages exp)

Works like clojure.core/map except will short circuit if an exception is thrown by the processing function and return the list of results processed prior to the exception. Optional arity allows the user to pass in a (atom nil) which will be reset! with the exception that was thrown. Note: since map-sc is lazy, the atom will not contain the exception until the sequence is realized.

Has same performance enhancement for chunked-seq’s as clojure.core/map but will guarantee that no elements after the exception will be processed even if in the middle of a chunk when the exception is thrown.

named?

(named? x)

Returns true if x is a namespaced keyword (an instance of clojure.lang.Named.

now

(now)

Creates a java.time.Instant for the current time

read-edn-resource

(read-edn-resource fd)(read-edn-resource opts fd)

Works like clojure.edn/read-string except reads the EDN from a classpath resource with the given file descriptor fd instead of a passed in string.

remove-nil-values

(remove-nil-values m)

Removes elements from a map that have nil values; does not recurse

remove-nils

(remove-nils nm)

Removes elements from a (possibly nested) map that have nil values. Returns nil if all values in the map are nil Src: http://stackoverflow.com/a/29363255

retry

macro

(retry n & body)

Will execute body and return the result. If an exception is thrown, will retry n times (making total executions n + 1 times). If all retries are exhausted, the final exception is thrown.

retry*

(retry* n thunk)(retry* n thunk on-retry)(retry* n thunk on-retry on-fail)

By default, will execute thunk and return the result. If an exception is thrown, will retry n times (making total executions n + 1 times). If all retries are exhausted, the final exception is thrown. Optional arities are provided for customization.

Args

n - the number of times to retry. If < 0, will retry indefinitely thunk - no-arg function to execute and catch any exceptions for retry on-retry - (optional arity) function that is called prior to the retry when an exception is thrown, but there are still retries available. Default is a no-op. The function takes 2 arguments: first is the number of times retried (first retry will be 0, second will be 1, etc) and the second is the exception that was thrown. It is presumed that this funciton is for side effects (eg: logging) and any result will be ignored. on-fail - (optional arity) function that is called after all retries have been exhausted. The result of this function will be returned as the final result of retry*. The default is to throw the final exception. The function takes 1 argument: the final exception.

Note: if an exception is thrown from either on-retry or on-fail processing will stop immediately and that exception thrown, with no further retries.

retry-backoff

macro

(retry-backoff n b & body)

Will execute body and return the result. If an exception is thrown, will sleep for previous retries * b milliseconds and then retry up to n times (making total executions n + 1 times). The first retry will not sleep, the second will sleep for b milliseconds and subsequent retries will sleep for retries * b milliseconds. If all retries are exhausted, the final exception is thrown. Can throw InterruptedExcpetion if the processing thread is interupted during a backoff sleep.

slurp-resource

(slurp-resource fd & opts)

Works like clojure.core/slurp except it resolves the resource file descriptor fd relative to the classpath instead of the file system.

sreduce

(sreduce f acc state coll)

Works like reduce, but also allows functional state to be passed and returned between iterations. f should be a function of three arguments: the accumulator, the next value, and the state. acc is an accumulator that will be the final return value. state is the starting state of the reduction. coll is the collection to iterate.

thread-name

(thread-name)

Convenience for returning the current thread name

to-clj-instant

(to-clj-instant jtime-instant)

Creates a clojure instant literal a given java.time.Instant

uuid

(uuid)

Generate a random UUID suitable for use as a GUID

whoami

(whoami)(whoami default)

Runs whoami on the OS and returns the user that owns the JVM process. Only for Linux based systems. Optional arity as default in case of failure.

zipper-map

(zipper-map f z)

Map f over every node of the zipper z.