by vmchale on 5/10/24, 5:09 PM with 12 comments
There's a guide here! https://vmchale.github.io/jacinda/
by jdp on 5/11/24, 11:34 AM
The colon being used in multiple contexts is tricky. As I was scanning the examples I found postfix `:` doing type conversion like in `(%)\. {%/Apple/}{`3:}` and then I was wondering what it does when it has nothing on its left-hand side, like in `[(+)|0 [:1"x]`. Then I noticed that the [ were unbalanced in the latter example, and eventually figured out that `[:` is its own operator separate from `:` and the middle `[` had nothing to do with function syntax.
by dan-robertson on 5/11/24, 11:40 AM
When I think about how I use awk, I think it’s mostly something like:
awk '!a[$2]++' # first occurrence of each value in the second field
Or awk '{a[$2]+=$3} END {for(x in a) print x, a[x]}'
Or just as an advanced version of cut. A fourth example is something that is annoying to do in a streaming way but easy with bash: compute moving average of second field grouped by third field over span of size 20 (backwards) in first field. awk '{ print $1, $3, 1, $2; print $1+20, $3, -1, -$2}' | sort -n | awk '{ a[$2]+=$3; b[$2]+=$4; print $1, $2, b[$2]/a[$2] }'
The above all feel somewhat functional as computations – the first is a folding filter, the second a fold, the third a map, and the fourth is a folding concat map if done on-line or a concat map followed and a folding map as written.The awk features that feel ‘non-functional’ to me are less the mutation and more operations like next, or the lack of compositionality: one can’t write an awk program that is, in some sense made of several awk programs (i.e sets of pattern–expr rules) joined together. That compositionality is the main advantage, in my opinion, of the ‘functional’ jq, which feels somewhat awk-adjacent. Is there some way to get composition of ja programs without falling back to byte streams in between?
by gleenn on 5/11/24, 2:13 AM
by asicsp on 5/11/24, 3:22 AM