File: more-examples.sh
   1 #!/bin/sh
   2 
   3 # more-examples.sh
   4 #
   5 # Here are several command-line examples using `gt` (short for `gotron`,
   6 # which in turn is short for `GO TRansform jsON`).
   7 #
   8 # All commands should run on a linux-style shell as they are: you can even
   9 # run this file as a shell script, if all goes well.
  10 
  11 
  12 # handle help options
  13 case "$1" in
  14     -h|--h|-help|--help)
  15         awk '/^# +examples\.sh/, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  16         exit 0
  17     ;;
  18 esac
  19 
  20 
  21 # find out how long 4GB last when playing 16-bit PCM sounds for various
  22 # settings; results then turned from seconds into HH:MM:SS-style strings
  23 gt '// 16-bit/2-byte samples, mono or stereo, for both CD-audio and DAT
  24     _{44_100, 48_000} * _{mono: 1, stereo: 2} * 2 &
  25 
  26     // find how many seconds 4 GB last with those settings, playback-wise
  27     (4 * gb) / _ &
  28 
  29     // turn seconds into HH:MM:SS.SS-style strings
  30     _ . dive(a(floor(v / 3600), floor(v % 3600 / 60), fix(v % 60, 2)) ^ `:`)
  31 ' .
  32 
  33 # get recent `live` exchange rates from the bank of canada, transforming
  34 # values from the latest record; the trick at the end of the URI is to
  35 # limit data-transfer size, by asking for records only for the current year
  36 gt 'define(
  37         latest, _.observations[-1],
  38         rates, latest.drop(`d`),
  39     ) &
  40 
  41     // improve keys/names by removing everything around the 3-letter
  42     // currency codes, then pick `v` fields, parsing them into numbers
  43     rates . each(k - `FX` - `CAD`, +v.v | v) &
  44 
  45     // rename field `d` as `date`, and use all entries from the previous
  46     // step, sorted by their currency-code labels
  47     merge(_{ date: latest.d }, _.sort(k))
  48 ' https://www.bankofcanada.ca/valet/observations/group/FX_RATES_DAILY/json?start_date=`date +%Y`-01-01