Write a function that returns an iterable object of all valid points 4-directionally adjacent to (x, y) A very common need in algorithms classes and computer science in general is to iterate 4-directionally over a grid or matrix (such as in BFS or DFS). This seems to often result in a lot of clunky and verbose code with a lot of arithmetic and comparisons within loops. I've seen many different approaches to this, but I can't shake the feeling that there's a more concise way to do this.

The challenge is to write a pure function that, given the width and height of a finite plane n, m originating at point (0,0), and coordinates (x,y) that can represent any valid point within that plane, returns an iterable object of all points within the plane that are 4-directionally adjacent to (x,y).

The goal is to define that function in as few bytes as possible.

Some examples to help illustrate valid input/output:

n = 5 (y-axis), m = 3 (x-axis) (zero-based)

matrix = [
    [A, B, C],
    [D, E, F],
    [G, H, I],
    [J, K, L],
    [M, N, O],
]

(x, y) =&gt; [valid iterable points]

E: (1, 1) =&gt; [(1, 0), (2, 1), (1, 2), (0, 1)]
A: (0, 0) =&gt; [(1, 0), (0, 1)]
L: (2, 3) =&gt; [(2, 2), (2, 4), (1, 3)]
N: (1, 4) =&gt; [(1, 3), (2, 4), (0, 4)]


n = 1 (y-axis), m = 1 (x-axis) (zero-based)

matrix = [
    [A],
]

(x, y) =&gt; [valid iterable points]

A: (0, 0) =&gt; []


n = 2 (y-axis), m = 1 (x-axis) (zero-based)

matrix = [
    [A],
    [B],
]

(x, y) =&gt; [valid iterable points]

A: (0, 0) =&gt; [(0, 1)]
B: (0, 1) =&gt; [(0, 0)]


And here's an example (this one in Python) of a function that satisfies the conditions:

def four_directions(x, y, n, m):
    valid_coordinates = []
    for xd, yd in [(1, 0), (0, 1), (-1, 0), (0, -1)]:
        nx, ny = x + xd, y + yd
        if 0 &lt;= nx &lt; m and 0 &lt;= ny &lt; n:
            valid_coordinates.append((nx, ny))
    return valid_coordinates


The example above defined a named function, but anonymous functions are also acceptable.

The inputs n, m, x, y are all unsigned 32-bit integers within the following ranges:

n &gt; 0
m &gt; 0
0 &lt;= x &lt; m
0 &lt;= y &lt; n


The output must take the form of an iterable (however your language of choice defines that) of (x, y) pairs.

Additional clarifications:

Complex numbers (and other representations/serializations) are OK as long as the consumer of the iterable can access x and y as integers knowing only their location.

Non-zero-based indexes are acceptable, but only if the language of choice is a non-zero-indexed language. If the language uses a mix of numbering systems, default to the numbering system of the data structure most commonly used to represent a matrix. If these are still all foreign concepts in the given language, any starting index is acceptable.
Python 2, 66 bytes



lambda m,n,x,y:[(x-1,y),(x+1,y)][~x:m-x]+[(x,y-1),(x,y+1)][~y:n-y]


Try it online!

Lists the four neighbors, then uses list slicing to remove those that are out-of-bounds.



Python 2, 71 bytes



lambda m,n,x,y:[(k/n,k%n)for k in range(m*n)if(k/n-x)**2+(k%n-y)**2==1]


Try it online!

Instead of checking which of the four neighbors are in-bounds, we do it the slower way of checking all in-bounds points for those that are neighbors, that is have Euclidian distance exactly 1 from (x,y). We also use the classic div-mod trick to iterate over a grid, saving the need to write two loops like for i in range(m)for j in range(n).

I tried using complex arithmetic to write the distance condition, but it turned out longer to write abs((k/n-x)*1j+k%n-y)==1.



Python 2, 70 bytes



lambda m,n,x,y:[(x+t/3,y+t%3-1)for t in-2,0,2,4if m&gt;x+t/3&gt;=0&lt;y+t%3&lt;=n]


Try it online!