Logo

Programming-Idioms

History of Idiom 20 > diff from v43 to v44

Edit summary for version 44 by Jerry:
New Clojure implementation by user [Jerry]

Version 43

2017-04-03, 18:36:31

Version 44

2017-09-01, 15:46:17

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
  (defn find-in-2d-matrix [m val]
    (for [i (range (count m))
          j (range (count (first m)))
          :when (= val (nth (nth m i) j))]
      [i j]))