RosettaCodeData/Task/Element-wise-operations/Jq/element-wise-operations-4.jq
2023-07-01 13:44:08 -04:00

23 lines
718 B
Text

def elementwise2( operator; other ):
def pow(i): . as $in | reduce range(0;i) as $i (1; .*$in);
def operation(x; op; y):
[x,y] | op as $op
| if $op == "+" then x+y
elif $op == "-" then x-y
elif $op == "*" then x*y
elif $op == "/" then x/y
elif $op == "%" then x%y
elif $op == "//" then x/y|floor
elif $op == "**" or $op == "^" or $op == "pow" then x|pow(y)
else $op
end;
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] = operation($self[$i][$j]; operator; $other[$i][$j] ) ) )
end;