All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hydra.lib.maps.Remove Maven / Gradle / Ivy

There is a newer version: 0.8.0
Show newest version
package hydra.lib.maps;

import hydra.Flows;
import hydra.compute.Flow;
import hydra.core.Name;
import hydra.core.Term;
import hydra.core.Type;
import hydra.dsl.Expect;
import hydra.dsl.Terms;
import hydra.graph.Graph;
import hydra.tools.PrimitiveFunction;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static hydra.dsl.Types.function;
import static hydra.dsl.Types.lambda;
import static hydra.dsl.Types.map;

public class Remove extends PrimitiveFunction {
    public Name name() {
        return new Name("hydra/lib/maps.remove");
    }

    @Override
    public Type type() {
        return lambda("k", "v",
                function("k", map("k", "v"), map("k", "v")));
    }

    @Override
    protected Function, Flow> implementation() {
        return args -> {
            Term key = args.get(0);
            return Flows.map(Expect.map(Flows::pure, Flows::pure, args.get(2)),
                    before -> Terms.map(apply(key, before)));
        };
    }

    public static  Function, Map> apply(K k) {
        return before -> apply(k, before);
    }

    /**
     * Apply the function to both arguments.
     */
    public static  Map apply(K k, Map before) {
        Map after = new HashMap<>(before);
        after.remove(k);
        return after;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy