Skip to content

Instantly share code, notes, and snippets.

@torifat
Created December 29, 2013 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torifat/8168872 to your computer and use it in GitHub Desktop.
Save torifat/8168872 to your computer and use it in GitHub Desktop.
def findAdjWithBase(w: Int, h: Int)(r: Int, c: Int) = {
def check(x: (Int, Int)) = 0 < x._1 && x._1 <= h && 0 < x._2 && x._2 <= w
if(check((r, c))){
List((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
.map(x => (x._1 + r, x._2 + c)).filter(check)
}
}
val findAdj = findAdjWithBase(9, 9)_
findAdj(1, 1); // List((1,2), (2,1), (2,2))
findAdj(4, 4); // List((3,3), (3,4), (3,5), (4,3), (4,5), (5,3), (5,4), (5,5))
findAdj(10, 10); // Nothing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment