From: Hannu Niemistö Date: Wed, 25 Apr 2018 12:03:16 +0000 (+0300) Subject: New method to Prelude: indexSet X-Git-Tag: v1.43.0~136^2~487 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F33%2F1733%2F1;p=simantics%2Fplatform.git New method to Prelude: indexSet Change-Id: I06affe3386f014ae098c31416503fd941f3c7d36 --- diff --git a/bundles/org.simantics.scl.runtime/scl/Prelude.scl b/bundles/org.simantics.scl.runtime/scl/Prelude.scl index d5d278828..32582affa 100644 --- a/bundles/org.simantics.scl.runtime/scl/Prelude.scl +++ b/bundles/org.simantics.scl.runtime/scl/Prelude.scl @@ -1965,6 +1965,11 @@ importJava "org.simantics.scl.runtime.Lists" where """ index :: [(a,b)] -> a -> Maybe b + """ + Given a list of elements, the function produces its characteristic function. + """ + indexSet :: [a] -> a -> Boolean + """ Given a list of values and a function computing a key for each value, the function produces a function that finds a value effeciently for the given key. diff --git a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/Lists.java b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/Lists.java index 5259ce631..99c19d6b9 100644 --- a/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/Lists.java +++ b/bundles/org.simantics.scl.runtime/src/org/simantics/scl/runtime/Lists.java @@ -240,7 +240,19 @@ public class Lists { } }; } - + + public static Function indexSet(List l) { + THashSet set = new THashSet(l.size()); + for(Object obj : l) + set.add(obj); + return new FunctionImpl1() { + @Override + public Object apply(Object p0) { + return set.contains(p0); + } + }; + } + public static Function indexBy(Function f, List l) { THashMap map = new THashMap(l.size()); for(Object o : l)