1. Dmitry Grebeniuk
  2. cadastr

Commits

Dmitry Grebeniuk  committed 8e3b81d

List: + findi_{exn,opt}

  • Participants
  • Parent commits b4ef44e
  • Branches default

Comments (0)

Files changed (1)

File src/cd_List.ml

View file
  • Ignore whitespace
       [ Not_found -> None ]
     ;
 
+    value rec findi_loop pred i lst =
+      match lst with
+      [ [] -> raise Not_found
+      | [h :: t] ->
+          if pred h
+          then (h, i)
+          else findi_loop pred (i + 1) t
+      ]
+    ;
+
+    (* returns [(elem, index)], where [elem] is the first [lst]'s element
+       where [pred elem = True], and [index] is its index, counting from 0.
+       Raises [Not_found] when no elements match [pred].
+     *)
+    value findi_exn pred lst =
+      findi_loop pred 0 lst
+    ;
+
+    value findi_opt pred lst =
+      try Some (findi_exn pred lst) with [Not_found -> None]
+    ;
+
     value rec drop_while pred lst =
       match lst with
       [ [] -> []