From e65cd7676f6e843e34a8501afc57f56cbc8602d3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Hannu=20Niemist=C3=B6?= Date: Wed, 25 Apr 2018 15:03:16 +0300 Subject: [PATCH] New method to Prelude: indexSet Change-Id: I06affe3386f014ae098c31416503fd941f3c7d36 --- bundles/org.simantics.scl.runtime/scl/Prelude.scl | 5 +++++ .../src/org/simantics/scl/runtime/Lists.java | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) 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) -- 2.43.2