Jq (programming language)

From HandWiki - Reading time: 2 min

jq
Jq logo.png
ParadigmPurely functional programming, JSON processing oriented
Designed byStephen Dolan
First appeared2012; 12 years ago (2012)
Stable release
1.5 / August 18, 2015; 8 years ago (2015-08-18)
Typing disciplinedynamic
Websitestedolan.github.io/jq/
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




Licensed under CC BY-SA 3.0 | Source: https://handwiki.org/wiki/Jq_(programming_language)
13 views | Status: cached on September 18 2024 03:06:30
↧ Download this article as ZWI file
Encyclosphere.org EncycloReader is supported by the EncyclosphereKSF