small medium large xlarge

Generic-user-small
03 Jul 2013, 15:22
Greg Vaughn (6 posts)

I’m on my 2nd reading of the book now, getting serious about Elixir. I’ve done a Conway’s Game of Life exercise (https://github.com/gvaughn/elixir_kata/tree/master/conway). It works, but I’m trying to refactor to learn more about the language. As such I’ve run into a situation that I don’t quite understand.

I’m trying to determine the neighbors of a cell:

n0 = fn(x,y) -> lc dx inlist [-1, 0, 1], dy inlist [-1, 0, 1], do: {x + dx, y + dy} end

is the basic idea I’m working with. That works great, but it does include the target {x,y} cell, which needs to be excluded. I got the following to work as I want via brute force:

n1 = fn(x,y) -> (lc dx inlist [-1, 0, 1], dy inlist [-1, 0, 1], do: {x + dx, y + dy}) – [{x, y}] end

but I’d like to use the lc filters. I’ve tried the following alterations:

n2 = fn(x,y) -> lc dx inlist [-1, 0, 1], dy inlist [-1, 0, 1], dx != 0, dy != 0, do: {x + dx, y + dy} end n3 = fn(x,y) -> lc dx inlist [-1, 0, 1], dy inlist [-1, 0, 1], dx != 0 and dy != 0, do: {x + dx, y + dy} end n4 = fn(x,y) -> lc dx inlist [-1, 0, 1], dy inlist [-1, 0, 1], dx != 0 && dy != 0, do: {x + dx, y + dy} end

and all of them filter out any iteration with a 0 in it, equivalent to: n5 = fn(x,y) -> lc dx inlist [-1, 1], dy inlist [-1, 1], do: {x + dx, y + dy} end

which is not what I want. Does anyone know how to implement a lc filter that acts upon both dx and dy?