]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.runtime/scl/Prelude.scl
Added function elemIndex to SCL Prelude
[simantics/platform.git] / bundles / org.simantics.scl.runtime / scl / Prelude.scl
index 6acbf924b1995ea0a082b29931d689bda66b53bb..c04cc8625aa0be2bceb1ab372f8f9fe5a7eca5c4 100644 (file)
@@ -2003,6 +2003,16 @@ elemMaybe el m = match m with
     Just el2 -> el == el2
     Nothing -> False
 
+"`elemIndex el lst` returns the index of the first element in the given list `lst` which is equal (by ==) to the query element, or Nothing if there is no such element."
+elemIndex :: a -> [a] -> Maybe Integer
+elemIndex el l = loop 0
+  where
+    len = length l
+    loop i | i < len = if el == l!i
+                       then Just i
+                       else loop (i+1)
+           | otherwise = Nothing
+
 """
 Computes a list that contains only elements that belongs to both input lists.
 """