summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-07-28 13:18:58 -0500
committerNicolas Williams <nico@cryptonector.com>2014-07-28 13:18:58 -0500
commite1b20b48f04a1225cd4d541500c1b00eb89a1a78 (patch)
tree1b873f10337d72076fcbc2784731ab7167155d98 /builtin.c
parent14bc1f652600892328ef0de58d09d99b6857f7ac (diff)
Add `first`, `nth`, `last` (fix #510)
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c6
1 files changed, 6 insertions, 0 deletions
diff --git a/builtin.c b/builtin.c
index 0af44163..dea5fbbf 100644
--- a/builtin.c
+++ b/builtin.c
@@ -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