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

hydra.lib.strings.Intercalate Maven / Gradle / Ivy

package hydra.lib.strings;

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

import static hydra.Flows.map2;
import static hydra.dsl.Types.function;
import static hydra.dsl.Types.list;
import static hydra.dsl.Types.string;

public class Intercalate extends PrimitiveFunction {
    public Name name() {
        return new Name("hydra/lib/strings.intercalate");
    }

    @Override
    public Type type() {
        return function(string(), list(string()), string());
    }

    @Override
    protected Function>, Flow, Term>> implementation() {
        return args -> map2(
                Expect.string(args.get(0)),
                Expect.list(Expect::string, args.get(1)),
                (delim, strings) -> Terms.string(Intercalate.apply(delim, strings)));
    }

    public static Function, String> apply(String delim) {
        return (strings) -> apply(delim, strings);
    }

    /**
     * Apply the function to both arguments.
     */
    public static String apply(String delim, List strings) {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (String s : strings) {
            if (first) {
                first = false;
            } else {
                sb.append(delim);
            }
            sb.append(s);
        }
        return sb.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy