I have a two-dimensional list ll : List[List[T]] and a function f : T => Boolean. I want to find a tuple (i, j) of integers such that f(l(i)(j)) == true whenever it exists, maybe wrapped inside an Option, and would like to do this in the style of functional programming. My current working solution looks as follows:
ll.zipWithIndex
.flatMap{case (l, i) => l.map((_, i)).zipWithIndex}
.find{case ((l, _), j) => f(l(j))}
.map{case ((_, i), j) => (i, j)}
However, I feel like there should be a nicer way to achieve this - my solution is very cumbersome and doesn't generalize nicely to higher-dimensional arrays. So is there a better way to do this?
Before I go: I'm fully aware this is easily achieved with nested for-loops. So I'm only interested of solutions in the style of functional programming.
forcomprehensions are just syntactic sugar formap()andflatMap()calls. What's non-FP about that?