Given a list of numbers in x and y, what would be the simplest way to
divide x by y unless y is zero?
I came up with these and I wasn't thrilled with any of them
(3,2,4) %^:(0~:])"0 (6,0,3)
0.5 0 1.33333
(3,2,4) ]`%@.(0~:])"0 (6,0,3)
0.5 0 1.33333
It would be nice if it worked on x and y as atoms too
(5 divideExceptZero 0) -: 0
I suppose I could just replace the _ with 0 too, but that also seems
excessive
(3,2,4) ((0,]) {~ _ ~: ])@% (6,0,3)
0.5 0 1.33333
but it does work with atoms:
divideExceptZero =: ((0,]) {~ _ ~: ])@%
(5 divideExceptZero 0) -: 0
1
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm