-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
jq has several "inverse" function-pairs such as in/has and inside/contains, where the input and argument are reversed. However defining pairs like this for every single function is not scalable; and currently one has to do a lot of manual variable binding which gets tedious.
It would be good to supply a higher order function flip where . | flip(f, $a, [..]) means the same as . as $orig | $a | f($orig, [..]).
Real world example:
$ ip -j -6 addr show primary scope global | jq -r '
.[] .addr_info[] .local //empty
|select(. as $i |any("fd", "fc"; . as $p |$i |startswith($p)) |not)'
2001:blah blah blah
would be cleaner written as
$ ip -j -6 addr show primary scope global | jq -r '
.[] .addr_info[] .local //empty
|select(. as $i |any("fd", "fc"; flip(startswith, $i) |not)'
2001:blah blah blah
(Yes, in this specific example I could just use regex, but I'm sure there are lots of other possible cases where such a concise alternative isn't available.)
Broadly speaking, flip is quite a core concept in FP language theory, and is implemented in core libraries of all major FP languages including Haskell, Ocaml, etc, etc. Since jq is heavily inspired by these concepts, it would be fitting to add flip.