RosettaCodeData/Task/Element-wise-operations/Jq/element-wise-operations-1.jq
2017-09-25 22:28:19 +02:00

12 lines
462 B
Text

# Occurrences of .[0] in "operator" will refer to an element in self,
# and occurrences of .[1] will refer to the corresponding element in other.
def elementwise( operator; other ):
length as $rows
| if $rows == 0 then .
else . as $self
| other as $other
| ($self[0]|length) as $cols
| reduce range(0; $rows) as $i
([]; reduce range(0; $cols) as $j
(.; .[$i][$j] = ([$self[$i][$j], $other[$i][$j]] | operator) ) )
end ;