Add combinations/0 and combinations/1
This commit is contained in:
1 parent
8aecf82bfe
commit
ec7647c4ab
2 files changed
+25
No files matched your search
@@ -1486,6 +1486,16 @@ static const char* const jq_builtins[] = {
|
|||||||
"def first: .[0];",
|
"def first: .[0];",
|
||||||
"def last: .[-1];",
|
"def last: .[-1];",
|
||||||
"def nth($n): .[$n];",
|
"def nth($n): .[$n];",
|
||||||
|
"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;
|
// # transpose a possibly jagged matrix, quickly;
|
||||||
// # rows are padded with nulls so the result is always rectangular.
|
// # rows are padded with nulls so the result is always rectangular.
|
||||||
"def transpose:"
|
"def transpose:"
|
||||||
|
|||||||
@@ -1307,6 +1307,21 @@ sections:
|
|||||||
input: '["foobar", "barfoo"]'
|
input: '["foobar", "barfoo"]'
|
||||||
output: ['[false, true]']
|
output: ['[false, true]']
|
||||||
|
|
||||||
|
- title: "`combinations`, `combinations(n)`"
|
||||||
|
body: |
|
||||||
|
|
||||||
|
Outputs all combinations of the elements of the arrays in the
|
||||||
|
input array. If given an argument `n`, it outputs all combinations
|
||||||
|
of `n` repetitions of the input array.
|
||||||
|
|
||||||
|
examples:
|
||||||
|
- program: 'combinations'
|
||||||
|
input: '[[1,2], [3, 4]]'
|
||||||
|
output: ['[1, 3]', '[1, 4]', '[2, 3]', '[2, 4]']
|
||||||
|
- program: 'combinations(2)'
|
||||||
|
input: '[0, 1]'
|
||||||
|
output: ['[0, 0]', '[0, 1]', '[1, 0]', '[1, 1]']
|
||||||
|
|
||||||
- title: "`ltrimstr(str)`"
|
- title: "`ltrimstr(str)`"
|
||||||
body: |
|
body: |
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user