jq: last preceding not-satisfying element


# made by MichaƂ Krzysztof Feiler, published: https://wiki1.mikf.pl/jq/preceding.html
def lastforeach(pred; proc):
  reduce .[] as $item ({last: null, results: []};
  {last: (if $item | pred then .last else $item end),
   results: (.results +
  if $item | pred then [({last: .last, cur: $item} | proc)] else [] end)})
  | .results ;

the function (filter) above, given:

returns an array of objects {last, cur} piped through proc, with the last being the last non-pred-satisfying element preceding the (each) pred-atisfying element cur.

To make things simpler and instead post-process (or not?) the resulting array of {last, cur}, just provide . as proc.