Utility and helper functions
Utility and helper functions
(add-shutdown-hook f)
Adds a shutdown hook that will be executed when the JVM is shutdown. The supplied function f will be called when the JVM shuts down. Please note: when using multiple hooks, there is no guarantee with respect to the order in which the hooks will be executed.
Adds a shutdown hook that will be executed when the JVM is shutdown. The supplied function f will be called when the JVM shuts down. Please note: when using multiple hooks, there is no guarantee with respect to the order in which the hooks will be executed.
(byte-seq-to-int byte-seq)
Convert the byte sequence byte-seq to an integer.
Convert the byte sequence byte-seq to an integer.
(change-int-in-byte-vector v n f)
In the given vector v, change the byte representation of an integer by applying function f.
In the given vector v, change the byte representation of an integer by applying function f.
(classname o)
Get classname without leading package names of the given object o.
Get classname without leading package names of the given object o.
(compress-object-to-byte-array obj)
(compress-object-to-byte-array obj alg)
Write an object into a byte array and compress it. A newly allocated byte array with the serialized and compressed object is returned. The compression algorithm can be specified via the second parameter. Currently GZIP (:gzip) and ZIP (:zip) are supported. The default is GZIP.
Write an object into a byte array and compress it. A newly allocated byte array with the serialized and compressed object is returned. The compression algorithm can be specified via the second parameter. Currently GZIP (:gzip) and ZIP (:zip) are supported. The default is GZIP.
(convert-from-clojure-to-java input)
Converts the given Clojure specific data structure (list, map, set, vector) into the equivalent "pure" Java data structure. The mapping is as follows: list and vector -> ArrayList, map -> HashMap, set -> HashSet. Nested data structures will be converted recursively.
Converts the given Clojure specific data structure (list, map, set, vector) into the equivalent "pure" Java data structure. The mapping is as follows: list and vector -> ArrayList, map -> HashMap, set -> HashSet. Nested data structures will be converted recursively.
(counter)
(counter init)
Convenience function for creating a more powerful counter. The created counter allows to have arbitrary single-argument functions passed for manipulating the internal value. When the created counter is called with no argument the current value is returned. Example: (use 'clj-assorted-utils.util) (let [cntr (counter 123)] (println (cntr)) (cntr inc) (cntr dec) (cntr #(- % 123)) (cntr))
Convenience function for creating a more powerful counter. The created counter allows to have arbitrary single-argument functions passed for manipulating the internal value. When the created counter is called with no argument the current value is returned. Example: (use 'clj-assorted-utils.util) (let [cntr (counter 123)] (println (cntr)) (cntr inc) (cntr dec) (cntr #(- % 123)) (cntr))
(create-string-to-file-output out-file)
(create-string-to-file-output out-file arg-map)
(delay-eval d & body)
Evaluates the supplied body with the given delay 'd' ([ms]).
Evaluates the supplied body with the given delay 'd' ([ms]).
(delta-counter)
Creates a counter for calculating deltas. The returned delta counter function expects two arguments, a keyword and a numerical value. When called for the first time the initial value will be associated to an internal counter and zero is returned. When called subsequently with the same keyword, the difference between the current value and the stored value is calculated and this delta is returned. The internal counter is then set to the new value. Example: (let [cntr (counter) delta-cntr (delta-counter)] (= 0 (delta-cntr :cntr (cntr))) (cntr inc) (cntr inc) (cntr inc) (= 3 (delta-cntr :cntr (cntr))) (= 0 (delta-cntr :cntr (cntr))))
Creates a counter for calculating deltas. The returned delta counter function expects two arguments, a keyword and a numerical value. When called for the first time the initial value will be associated to an internal counter and zero is returned. When called subsequently with the same keyword, the difference between the current value and the stored value is calculated and this delta is returned. The internal counter is then set to the new value. Example: (let [cntr (counter) delta-cntr (delta-counter)] (= 0 (delta-cntr :cntr (cntr))) (cntr inc) (cntr inc) (cntr inc) (= 3 (delta-cntr :cntr (cntr))) (= 0 (delta-cntr :cntr (cntr))))
(dir-exists? d)
Returns true if f exists and is a directory.
Returns true if f exists and is a directory.
(exec-blocking cmd)
Execute the given command and block until execution has finished.
Execute the given command and block until execution has finished.
(exec-with-out cmd stdout-fn)
(exec-with-out cmd stdout-fn stderr-fn)
Execute the given command and process the output written to stdout with stdout-fn. stdout-fn is run in an own thread. The data written to stdout is passed to stdout-fn line-by-line. When a value of nil is read processing stops and the process in which cmd was executed is destroyed.
Execute the given command and process the output written to stdout with stdout-fn. stdout-fn is run in an own thread. The data written to stdout is passed to stdout-fn line-by-line. When a value of nil is read processing stops and the process in which cmd was executed is destroyed.
(executor)
Create an executor for executing fns in an own thread.
Create an executor for executing fns in an own thread.
(exists? f)
Returns true if f exists in the filesystem.
Returns true if f exists in the filesystem.
(file-exists? f)
Returns true if f exists and is a file.
Returns true if f exists and is a file.
(await-flag this)
Block the current thread until the flag was set.
Block the current thread until the flag was set.
(flag-set? this)
Test if flag had been set. Returns true if flag was set and false otherwise.
Test if flag had been set. Returns true if flag was set and false otherwise.
(set-flag this)
Set flag to true.
Set flag to true.
(fn-name f)
Get the name of the given fn f.
Get the name of the given fn f.
(get-defn-arglists f)
Get the arglists of a function that was defined with defn. This function is intended to be used from within a macor. For a macro version of this functionality please use get-defn-arglists-m.
Get the arglists of a function that was defined with defn. This function is intended to be used from within a macor. For a macro version of this functionality please use get-defn-arglists-m.
(get-defn-arglists-m f)
Macro version of get-defn-arglists.
Macro version of get-defn-arglists.
(get-fn-arglists f)
Returns a map that contains the actual fn that was passed as argument and its arglists. The result will be a map of form: {:fn f :args <f-args>}. This function is intended to be used from within a macor. For a macro version of this functionality please use get-fn-arglists-m.
Returns a map that contains the actual fn that was passed as argument and its arglists. The result will be a map of form: {:fn f :args <f-args>}. This function is intended to be used from within a macor. For a macro version of this functionality please use get-fn-arglists-m.
(get-fn-arglists-m f)
Macro version of get-fn-arglists.
Macro version of get-fn-arglists.
(get-int-from-byte-vector v n)
From given vector of bytes v get the integer starting at offset n. Starting at offset n the next 4 bytes will be converted to an integer.
From given vector of bytes v get the integer starting at offset n. Starting at offset n the next 4 bytes will be converted to an integer.
(get-system-property p)
Get system property p. p is a String naming the property go get.
Get system property p. p is a String naming the property go get.
(get-with-default m k d)
Get a value for the given key k from a map m and if the value does not exist return the default d.
Get a value for the given key k from a map m and if the value does not exist return the default d.
(inc-counter c)
Increment the given simple counter c.
Increment the given simple counter c.
(int-to-byte-vector val)
Convert the given integer val to a vector of 4 bytes.
Convert the given integer val to a vector of 4 bytes.
(is-dir? d)
Returns true if f is a directory.
Returns true if f is a directory.
(is-file? f)
Returns true if f is a file.
Returns true if f is a file.
(mkdir d)
Create directory d and if required all parent directories. This is equivalent to 'mkdir -p d' on Linux systems.
Create directory d and if required all parent directories. This is equivalent to 'mkdir -p d' on Linux systems.
(object-to-byte-array obj)
Write the given object into a byte array. A newly allocated byte arry with the serialized object is returned.
Write the given object into a byte array. A newly allocated byte arry with the serialized object is returned.
(prepare-counter)
(prepare-counter init)
Prepare a simple counter. Use @ to access the value.
Prepare a simple counter. Use @ to access the value.
(prepare-flag)
(prepare-flag n)
Prepare a flag with default value false.
Prepare a flag with default value false.
(print-err-ln & s)
Deprecated! Use println-err instead.
Deprecated! Use println-err instead.
(process-input-stream-line-by-line input-stream function)
Process the given input stream line-by-line. The data read from the stream is passed to the given function. The function is executed in an own thread.
Process the given input stream line-by-line. The data read from the stream is passed to the given function. The function is executed in an own thread.
(process-line-by-line location f)
Process input from location line-by-line. Each line is passed to f. The processing is done with a clojure reader. So all locations supported by reader are automatically supported as well.
Process input from location line-by-line. Each line is passed to f. The processing is done with a clojure reader. So all locations supported by reader are automatically supported as well.
(reduce-selected-map-entries m f selected-keys)
Reduce the values of all selected keys of a map m by applying the function f.
Reduce the values of all selected keys of a map m by applying the function f.
(rmdir d)
Delete d if d is an empty directory.
Delete d if d is an empty directory.
(run-once exec f d)
(run-once exec f d tu)
Run f using executor exec once with delay d. Optionally a time unit tu can be given. tu defaults to TimeUnit/MILLISECONDS.
Run f using executor exec once with delay d. Optionally a time unit tu can be given. tu defaults to TimeUnit/MILLISECONDS.
(run-repeat exec f d)
(run-repeat exec f id d)
(run-repeat exec f id d tu)
Run f repeatedly using executor exec with delay d. Optionally an initial delay, id, and a time unit tu can be given. Time unit is a static member of TimeUnit, e.g., TimeUnit/SECONDS and defaults to TimeUnit/MILLISECONDS.
Run f repeatedly using executor exec with delay d. Optionally an initial delay, id, and a time unit tu can be given. Time unit is a static member of TimeUnit, e.g., TimeUnit/SECONDS and defaults to TimeUnit/MILLISECONDS.
(shutdown-now exec)
Force executor shut down.
Force executor shut down.
(sleep ms)
Sleep for the given number of milliseconds.
Sleep for the given number of milliseconds.
(stringify-keyword k)
If a keyword is passed returns the name of the keyword. Else the input is left unchanged.
If a keyword is passed returns the name of the keyword. Else the input is left unchanged.
(stringify-map m)
Convert all keywords in a map to their respective names. This, essentially, is an extended version of clojure.walk/stringify-keys, which only converts the keys to strings.
Convert _all_ keywords in a map to their respective names. This, essentially, is an extended version of clojure.walk/stringify-keys, which only converts the keys to strings.
(touch f)
If f does not exist, create it.
If f does not exist, create it.
(vec-replace v n delta)
In given vector v replace the content of v starting at index n with delta.
In given vector v replace the content of v starting at index n with delta.
(with-eo-str & body)
Macro that executes body capturing its stdout and stderr output. This returns a map with the following structure: {:all both-stdout-and-std-err-output-in-proper-order :stderr stderr-output :stdout stdout-output}
Macro that executes body capturing its stdout and stderr output. This returns a map with the following structure: {:all both-stdout-and-std-err-output-in-proper-order :stderr stderr-output :stdout stdout-output}
(with-err-str & body)
Analog to with-out-str, just for err: https://clojuredocs.org/clojure.core/with-out-str
Analog to with-out-str, just for *err*: https://clojuredocs.org/clojure.core/with-out-str
(with-err-str-cb cb-fn & body)
Callback version of with-err-str. This version executes the function cb-fn for every element that was added to the writer. cb-fn is executed after the element was added to the writer.
Callback version of with-err-str. This version executes the function cb-fn for every element that was added to the writer. cb-fn is executed after the element was added to the writer.
(with-err-str-custom write-fn & body)
Customizable version of with-err-str. This version executes the function write-fn for every element that is added to the writer. The element that is added to the writer is the string representation of the return value of write-fn.
Customizable version of with-err-str. This version executes the function write-fn for every element that is added to the writer. The element that is added to the writer is the string representation of the return value of write-fn.
(with-out-str-cb cb-fn & body)
Callback version of with-out-str: https://clojuredocs.org/clojure.core/with-out-str This version executes the function cb-fn for every element that was added to the writer. cb-fn is executed after the element was added to the writer.
Callback version of with-out-str: https://clojuredocs.org/clojure.core/with-out-str This version executes the function cb-fn for every element that was added to the writer. cb-fn is executed after the element was added to the writer.
(with-out-str-custom write-fn & body)
Customizable version of with-out-str: https://clojuredocs.org/clojure.core/with-out-str This version executes the function write-fn for every element that is added to the writer. The element that is added to the writer is the string representation of the return value of write-fn.
Customizable version of with-out-str: https://clojuredocs.org/clojure.core/with-out-str This version executes the function write-fn for every element that is added to the writer. The element that is added to the writer is the string representation of the return value of write-fn.
(xml-string-to-map xml-str)
Takes an XML definition in form of a string and outputs the corresponding map.
Takes an XML definition in form of a string and outputs the corresponding map.
(xml-string-to-map-stringified xml-str)
Convert the XML string xml-str to a map with all keywords converted to strings.
Convert the XML string xml-str to a map with all keywords converted to strings.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close