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

hydra.lib.lists.Nub Maven / Gradle / Ivy

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

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.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;

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

public class Nub extends PrimitiveFunction {
    public Name name() {
        return new Name("hydra/lib/lists.nub");
    }

    @Override
    public Type type() {
        return lambda("a", function(list("a"), list("a")));
    }

    @Override
    protected Function, Flow> implementation() {
      return args -> Flows.map(Expect.list(Flows::pure, args.get(0)), l -> Terms.list(apply(l)));
    }

    /**
     * Apply the function to the single argument.
     */
    public static  List apply(List arg) {
        Set visited = new HashSet<>();
        List result = new ArrayList<>(arg.size());
        for (X x : arg) {
            if (!visited.contains(x)) {
                visited.add(x);
                result.add(x);
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy