doc/presentations/bts2022/jq4
raw| 1 | # Function using lambda argument. map from standard library: |
| 2 | def map(f): [.[] | f]; |
| 3 | > [1,2,3] | map(. * 2) |
| 4 | [ |
| 5 | 2, |
| 6 | 4, |
| 7 | 6 |
| 8 | ] |
| 9 | # select from standard library: |
| 10 | def select(f): if f then . else empty end; |
| 11 | > [1,2,3] | map(select(. % 2 == 0)) |
| 12 | [ |
| 13 | 2 |
| 14 | ] |
| 15 | |
| 16 | # Function using argument binding and recursion to output multiple values |
| 17 | def down($n): |
| 18 | if $n >= 0 then $n, down($n-1) |
| 19 | else empty |
| 20 | end; |