diff options
author | Nicolas Williams <nico@cryptonector.com> | 2014-07-28 13:18:58 -0500 |
---|---|---|
committer | Nicolas Williams <nico@cryptonector.com> | 2014-07-28 13:18:58 -0500 |
commit | e1b20b48f04a1225cd4d541500c1b00eb89a1a78 (patch) | |
tree | 1b873f10337d72076fcbc2784731ab7167155d98 /builtin.c | |
parent | 14bc1f652600892328ef0de58d09d99b6857f7ac (diff) |
Add `first`, `nth`, `last` (fix #510)
Diffstat (limited to 'builtin.c')
-rw-r--r-- | builtin.c | 6 |
1 files changed, 6 insertions, 0 deletions
@@ -986,6 +986,12 @@ static const char* const jq_builtins[] = { " if cond then ., (update | _while) else empty end; " " try _while catch if .==\"break\" then empty else . end;", "def limit(n; exp): if n < 0 then exp else foreach exp as $item ([n, null]; if .[0] < 1 then break else [.[0] -1, $item] end; .[1]) end;", + "def first(g): foreach g as $item ([false, null]; if .[0]==true then break else [true, $item] end; .[1]);", + "def last(g): reduce g as $item (null; $item);", + "def nth(n; g): if n < 0 then error(\"nth doesn't support negative indices\") else last(limit(n + 1; g)) end;", + "def first: .[0];", + "def last: .[-1];", + "def nth(n): .[n];", }; #undef LIBM_DD |