Logo

Programming-Idioms

History of Idiom 20 > diff from v46 to v47

Edit summary for version 47 by Debaran:
New Scala implementation by user [Debaran]

Version 46

2017-12-28, 22:35:23

Version 47

2019-02-02, 03:06:18

Idiom #20 Return two values

Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.

Idiom #20 Return two values

Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.

Code
def search[T](m: Iterable[Iterable[T]], x: T): Option[(Int, Int)] = {
  for ((row, i) <- m.view.zipWithIndex; (column, j) <- row.view.zipWithIndex if column == x)
    return Some((i, j))
  None
}
Comments bubble
Note: This will refuse to work on Array