Is there a more efficient, idiomatic method of finding the nearest
(absolute difference) x to y?

nearest=:([ {~ {.@/: @: |@:-)

    1 5 8 9 nearest 4
5

     1 5 8 9 nearest 6
5

    1 5 8 9 nearest 5
5

    1 5 8 9 nearest 2
1

This seems to work fine but was curious if there is a more common solution
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to