com.github.tonivade.purefun.Applicable Maven / Gradle / Ivy
/*
* Copyright (c) 2018-2021, Antonio Gabriel Muñoz Conejo
* Distributed under the terms of the MIT License
*/
package com.github.tonivade.purefun;
import static com.github.tonivade.purefun.Function2.first;
import static com.github.tonivade.purefun.Function2.second;
public interface Applicable extends Mappable {
@Override
Applicable map(Function1 super A, ? extends R> mapper);
Applicable ap(Kind> apply);
default Applicable> zip(Kind other) {
return mapN(narrowK(this), narrowK(other), Tuple::of);
}
default Applicable zipLeft(Kind other) {
return mapN(narrowK(this), narrowK(other), first());
}
default Applicable zipRight(Kind other) {
return mapN(narrowK(this), narrowK(other), second());
}
default Applicable zipWith(Kind other, Function2 super A, ? super B, ? extends R> mapper) {
return mapN(narrowK(this), narrowK(other), mapper);
}
static Applicable mapN(Applicable fa, Applicable fb,
Function2 super A, ? super B, ? extends C> mapper) {
return fb.ap(fa.map(mapper.curried()));
}
static Applicable mapN(
Applicable fa,
Applicable fb,
Applicable fc,
Function3 super A, ? super B, ? super C, ? extends D> mapper) {
return fc.ap(mapN(fa, fb, (a, b) -> mapper.curried().apply(a).apply(b)));
}
static Applicable mapN(
Applicable fa,
Applicable fb,
Applicable fc,
Applicable fd,
Function4 super A, ? super B, ? super C, ? super D, ? extends E> mapper) {
return fd.ap(mapN(fa, fb, fc, (a, b, c) -> mapper.curried().apply(a).apply(b).apply(c)));
}
static Applicable mapN(
Applicable fa,
Applicable fb,
Applicable fc,
Applicable fd,
Applicable fe,
Function5 super A, ? super B, ? super C, ? super D, ? super E, ? extends R> mapper) {
return fe.ap(mapN(fa, fb, fc, fd, (a, b, c, d) -> mapper.curried().apply(a).apply(b).apply(c).apply(d)));
}
@SuppressWarnings("unchecked")
static Applicable narrowK(Kind kind) {
return (Applicable) kind;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy