jq (programming language)
Paradigm | Purely functional programming, JSON processing oriented |
---|---|
Designed by | Stephen Dolan |
First appeared | 2012 |
Stable release | 1.5
/ August 18, 2015 |
Typing discipline | dynamic |
Website | stedolan |
Major implementations | |
jq | |
Influenced by | |
Icon, Haskell |
jq is a very high-level functional programming language with support for backtracking and managing streams of JSON data. It is related to the Icon and Haskell programming languages.
Basic syntax
The jq language is based on the same concepts of streams, pipes, and filters that are familiar from the Unix shell. Filters can be constructed from valid JSON expressions using a JSON-oriented syntax and are connected using the pipe character "|
". The identity filter is ".
", so that for example the expression 1 | {"a": .}
would produce the JSON value: {"a": 1}
.
Here is an example showing how to define a named, parameterized filter for formatting an integer in any base. The implementation illustrates tacit programming:
def tobase($b): def digit: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[.:.+1]; def div: (. / $b) | floor; def mod: . % $b; def r: if . < $b then digit else (div | r) + (mod | digit) end; # select(2 <= $b and $b <= 36) | r ;
The next example demonstrates the use of generators in a classical verbal arithmetic game:
def send_more_money: def choose(m;n;used): ([range(m;n+1)] - used)[]; def num(a;b;c;d): 1000*a + 100*b + 10*c + d; def num(a;b;c;d;e): 10*num(a;b;c;d) + e; label $exit | 1 as $m | 0 as $o | choose(8;9;[]) as $s | choose(2;9;[$s]) as $e | choose(2;9;[$s,$e]) as $n | choose(2;9;[$s,$e,$n]) as $d | choose(2;9;[$s,$e,$n,$d]) as $r | choose(2;9;[$s,$e,$n,$d,$r]) as $y | select(num($s;$e;$n;$d) + num($m;$o;$r;$e) == num($m;$o;$n;$e;$y)) | [$s,$e,$n,$d,$m,$o,$r,$e,$m,$o,$n,$e,$y], break $exit ;
See also
References
The jq manual and the jq wiki are the main jq references.
External links
- jq homepage
- jq source code
- The jq Programming Language page on the Rosetta Code comparative programming tasks project site
Original source: https://en.wikipedia.org/wiki/Jq (programming language).
Read more |