I am confused about the following line:
Seq<String> s1 = seq.zip(split, Function::apply);
In this snippet:
static String underscoreToCamel(String str) {
UnaryOperator<String> capitalize = s -> s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
Seq<UnaryOperator<String>> seq = c -> {
c.accept(String::toLowerCase);
while (true) {
c.accept(capitalize);
}
};
List<String> split = Arrays.asList(str.split("_"));
Seq<String> s1 = seq.zip(split, Function::apply);
String a = s1.join("");
return a;
}
public interface Seq<T> {
void consume(Consumer<T> consumer);
static <T> Seq<T> unit(T t) {
return consumer -> consumer.accept(t);
}
default <E> Seq<E> map(Function<T, E> function) {
return consumer -> consume(t -> consumer.accept(function.apply(t)));
}
default <E> Seq<E> flatMap(Function<T, Seq<E>> function) {
return consumer -> consume(t -> function.apply(t).consume(consumer));
}
default String join(String sep) {
StringJoiner joiner = new StringJoiner(sep);
consume(t -> joiner.add(t.toString()));
return joiner.toString();
}
static <T> T stop() {
throw StopException.INSTANCE;
}
default void consumeTillStop(Consumer<T> consumer) {
try {
consume(consumer);
} catch (StopException ignore) {}
}
default <U, R> Seq<R> zip(Iterable<U> iterable, BiFunction<T, U, R> function) {
return c -> {
Iterator<U> iterator = iterable.iterator();
consumeTillStop(t -> {
if (iterator.hasNext()) {
c.accept(function.apply(t, iterator.next()));
} else {
stop();
}
});
};
}
}
I do understand that Function::apply is a method reference and that the method wants a BiFunction<T, U, R>. But I do not get how this is compatible.
What exactly does it resolve to? Why can I supply Function::apply in this case?
Function.apply()is non-static1-parameter method, so it needs an instance to run; butFunction::applyis not providing such instance, so needs an additional argument when called, meaning it is equivalent to a 2-parameter function (first argument will bethis) ||BiFunction<T, U, R> function = Function::apply~=(T t, U u) -> t.apply(u)returning typeR( if I am correct, in this specific case:(Consumer<String> t, String u) -> t.apply(s))