summaryrefslogtreecommitdiffstats
path: root/builtin.c
diff options
context:
space:
mode:
authorSantiago Lapresta <santiago@typeform.com>2015-06-04 18:30:38 +0200
committerNicolas Williams <nico@cryptonector.com>2015-06-23 10:59:58 -0500
commitec7647c4ab1c50d7f45c8ff9fd73917af867e0be (patch)
tree6ea8e6b55a9e99bdc3cde57881300945d9ca84d5 /builtin.c
parent8aecf82bfef6647315eb96e69eac3f65ce38ee70 (diff)
Add combinations/0 and combinations/1
Diffstat (limited to 'builtin.c')
-rw-r--r--builtin.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/builtin.c b/builtin.c
index 7cc2b958..7cb402a0 100644
--- a/builtin.c
+++ b/builtin.c
@@ -1486,7 +1486,17 @@ static const char* const jq_builtins[] = {
"def first: .[0];",
"def last: .[-1];",
"def nth($n): .[$n];",
- // # transpose a possibly jagged matrix, quickly;
+ "def combinations:"
+ " if length == 0 then [] else"
+ " .[0][] as $x"
+ " | (.[1:] | combinations) as $y"
+ " | [$x] + $y"
+ " end;",
+ "def combinations(n):"
+ " . as $dot"
+ " | [range(n) | $dot]"
+ " | combinations;",
+ // # transpose a possibly jagged matrix, quickly;
// # rows are padded with nulls so the result is always rectangular.
"def transpose:"
" if . == [] then []"