Content deleted Content added
Petermsiegel (talk | contribs) |
|||
Line 122:
</syntaxhighlight>
===
====Unix pipeline====
{{main|Pipeline (Unix)}}
Line 132 ⟶ 133:
Due to the way pipelines work, it is only normally possible to pass one "argument" at a time in the form of a pair of standard input/output stream. Although extra [[file descriptor]]s can be opened from [[named pipe]]s, this no longer constitutes a point-free style.
====jq====
[[jq (programming language|jq]] is a JSON-oriented programming language in which
the '|' symbol is used to connect filters to form a pipeline
in a familiar way. For example:
[1,2] | add
evaluates to 3.
Although similar to Unix pipelines, jq pipelines allow the
incoming data to be sent to more than one recipient on the
RHS of the '|' as though in parallel. For example, the program `add/length`
will compute the average of the numbers in an array, so that:
[1,2] | add/length
evaluates to 1.5
Similarly:
[1,2] | [length, add, add/length]
evaluates to [2,3,1.5]
A dot ('.') can be used to define an attachment point on the RHS, e.g.:
1 | [., .]
evaluates to [1,1]
and similarly:
2 | pow(.; .)
evaluates to 4 since pow(x;y) is x to the power y.
=====Fibonnaci sequence======
A tacit jq program for generating the Fibonnaci sequence would be:
[0,1] | recurse( [last, add] ) | first
Here, [0,1] is the initial pair to be taken as the first two items
in the Fibonnaci sequence. (The pair [1,1] could likewise be used for
the variant definition.)
The alphabetic tokens are built-in filters: `first` and `last`
emit the first and last elements of their input arrays respectively;
and `recurse(f)` applies a filter, f, to its input recursively.
jq also allows new filters to be defined in a tacit style, e.g.:
def fib: [0,1] | recurse( [last, add] ) | first;
=====Composition of unary functions=====
In the section on Python in this article, the following Python definition is considered:
```
def example(x):
return baz(bar(foo(x)))
```
In point-free style, this could be written in Python as:
example = compose(foo, bar, baz)
In jq, the equivalent point-free definition would be:
def example: foo | bar | baz;
== See also ==
|