Common list elements: Difference between revisions

Line 429:
{{out}}
<pre>[3,6,9]</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The following definition of `intersection` does not place any restrictions
on the arrays whose intersection is sought. The helper function, `ios`,
might be independently useful and so is defined as a top-level filter.
<lang jq># If a and b are sorted lists, and if all the elements respectively of a and b are distinct,
# then [a,b] | ios will emit the stream of elements in the set-intersection of a and b.
def ios:
.[0] as $a | .[1] as $b
| if 0 == ($a|length) or 0 == ($b|length) then empty
elif $a[0] == $b[0] then $a[0], ([$a[1:], $b[1:]] | ios)
elif $a[0] < $b[0] then [$a[1:], $b] | ios
else [$a, $b[1:]] | ios
end ;
 
# input: an array of arbitrary JSON arrays
# output: their intersection as sets
def intersection:
def go:
if length == 1 then (.[0]|unique)
else [(.[0]|unique), (.[1:] | go)] | [ios]
end;
if length == 0 then []
elif any(.[]; length == 0) then []
else sort_by(length) | go
end;</lang>
<lang jq># The task:
[[2,5,1,3,8,9,4,6], [3,5,6,2,9,8,4], [1,3,7,6,9]]</lang>
{{out}}
<pre>
[3,6,9]
</pre>
 
 
=={{header|Julia}}==
2,442

edits