From: Tuukka Lehtonen Date: Thu, 23 Nov 2017 22:00:21 +0000 (+0200) Subject: Added function elemIndex to SCL Prelude X-Git-Tag: v1.31.0~21 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F59%2F1259%2F1;p=simantics%2Fplatform.git Added function elemIndex to SCL Prelude refs #7639 Change-Id: I927c447e0e5a1d6102172c8d99f36c4a79e9138a --- diff --git a/bundles/org.simantics.scl.runtime/scl/Prelude.md b/bundles/org.simantics.scl.runtime/scl/Prelude.md index 36b6023c9..df91f29d1 100644 --- a/bundles/org.simantics.scl.runtime/scl/Prelude.md +++ b/bundles/org.simantics.scl.runtime/scl/Prelude.md @@ -37,7 +37,7 @@ ::class[Sequence] ::value[!, getList] -::value[elem] +::value[elem, elemIndex] ::value[filter, concatMap] ::value[foldl, foldl1, foldr] ::value[unfoldl, unfoldr] diff --git a/bundles/org.simantics.scl.runtime/scl/Prelude.scl b/bundles/org.simantics.scl.runtime/scl/Prelude.scl index 6acbf924b..c04cc8625 100644 --- a/bundles/org.simantics.scl.runtime/scl/Prelude.scl +++ b/bundles/org.simantics.scl.runtime/scl/Prelude.scl @@ -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. """