From 46ea0d713406fbd0a0d8a8e5f41b7fb8ea7001c3 Mon Sep 17 00:00:00 2001 From: Tuukka Lehtonen Date: Fri, 24 Nov 2017 00:00:21 +0200 Subject: [PATCH] Added function elemIndex to SCL Prelude refs #7639 Change-Id: I927c447e0e5a1d6102172c8d99f36c4a79e9138a --- bundles/org.simantics.scl.runtime/scl/Prelude.md | 2 +- bundles/org.simantics.scl.runtime/scl/Prelude.scl | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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. """ -- 2.47.1