Equilibrium index: Difference between revisions
→{{header|jq}}: use foreach; now also works with jaq
m (→{{header|Quackery}}: tidied up) |
(→{{header|jq}}: use foreach; now also works with jaq) |
||
Line 1,518:
=={{header|jq}}==
{{works with | jq}}
''Also works with gojq, the Go implementation of jq, and jaq''
▲The top-level function is defined as a 0-arity filter that emits answers as a stream, as is idiomatic in jq.
<syntaxhighlight lang="jq"># The index origin is 0 in jq.
def equilibrium_indices:
. as $in
| add as $add
| if 0 == $add - .[0]
foreach range(1;length) as $i (
[0, .[0],
$in[$i] as $x |
</syntaxhighlight>
'''Example 1:'''
<syntaxhighlight lang="jq">[-7, 1, 5, 2, -4, 3, 0] | equilibrium_indices</syntaxhighlight>
|