package mb.nabl2.util;

import java.util.Map.Entry;

import org.metaborg.util.functions.Function2;

import io.usethesource.capsule.Map;

public final class CapsuleUtil {

    private CapsuleUtil() {
    }

    public static <K, V> void replace(Map.Transient<K, V> map, Function2<K, V, V> mapper) {
        for(Entry<K, V> entry : map.entrySet()) {
            final K key = entry.getKey();
            final V val = mapper.apply(key, entry.getValue());
            if(val != null) {
                map.__put(key, val);
            } else {
                map.__remove(key);
            }
        }
    }

    public static <K, V> Map.Immutable<K, V> replace(Map.Immutable<K, V> map, Function2<K, V, V> mapper) {
        final Map.Transient<K, V> newMap = Map.Transient.of();
        for(Entry<K, V> entry : map.entrySet()) {
            final K key = entry.getKey();
            final V val = mapper.apply(key, entry.getValue());
            if(val != null) {
                newMap.__put(key, val);
            }
        }
        return newMap.freeze();
    }

}
@@ -128,16 +128,52 @@
 ule.Map;
+%0Aimport io.usethesource.capsule.Set;
 %0A%0Apublic
@@ -1094,9 +1094,381 @@
 %0A    %7D%0A%0A
+    @SuppressWarnings(%22unchecked%22) public static %3CV%3E Set.Immutable%3CV%3E toSet(Iterable%3C? extends V%3E values) %7B%0A        if(values instanceof Set.Immutable) %7B%0A            return (Set.Immutable%3CV%3E) values;%0A        %7D%0A        Set.Transient%3CV%3E set = Set.Transient.of();%0A        for(V value : values) %7B%0A            set.__insert(value);%0A        %7D%0A        return set.freeze();%0A    %7D%0A%0A
 %7D

Add method to construct Capsule set from Iterable.