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

hydra.lib.maps.FromList 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.TypeScheme;
import hydra.dsl.Expect;
import hydra.graph.Graph;
import hydra.tools.PrimitiveFunction;
import hydra.util.Tuple;

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

import static hydra.dsl.Expect.list;
import static hydra.dsl.Types.function;
import static hydra.dsl.Types.list;
import static hydra.dsl.Types.map;
import static hydra.dsl.Types.pair;
import static hydra.dsl.Types.scheme;
import static hydra.dsl.Types.variable;

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

    @Override
    public TypeScheme type() {
        return scheme("k", "v",
                function(list(pair(variable("k"), variable("v"))), map("k", "v")));
    }

    @Override
    protected Function, Flow> implementation() {
        return args -> Flows.map(list(term -> Expect.pair(Flows::pure, Flows::pure, term), args.get(0)),
                (Function>, Term>) pairs -> new Term.Map(apply(pairs)));
    }

    /**
     * Apply the function to its single argument.
     */
    public static  Map apply(List> pairs) {
        Map mp = new HashMap<>();
        for (Tuple.Tuple2 pair : pairs) {
            mp.put(pair.object1, pair.object2);
        }
        return mp;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy