Bringing Home The Row Number

This solution came from one of my Stackoverflow. I wanted to find the row number where a value match took place.

The solution accepts a sequence of sequences, rows, a comparision value, val, and a index in which to perform the compare, col-idx.

(defn ret-match-row
"Searches a sequences of sequences -- rows -- at a specific column col-idx,
and returns the index of the first match and then stops."

[rows val col-idx]

(loop [[row & rows] rows
pos 0]
(not row) nil
(= val (nth row col-idx)) pos
:not-found (recur rows (inc pos)))))

(ret-match-row [[44 2 3 4 5][1 6 7 8 10][11 12 13 14 15]] 11 0)
--> 2

Leave a comment

Filed under Clojure, Functional Programming

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s