Skip to content
Jaskirat Rajasansir edited this page Apr 17, 2024 · 5 revisions

Utility Functions

This library contains a set of general utility functions.

.util.inDebugMode

We define the use of the built-in command line argument -e to also define if the process is started in debug mode or not. By using -e 1, this function will return true.

.util.system

Wraps the built-in system command to run it with protected execute. If the system command fails (i.e. the process exits with a non-zero status), this function will throw SystemCallFailedException. The error will also be logged to the console.

.util.trim

An extended version of the built-in trim function. As well as removing spaces from the beginning and end of a string, it also removes new line and tab characters.

.util.isEmpty

A null like function with support for checking empty dictionaries, empty tables and lists will all null elements. Below are some examples of how this function differs to null:

q) null 1 2 3
000b
q) .util.isEmpty 1 2 3
0b
q)null (`;0N;0Np;0Nf)
1111b
q).util.isEmpty (`;0N;0Np;0Nf)
1b
q) null ()!()
q) .util.isEmpty ()!()
1b
q) null flip `col1`col2!"SS"$\:()
col1 col2
---------
q) .util.isEmpty flip `col1`col2!"SS"$\:()
1b

.util.unenumerate

This function will unenumerate any enumerated columns in an unkeyed table.

.util.renameColumn

This function allows you to rename a column, or columns, in a table. The table should be unkeyed prior to passing into this function.

.util.getTableCounts

This function returns the number of rows in root namespace tables. Optionally, you can specify a subset of the tables to return the counts for

.util.clearTable

This function empties any root namespace table specified.

.util.findAndReplace

A standard string find and replace function. This function supports multiple "find" and "replace" values as long as there are an equal number of find and replace values.

q) .util.findAndReplace["a b.c"; " "; "/"]
"a/b.c"

/ Note in this case when replacing multiple characters with the same one,
/ the length of the 'find' and 'replace' argument must be the same
q) .util.findAndReplace["a b.c"; " ."; 2#"/"]
"a/b/c"

q) .util.findAndReplace["a b.c"; " ."; "/,"]
"a/b,c"

.util.gc

A simple wrapper function around the built-in .Q.gc function to provide logging regarding the amount of memory returned to the Operating System. The change in memory usage is returned.

q) a:til 1000000000
q) delete a from `.
q) .util.gc[]
2019.01.10 10:22:19.290 INFO pid-9258 jas 0 Running garbage collection
2019.01.10 10:22:19.892 INFO pid-9258 jas 0 Garbage collection complete [ Returned to OS (from heap): 8192 MB ] [ Time: 0D00:00:00.601529000 ]

used| 1136
heap| 8589934592
peak| 0
wmax| 0
mmap| 0
mphy| 0
syms| 0
symw| 0
Clone this wiki locally