In relpipe-tr-scheme
we can generate new records – not only modify records from the input.
There is --has-more-records
option which – if evaluated as true – says: „read one more record from the Scheme context and call me again“.
We can also suppress all original records by --where '#f'
.
And we can also change the structure of the relation (see previous examples).
Thus we can iterate through a relation but completely replace its structure and content.
What it is good for? We can do aggregations – we can count records, compute sum, maximum, minimum or average value etc.
#!/bin/bash
# argument: directory path
# prints file count and sum of file sizes
find "$1" -type f -print0 \
| relpipe-in-filesystem \
--file path \
--file size \
| relpipe-tr-scheme \
--relation 'f.*' \
--output-attribute 'count' integer \
--output-attribute 'sum' integer \
--before-records '
(define $sum 0)
(define $count 0)
(define return-sum #f)' \
--for-each '
(set! $sum (+ $sum $size) )
(set! $count (+ $count 1 ) )' \
--where '#f' \
--after-records '(set! return-sum #t)' \
--has-more-records '
(if return-sum
(begin (set! return-sum #f) #t)
#f
)' \
| relpipe-out-tabular
Usage example:
$ ./guile-file-count-size-sum.sh /usr/share/icons/oxygen/
filesystem:
╭─────────────────┬───────────────╮
│ count (integer) │ sum (integer) │
├─────────────────┼───────────────┤
│ 6260 │ 31091700 │
╰─────────────────┴───────────────╯
Record count: 1
In SQL, the same result can be achieved by:
SELECT
count(*) AS count,
sum(size) AS sum
FROM filesystem;
This should be possible with relpipe-tr-sql
in later versions.
SQL is much more declarative and for many cases a better tool.
In SQL we describe „how the result should look like“ instead of „how the result should be produced step by step“.
One day, there might also be a translator that parses SQL code and generates Scheme code, so we could have advantages of both worlds a) concise and declarative syntax of SQL and b) streaming – which means no need for putting all the data in the RAM or on the disk.
Relational pipes, open standard and free software © 2018-2022 GlobalCode