Evaluating Expressions
When called with --eval 'expr'
or -e 'expr'
, it executes the Egison expression.
A expression should be enclosed with single quotes ('
).
$ egison -e '(take 10 primes)'
{2 3 5 7 11 13 17 19 23 29}
Output in TSV Format
When called with --tsv
or -T
, it outputs in TSV format.
Each element of the output collection is printed in a line.
$ egison -T -e '(take 10 primes)'
2
3
5
7
11
13
17
19
23
29
When the inner elements are collections, the elements of these collections are splitted by tab.
$ egison -T -e '(map p-f (between 100 110))'
2 2 5 5
101
2 3 17
103
2 2 2 13
3 5 7
2 53
107
2 2 3 3 3
109
2 5 11
When the inner elements are tuples, the elements of these tuples are also splitted by tab.
$ egison -T -e '(zip nats primes)' | head -n 10
1 2
2 3
3 5
4 7
5 11
6 13
7 17
8 19
9 23
10 29
Handling Input in TSV Format
When called with --map
、--filter
、--substitute
, it parses input as TSV and treat them as a collection of tuples.
We explain these options in this section.
Applying a same operation to every line (--map
option)
When called with --map 'expr'
or -m 'expr'
, it applies the function specified by 'expr'
to every line.
$ seq 100 110 | egison -T -m '(lambda [$x] [x (p-f x)])'
100 2 2 5 5
101 101
102 2 3 17
103 103
104 2 2 2 13
105 3 5 7
106 2 53
107 107
108 2 2 3 3 3
109 109
110 2 5 11
Extracting lines that satisfy the predicate (--filter
option)
When called with --filter 'expr'
or -f 'expr'
, it extracts lines that satisfy the predicate specified by 'expr'
.
$ seq 1 20 | egison -f 'prime?'
2
3
5
7
11
13
17
19
Handling input as an infinite stream (--substitute
option)
When called with --substitute 'expr'
or -s 'expr'
, it treats whole input as an infinite stream and apply it to the function specified by 'expr'
.
We enumerate twin primes to demonstrate this feature in the next section.
Enumerating twin primes on a shell
We can enumerate twin primes in command line using Egison commands.
$ seq 1 100 | egison -f 'prime?' | egison -T -s '(match-all-lambda (list integer) [<join _ <cons $p <cons ,(+ p 2) _>>> [p (+ p 2)]])'
3 5
5 7
11 13
17 19
29 31
41 43
59 61
71 73
We can enumerate twin primes in one Egison expression as follow.
$ egison -T -e '(match-all primes (list integer) [<join _ <cons $p <cons ,(+ p 2) _>>> [p (+ p 2)]])' | head -n 8
3 5
5 7
11 13
17 19
29 31
41 43
59 61
71 73
Advanced Options
We can specify how to parse each element in TSV by using --field
or -F
options.
When called with -F 2c
as the following example, it treats the second element and after as one collection.
This option is designed referring the -k
option of the sort
command.
For example, when called with -F 2,4c
, it encloses the second, third, and fourth elements with {
and }
and treats them as a collection.
$ seq 10 20 | egison -T -m '(lambda [$x] [x (p-f x)])' | egison -F 2c -m 'id'
[10 {2 5}]
[11 {11}]
[12 {2 2 3}]
[13 {13}]
[14 {2 7}]
[15 {3 5}]
[16 {2 2 2 2}]
[17 {17}]
[18 {2 3 3}]
[19 {19}]
[20 {2 2 5}]
We can use s
not only c
.
When called with -F 1,1s
, the first element is enclosed with ""
and we can parse it as a string.
Additionally, when called with -F 1,3s
, the first, second, and third elements are enclosed with ""
.
$ seq 10 20 | egison -T -m '(lambda [$x] [x (p-f x)])' | egison -F 1,1s -F 2c -m 'id'
["10" {2 5}]
["11" {11}]
["12" {2 2 3}]
["13" {13}]
["14" {2 7}]
["15" {3 5}]
["16" {2 2 2 2}]
["17" {17}]
["18" {2 3 3}]
["19" {19}]
["20" {2 2 5}]