Turn clojure code to KDB+/Q code.
Not deployed to clojars yet. You should build it yourself:
The dependency:
[qwrap-clj "0.1.0-SNAPSHOT"]To build it:
lein jar
# or
boot build(require ['qwrap.core :refer :all])It provides many q-* macros similar to clojure counter-parts. The two entrypoints are:
q- convert all theq-*constructs to one KDB+/Q code string.run-q- connect to a Q server and executeq-converted string to the server. The result is converted to clojure data structures.
For example,
(q (q-let [a [1 2 3]
b [:a :b :c]]
(q-table {a a} {b b})))Generates the following Q code:
({a:(1;2;3);b:(`a;`b;`c);([a:a] b:b)}[])And
(run-q ["" 6001] (q- ...))sends q code to Q server on localhost:6001. (You starts Q process with q -p 6001)
Define a Q variable.
(q-def L [1])
; -> (enlist 1)
(q-def L [1 2 3])
; -> (1;2;3)
(q-def D {a [1 2 3]
b [:a :b :c]})
; -> ((1;2;3) ! {`a;`b;`c})Note: a literal vector is converted to Q list. a literal map is converted to Q dict.
Define in local scope.
(q-let [a [1 2 3]
b [:a :b :c]]
(q-concat a b))
; -> ({a:(1;2;3); b:(`a;`b;`c); (a , b)}[]) Define a Q function.
(q-defn my-concat [a b]
(q-concat a b))
; -> my-concat: {[a b] (a , b)}
If-else expression.
(q-if (= a b) (+ a 1) (- a 1))Multiple conditions.
(q-cond
(= a b) a
:else b)List/Dict/Table lookup. The naming convention *-in means lookup in depth (the same as in clojure)
(q-get list 1) ; normal get, -> list[1]
(q-get list [1 2 3]) ; this is item-wise get, -> [ list[1] list[2] list[3] ]
(q-get-in list [1 2 3]) ; in depth get, -> list[1][2][3]Associate value to multiple keys (or a key path).
(q-def L [[1 2] [10 20] [-10 -20])
(q-assoc L [1] [100 200]) ; normal set -> [[1 2] [100 200] [-10 -20]]
(q-assoc L [1 2] [100 200]) ; item-wise set -> [[1 2] [100 200] [100 200]]
(q-assoc-in L [1 1] 200) ; in depth set -> [ [1 2] [10 200] [-10 -20] ]Update a variable.
(q-update L [0 1] + 100) ; item-wise update with + -> [[101 102] [110 120] [-10 -20]]
(q-update-in L [0 1] + 100) ; in depth update with + -> [[1 102] [10 20] [-10 -20]]Q file construct.
(q-file "abc.csv") ; -> `:abc.csvLoad csv file.
; (q-load-csv csv-file types)
(q-load-csv (q-file "abc.csv") [:double :int :date :symbol])Save table to a file or a splayed directory.
(q-save-table tbl (q-file "abc.csv")) ; save to a file
(q-save-table tbl ; table variable
(q-file "abc/def/") ; target file or directory
:splayed? true ; splayed storage mode?
:sympath (q-file "abc/") ; symbol encoding directory (splayed mode only)
:compress? true) ; use compression?
; The above example saves to a splayed directory and encode symbols.
; -> (`:abc/def/;17;2;6) set .Q.en[`:abc/] tbl