hydra.lib.maps.MapKeys Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hydra-java Show documentation
Show all versions of hydra-java Show documentation
The Hydra language for strongly-typed transformations
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.function.Function;
import static hydra.Flows.pure;
import static hydra.dsl.Types.function;
import static hydra.dsl.Types.lambda;
import static hydra.dsl.Types.map;
public class MapKeys extends PrimitiveFunction {
public Name name() {
return new Name("hydra/lib/maps.mapKeys");
}
@Override
public Type type() {
return lambda("k1", lambda("k2", lambda("v",
function(function("k1", "k2"), map("k1", "v"), map("k2", "v")))));
}
@Override
protected Function, Flow> implementation() {
return args -> Flows.map(
Expect.map(k -> pure(Terms.apply(args.get(0), k)), Flows::pure, args.get(1)), Terms::map);
}
public static Function, java.util.Map> apply(Function mapping) {
return (arg) -> apply(mapping, arg);
}
/**
* Apply the function to both arguments.
*/
public static java.util.Map apply(Function mapping, java.util.Map arg) {
java.util.Map result = new HashMap<>();
for (java.util.Map.Entry e : arg.entrySet()) {
result.put(mapping.apply(e.getKey()), e.getValue());
}
return result;
}
}