diff --git a/control/src/main/java/com/github/tonivade/purefun/control/Control.java b/control/src/main/java/com/github/tonivade/purefun/control/Control.java index 6067da0de..82a9f0b29 100644 --- a/control/src/main/java/com/github/tonivade/purefun/control/Control.java +++ b/control/src/main/java/com/github/tonivade/purefun/control/Control.java @@ -15,7 +15,7 @@ import com.github.tonivade.purefun.core.Tuple2; @HigherKind -public sealed interface Control extends ControlOf, Bindable { +public sealed interface Control extends ControlOf, Bindable, T> { Result apply(MetaCont cont); @@ -29,12 +29,12 @@ default Control map(Function1 mapper) { } @Override - default Control flatMap(Function1> mapper) { + default Control flatMap(Function1, ? extends R>> mapper) { return new FlatMapped<>(Control.this, mapper); } @Override - default Control andThen(Kind next) { + default Control andThen(Kind, ? extends R> next) { return flatMap(ignore -> next); } @@ -85,9 +85,9 @@ public Result apply(MetaCont cont) { final class FlatMapped implements Control { private final Control self; - private final Function1> outer; + private final Function1, ? extends R>> outer; - private FlatMapped(Control self, Function1> outer) { + private FlatMapped(Control self, Function1, ? extends R>> outer) { this.self = checkNonNull(self); this.outer = checkNonNull(outer); } @@ -225,7 +225,7 @@ public Control map(Function1 mapper) { @Override @SuppressWarnings("unchecked") - public Control flatMap(Function1> mapper) { + public Control flatMap(Function1, ? extends R>> mapper) { return (Control) this; } } diff --git a/control/src/main/java/com/github/tonivade/purefun/control/MetaCont.java b/control/src/main/java/com/github/tonivade/purefun/control/MetaCont.java index 43fafd535..f0428e6fb 100644 --- a/control/src/main/java/com/github/tonivade/purefun/control/MetaCont.java +++ b/control/src/main/java/com/github/tonivade/purefun/control/MetaCont.java @@ -27,7 +27,7 @@ default MetaCont map(Function1 mapper) { } @SuppressWarnings({"unchecked", "rawtypes"}) - default MetaCont flatMap(Function1> mapper) { + default MetaCont flatMap(Function1, ? extends A>> mapper) { Function1> f = (Function1) mapper; return new Frames<>(NonEmptyList.of(f), this); } @@ -114,7 +114,7 @@ public Tuple2, MetaCont> splitAt(Marker.Cont cont) { @Override @SuppressWarnings({"unchecked", "rawtypes"}) - public MetaCont flatMap(Function1> mapper) { + public MetaCont flatMap(Function1, ? extends A>> mapper) { NonEmptyList>> list = NonEmptyList.of((Function1) mapper); return new Frames<>(list.appendAll(frames), tail); } diff --git a/control/src/test/java/com/github/tonivade/purefun/control/ControlTest.java b/control/src/test/java/com/github/tonivade/purefun/control/ControlTest.java index 1d0b6b9d5..714e84cb4 100644 --- a/control/src/test/java/com/github/tonivade/purefun/control/ControlTest.java +++ b/control/src/test/java/com/github/tonivade/purefun/control/ControlTest.java @@ -54,7 +54,7 @@ private Control program(Amb amb) { } private Control program(State state, Amb amb) { - return Instances.monad(Control_.class).use() + return Instances.>monad().use() .then(state.get()) .flatMap(x -> amb.flip().flatMap(b -> b ? state.set(x + 1) : pure(unit()))) .then(state.get()) diff --git a/control/src/test/java/com/github/tonivade/purefun/control/PipingExample.java b/control/src/test/java/com/github/tonivade/purefun/control/PipingExample.java index 6c5a516bc..dedc1cb6e 100644 --- a/control/src/test/java/com/github/tonivade/purefun/control/PipingExample.java +++ b/control/src/test/java/com/github/tonivade/purefun/control/PipingExample.java @@ -76,9 +76,9 @@ static abstract class Process extends Stateful>, } } - static final class Down extends Process implements Receive { + static final class Down extends Process, Receive> implements Receive { - Down(Kind> init) { + Down(Kind, Control> init) { super(init); } @@ -91,9 +91,9 @@ public Control receive() { } } - static final class Up extends Process implements Send { + static final class Up extends Process, Send> implements Send { - Up(Kind> init) { + Up(Kind, Control> init) { super(init); } @@ -110,13 +110,13 @@ public Control send(int n) { @HigherKind final class Prod implements ProdOf { - private final Function1, R>> apply; + private final Function1, R>, R>> apply; - Prod(Function1, R>> apply) { + Prod(Function1, R>, R>> apply) { this.apply = requireNonNull(apply); } - public Function1, R> apply(Unit unit) { + public Function1, R>, R> apply(Unit unit) { return apply.apply(unit); } } @@ -124,13 +124,13 @@ public Function1, R> apply(Unit unit) { @HigherKind final class Cons implements ConsOf { - private final Function1, R>> apply; + private final Function1, R>, R>> apply; - Cons(Function1, R>> apply) { + Cons(Function1, R>, R>> apply) { this.apply = requireNonNull(apply); } - public Function1, R> apply(int n) { + public Function1, R>, R> apply(int n) { return apply.apply(n); } } diff --git a/control/src/test/java/com/github/tonivade/purefun/control/TwitterExample.java b/control/src/test/java/com/github/tonivade/purefun/control/TwitterExample.java index c9c8af1c4..3ca0d81fb 100644 --- a/control/src/test/java/com/github/tonivade/purefun/control/TwitterExample.java +++ b/control/src/test/java/com/github/tonivade/purefun/control/TwitterExample.java @@ -37,7 +37,7 @@ static Control instanceOf(Function1> apply) { } private Control> program(Twitter twitter) { - return Instances.monad().use() + return Instances.>monad().use() .then(twitter.userTweets("12345")) .then(twitter.userTweets("54321")) .apply(ImmutableList::appendAll) diff --git a/core/src/main/java/com/github/tonivade/purefun/concurrent/Future.java b/core/src/main/java/com/github/tonivade/purefun/concurrent/Future.java index f7e8f6c63..49b3fb1d5 100644 --- a/core/src/main/java/com/github/tonivade/purefun/concurrent/Future.java +++ b/core/src/main/java/com/github/tonivade/purefun/concurrent/Future.java @@ -67,7 +67,7 @@ * @see Promise */ @HigherKind -public sealed interface Future extends FutureOf, Bindable { +public sealed interface Future extends FutureOf, Bindable, T> { Executor DEFAULT_EXECUTOR = Executors.newVirtualThreadPerTaskExecutor(); @@ -88,7 +88,7 @@ public sealed interface Future extends FutureOf, Bindable { Future mapError(Function1 mapper); @Override - Future flatMap(Function1> mapper); + Future flatMap(Function1, ? extends R>> mapper); Future andThen(Future next); @@ -352,7 +352,7 @@ public Future mapError(Function1 mapp } @Override - public Future flatMap(Function1> mapper) { + public Future flatMap(Function1, ? extends R>> mapper) { return chain(value -> value.fold(e -> Future.failure(executor, e), mapper)); } @@ -419,7 +419,7 @@ private Future transform(Function1, ? extends Tr promise.onComplete(value -> p.tryComplete(mapper.apply(value))), this::cancel); } - private Future chain(Function1, ? extends Kind> mapper) { + private Future chain(Function1, ? extends Kind, ? extends R>> mapper) { checkNonNull(executor); checkNonNull(mapper); return new FutureImpl<>(executor, diff --git a/core/src/main/java/com/github/tonivade/purefun/concurrent/Par.java b/core/src/main/java/com/github/tonivade/purefun/concurrent/Par.java index 066ab6b73..369721539 100644 --- a/core/src/main/java/com/github/tonivade/purefun/concurrent/Par.java +++ b/core/src/main/java/com/github/tonivade/purefun/concurrent/Par.java @@ -28,7 +28,7 @@ @HigherKind @FunctionalInterface -public non-sealed interface Par extends ParOf, Bindable { +public non-sealed interface Par extends ParOf, Bindable, T> { Future apply(Executor executor); @@ -46,7 +46,7 @@ default Par map(Function1 mapper) { } @Override - default Par flatMap(Function1> mapper) { + default Par flatMap(Function1, ? extends R>> mapper) { return executor -> apply(executor).flatMap(value -> mapper.andThen(ParOf::narrowK).apply(value).apply(executor)); } @@ -104,7 +104,7 @@ static Par task(Producer producer) { return executor -> Future.task(executor, producer); } - static Par defer(Producer> producer) { + static Par defer(Producer, ? extends T>> producer) { return executor -> { Producer> andThen = producer.andThen(ParOf::narrowK); return andThen.get().apply(executor); diff --git a/core/src/main/java/com/github/tonivade/purefun/concurrent/Promise.java b/core/src/main/java/com/github/tonivade/purefun/concurrent/Promise.java index 94c4e52e4..097eb4c9b 100644 --- a/core/src/main/java/com/github/tonivade/purefun/concurrent/Promise.java +++ b/core/src/main/java/com/github/tonivade/purefun/concurrent/Promise.java @@ -35,7 +35,7 @@ import com.github.tonivade.purefun.type.TryOf; @HigherKind -public sealed interface Promise extends PromiseOf, Bindable, Applicable permits PromiseImpl { +public sealed interface Promise extends PromiseOf, Bindable, T>, Applicable, T> permits PromiseImpl { boolean tryComplete(Try value); @@ -71,15 +71,15 @@ default Promise onFailure(Consumer1 consumer) { Promise map(Function1 mapper); @Override - Promise ap(Kind> apply); + Promise ap(Kind, ? extends Function1> apply); @Override - default Promise andThen(Kind next) { + default Promise andThen(Kind, ? extends R> next) { return PromiseOf.narrowK(Bindable.super.andThen(next)); } @Override - Promise flatMap(Function1> mapper); + Promise flatMap(Function1, ? extends R>> mapper); default Promise then(Consumer1 next) { return map(next.asFunction()); @@ -236,7 +236,7 @@ public Promise onComplete(Consumer1> consumer) { } @Override - public Promise ap(Kind> apply) { + public Promise ap(Kind, ? extends Function1> apply) { Promise result = new PromiseImpl<>(executor); onComplete(try1 -> PromiseOf.narrowK(apply).onComplete( try2 -> result.tryComplete(Try.map2(try2, try1, Function1::apply)))); @@ -251,7 +251,7 @@ public Promise map(Function1 mapper) { } @Override - public Promise flatMap(Function1> mapper) { + public Promise flatMap(Function1, ? extends R>> mapper) { Promise other = new PromiseImpl<>(executor); onComplete(value -> { Try> map = value.map(mapper.andThen(PromiseOf::narrowK)); diff --git a/core/src/main/java/com/github/tonivade/purefun/data/ImmutableArray.java b/core/src/main/java/com/github/tonivade/purefun/data/ImmutableArray.java index 34d701c9c..7aa3df6fb 100644 --- a/core/src/main/java/com/github/tonivade/purefun/data/ImmutableArray.java +++ b/core/src/main/java/com/github/tonivade/purefun/data/ImmutableArray.java @@ -61,7 +61,7 @@ default ImmutableArray map(Function1 mapper) { } @Override - default ImmutableArray flatMap(Function1> mapper) { + default ImmutableArray flatMap(Function1, ? extends R>> mapper) { return ImmutableArray.from(stream().flatMap(mapper.andThen(SequenceOf::narrowK).andThen(Sequence::stream)::apply)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/data/ImmutableList.java b/core/src/main/java/com/github/tonivade/purefun/data/ImmutableList.java index f1b958636..346ba69be 100644 --- a/core/src/main/java/com/github/tonivade/purefun/data/ImmutableList.java +++ b/core/src/main/java/com/github/tonivade/purefun/data/ImmutableList.java @@ -66,7 +66,7 @@ default ImmutableList map(Function1 mapper) { } @Override - default ImmutableList flatMap(Function1> mapper) { + default ImmutableList flatMap(Function1, ? extends R>> mapper) { return ImmutableList.from(stream().flatMap(mapper.andThen(SequenceOf::narrowK).andThen(Sequence::stream)::apply)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/data/ImmutableSet.java b/core/src/main/java/com/github/tonivade/purefun/data/ImmutableSet.java index 7525cdeb6..f33908d9f 100644 --- a/core/src/main/java/com/github/tonivade/purefun/data/ImmutableSet.java +++ b/core/src/main/java/com/github/tonivade/purefun/data/ImmutableSet.java @@ -56,7 +56,7 @@ default ImmutableSet map(Function1 mapper) { } @Override - default ImmutableSet flatMap(Function1> mapper) { + default ImmutableSet flatMap(Function1, ? extends R>> mapper) { return ImmutableSet.from(stream().flatMap(mapper.andThen(SequenceOf::narrowK).andThen(Sequence::stream)::apply)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/data/ImmutableTree.java b/core/src/main/java/com/github/tonivade/purefun/data/ImmutableTree.java index 456088740..161773b7c 100644 --- a/core/src/main/java/com/github/tonivade/purefun/data/ImmutableTree.java +++ b/core/src/main/java/com/github/tonivade/purefun/data/ImmutableTree.java @@ -78,11 +78,11 @@ default ImmutableTree map(Comparator comparator, Function1 ImmutableTree flatMap(Function1> mapper) { + default ImmutableTree flatMap(Function1, ? extends R>> mapper) { return ImmutableTree.from(naturalOrder(), stream().flatMap(mapper.andThen(SequenceOf::narrowK).andThen(Sequence::stream)::apply)); } - default ImmutableTree flatMap(Comparator comparator, Function1> mapper) { + default ImmutableTree flatMap(Comparator comparator, Function1, ? extends R>> mapper) { return ImmutableTree.from(comparator, stream().flatMap(mapper.andThen(SequenceOf::narrowK).andThen(Sequence::stream)::apply)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/data/NonEmptyList.java b/core/src/main/java/com/github/tonivade/purefun/data/NonEmptyList.java index 82eb4148f..ac100f501 100644 --- a/core/src/main/java/com/github/tonivade/purefun/data/NonEmptyList.java +++ b/core/src/main/java/com/github/tonivade/purefun/data/NonEmptyList.java @@ -40,7 +40,7 @@ public NonEmptyList map(Function1 mapper) { } @Override - public NonEmptyList flatMap(Function1> mapper) { + public NonEmptyList flatMap(Function1, ? extends R>> mapper) { return of(value.flatMap(mapper)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/data/Sequence.java b/core/src/main/java/com/github/tonivade/purefun/data/Sequence.java index 83248acd4..801015909 100644 --- a/core/src/main/java/com/github/tonivade/purefun/data/Sequence.java +++ b/core/src/main/java/com/github/tonivade/purefun/data/Sequence.java @@ -33,7 +33,7 @@ import com.github.tonivade.purefun.type.Option; @HigherKind -public non-sealed interface Sequence extends SequenceOf, Iterable, Bindable { +public non-sealed interface Sequence extends SequenceOf, Iterable, Bindable, E> { int size(); @@ -59,7 +59,7 @@ default boolean containsAll(Iterable elements) { Sequence map(Function1 mapper); @Override - Sequence flatMap(Function1> mapper); + Sequence flatMap(Function1, ? extends R>> mapper); Sequence filter(Matcher1 matcher); diff --git a/core/src/main/java/com/github/tonivade/purefun/type/Either.java b/core/src/main/java/com/github/tonivade/purefun/type/Either.java index a1f1bc222..c2302017f 100644 --- a/core/src/main/java/com/github/tonivade/purefun/type/Either.java +++ b/core/src/main/java/com/github/tonivade/purefun/type/Either.java @@ -39,7 +39,7 @@ * @param type of the right value, positive case */ @HigherKind -public sealed interface Either extends EitherOf, Bindable, R>, Applicable, R> { +public sealed interface Either extends EitherOf, Bindable, L>, R>, Applicable, L>, R> { static Either left(L value) { return new Left<>(value); @@ -108,7 +108,7 @@ default Either map(Function1 map) { @Override default Either ap( - Kind, ? extends Function1> apply) { + Kind, L>, ? extends Function1> apply) { return apply.fix(toEither()).flatMap(this::map); } @@ -118,7 +118,7 @@ default Either mapLeft(Function1 map) { @Override @SuppressWarnings("unchecked") - default Either flatMap(Function1, ? extends T>> map) { + default Either flatMap(Function1, L>, ? extends T>> map) { if (this instanceof Right(var right)) { return map.andThen(EitherOf::narrowK).apply(right); } @@ -144,7 +144,7 @@ default Option> filterNot(Matcher1 matcher) { return filter(matcher.negate()); } - default Either filterOrElse(Matcher1 matcher, Producer, R>> orElse) { + default Either filterOrElse(Matcher1 matcher, Producer, L>, R>> orElse) { if (this instanceof Left) { return this; } @@ -154,14 +154,14 @@ default Either filterOrElse(Matcher1 matcher, Producer or(Producer, R>> orElse) { + default Either or(Producer, L>, R>> orElse) { if (this instanceof Left) { return orElse.andThen(EitherOf::narrowK).get(); } return this; } - default Either orElse(Kind, R> orElse) { + default Either orElse(Kind, L>, R> orElse) { return or(Producer.cons(orElse)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/type/Eval.java b/core/src/main/java/com/github/tonivade/purefun/type/Eval.java index 23b133d8d..7cf99451e 100644 --- a/core/src/main/java/com/github/tonivade/purefun/type/Eval.java +++ b/core/src/main/java/com/github/tonivade/purefun/type/Eval.java @@ -30,7 +30,7 @@ * @param result of the computation */ @HigherKind -public sealed interface Eval extends EvalOf, Bindable { +public sealed interface Eval extends EvalOf, Bindable, A> { Eval TRUE = now(true); Eval FALSE = now(false); @@ -46,7 +46,7 @@ default Eval map(Function1 map) { } @Override - Eval flatMap(Function1> map); + Eval flatMap(Function1, ? extends R>> map); static Eval now(T value) { return new Done<>(cons(value)); @@ -60,7 +60,7 @@ static Eval always(Producer always) { return new Done<>(always); } - static Eval defer(Producer> eval) { + static Eval defer(Producer, ? extends T>> eval) { return new Defer<>(eval); } @@ -82,7 +82,7 @@ public A value() { } @Override - public Eval flatMap(Function1> map) { + public Eval flatMap(Function1, ? extends R>> map) { return new FlatMapped<>(cons(this), map::apply); } @@ -94,9 +94,9 @@ public String toString() { final class Defer implements Eval { - private final Producer> deferred; + private final Producer, ? extends A>> deferred; - private Defer(Producer> deferred) { + private Defer(Producer, ? extends A>> deferred) { this.deferred = checkNonNull(deferred); } @@ -106,7 +106,7 @@ public A value() { } @Override - public Eval flatMap(Function1> map) { + public Eval flatMap(Function1, ? extends R>> map) { return new FlatMapped<>(deferred::get, map::apply); } @@ -123,10 +123,10 @@ public String toString() { final class FlatMapped implements Eval { - private final Producer> start; - private final Function1> run; + private final Producer, ? extends A>> start; + private final Function1, ? extends B>> run; - private FlatMapped(Producer> start, Function1> run) { + private FlatMapped(Producer, ? extends A>> start, Function1, ? extends B>> run) { this.start = checkNonNull(start); this.run = checkNonNull(run); } @@ -137,7 +137,7 @@ public B value() { } @Override - public Eval flatMap(Function1> map) { + public Eval flatMap(Function1, ? extends R>> map) { return new FlatMapped<>(this::start, b -> new FlatMapped<>(() -> run(b), map::apply)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/type/Id.java b/core/src/main/java/com/github/tonivade/purefun/type/Id.java index 4fe0dbc0b..c4d5365d2 100644 --- a/core/src/main/java/com/github/tonivade/purefun/type/Id.java +++ b/core/src/main/java/com/github/tonivade/purefun/type/Id.java @@ -22,7 +22,7 @@ * @param the wrapped value */ @HigherKind -public record Id(T value) implements IdOf, Bindable, Applicable, Serializable { +public record Id(T value) implements IdOf, Bindable, T>, Applicable, T>, Serializable { public Id { checkNonNull(value); @@ -34,12 +34,12 @@ public Id map(Function1 map) { } @Override - public Id ap(Kind> apply) { + public Id ap(Kind, ? extends Function1> apply) { return apply.fix(toId()).flatMap(this::map); } @Override - public Id flatMap(Function1> map) { + public Id flatMap(Function1, ? extends R>> map) { return map.andThen(IdOf::narrowK).apply(value); } diff --git a/core/src/main/java/com/github/tonivade/purefun/type/Option.java b/core/src/main/java/com/github/tonivade/purefun/type/Option.java index b20836a7a..8b7780e1d 100644 --- a/core/src/main/java/com/github/tonivade/purefun/type/Option.java +++ b/core/src/main/java/com/github/tonivade/purefun/type/Option.java @@ -39,7 +39,7 @@ * @param the wrapped value */ @HigherKind -public sealed interface Option extends OptionOf, Bindable, Applicable { +public sealed interface Option extends OptionOf, Bindable, T>, Applicable, T> { static Option some(T value) { return new Some<>(value); @@ -78,12 +78,12 @@ default Option map(Function1 mapper) { } @Override - default Option ap(Kind> apply) { + default Option ap(Kind, ? extends Function1> apply) { return apply.fix(toOption()).flatMap(this::map); } @Override - default Option flatMap(Function1> map) { + default Option flatMap(Function1, ? extends R>> map) { return switch (this) { case Some(var value) -> map.andThen(OptionOf::narrowK).apply(value); case None n -> none(); @@ -115,14 +115,14 @@ default Option filterNot(Matcher1 matcher) { return filter(matcher.negate()); } - default Option or(Producer> orElse) { + default Option or(Producer, T>> orElse) { if (this instanceof None) { return orElse.andThen(OptionOf::narrowK).get(); } return this; } - default Option orElse(Kind orElse) { + default Option orElse(Kind, T> orElse) { return or(cons(orElse)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/type/Try.java b/core/src/main/java/com/github/tonivade/purefun/type/Try.java index 07d86f987..42d8481fe 100644 --- a/core/src/main/java/com/github/tonivade/purefun/type/Try.java +++ b/core/src/main/java/com/github/tonivade/purefun/type/Try.java @@ -41,7 +41,7 @@ * @param the wrapped value */ @HigherKind -public sealed interface Try extends TryOf, Bindable, Applicable { +public sealed interface Try extends TryOf, Bindable, T>, Applicable, T> { static Try success(T value) { return new Success<>(value); @@ -122,7 +122,7 @@ default Try map(Function1 mapper) { } @Override - default Try ap(Kind> apply) { + default Try ap(Kind, ? extends Function1> apply) { return apply.fix(toTry()).flatMap(this::map); } @@ -135,7 +135,7 @@ default Try mapError(Function1 mapper @Override @SuppressWarnings("unchecked") - default Try flatMap(Function1> mapper) { + default Try flatMap(Function1, ? extends R>> mapper) { return switch (this) { case Success(var value) -> mapper.andThen(TryOf::narrowK).apply(value); case Failure f -> (Try) this; @@ -179,7 +179,7 @@ default Try filterNot(Matcher1 matcher) { return filter(matcher.negate()); } - default Try filterOrElse(Matcher1 matcher, Producer> producer) { + default Try filterOrElse(Matcher1 matcher, Producer, ? extends T>> producer) { if (this instanceof Failure) { return this; } @@ -196,14 +196,14 @@ default U fold(Function1 failureMapper, Func }; } - default Try or(Producer> orElse) { + default Try or(Producer, T>> orElse) { if (this instanceof Failure) { return orElse.andThen(TryOf::narrowK).get(); } return this; } - default Try orElse(Kind orElse) { + default Try orElse(Kind, T> orElse) { return or(Producer.cons(orElse)); } diff --git a/core/src/main/java/com/github/tonivade/purefun/type/Validation.java b/core/src/main/java/com/github/tonivade/purefun/type/Validation.java index 9245b9d5e..ef65b9999 100644 --- a/core/src/main/java/com/github/tonivade/purefun/type/Validation.java +++ b/core/src/main/java/com/github/tonivade/purefun/type/Validation.java @@ -49,7 +49,7 @@ * @param type of the value when valid */ @HigherKind -public sealed interface Validation extends ValidationOf, Bindable, T> { +public sealed interface Validation extends ValidationOf, Bindable, E>, T> { static Validation valid(T value) { return new Valid<>(value); @@ -105,7 +105,7 @@ default Validation bimap(Function1 error, @Override @SuppressWarnings("unchecked") - default Validation flatMap(Function1, ? extends R>> mapper) { + default Validation flatMap(Function1, E>, ? extends R>> mapper) { if (this instanceof Valid(var value)) { return mapper.andThen(ValidationOf::narrowK).apply(value); } @@ -126,7 +126,7 @@ default Option> filterNot(Matcher1 matcher) { return filter(matcher.negate()); } - default Validation filterOrElse(Matcher1 matcher, Producer, T>> orElse) { + default Validation filterOrElse(Matcher1 matcher, Producer, E>, T>> orElse) { if (this instanceof Invalid) { return this; } @@ -136,14 +136,14 @@ default Validation filterOrElse(Matcher1 matcher, Producer or(Producer, T>> orElse) { + default Validation or(Producer, E>, T>> orElse) { if (this instanceof Invalid) { return orElse.andThen(ValidationOf::narrowK).get(); } return this; } - default Validation orElse(Kind, T> orElse) { + default Validation orElse(Kind, E>, T> orElse) { return or(Producer.cons(orElse)); } diff --git a/effect/src/main/java/com/github/tonivade/purefun/effect/EIO.java b/effect/src/main/java/com/github/tonivade/purefun/effect/EIO.java index 36e4759e5..ca33ce9c9 100644 --- a/effect/src/main/java/com/github/tonivade/purefun/effect/EIO.java +++ b/effect/src/main/java/com/github/tonivade/purefun/effect/EIO.java @@ -34,7 +34,7 @@ import com.github.tonivade.purefun.typeclasses.FunctionK; @HigherKind -public final class EIO implements EIOOf, Effect, A> { +public final class EIO implements EIOOf, Effect, E>, A> { private static final EIO UNIT = new EIO<>(PureIO.unit()); @@ -75,7 +75,7 @@ public EIO map(Function1 map) { } @Override - public EIO flatMap(Function1, ? extends B>> map) { + public EIO flatMap(Function1, E>, ? extends B>> map) { return new EIO<>(instance.flatMap(value -> { EIO apply = map.andThen(EIOOf::narrowK).apply(value); return apply.instance; @@ -83,12 +83,12 @@ public EIO flatMap(Function1, } @Override - public EIO andThen(Kind, ? extends B> next) { + public EIO andThen(Kind, E>, ? extends B> next) { return new EIO<>(instance.andThen(next.fix(EIOOf.toEIO()).instance)); } @Override - public EIO ap(Kind, ? extends Function1> apply) { + public EIO ap(Kind, E>, ? extends Function1> apply) { return new EIO<>(instance.ap(apply.fix(EIOOf.toEIO()).toPureIO())); } @@ -100,7 +100,7 @@ public EIO mapError(Function1 map) { return new EIO<>(instance.mapError(map)); } - public EIO flatMapError(Function1, ? extends A>> map) { + public EIO flatMapError(Function1, F>, ? extends A>> map) { return new EIO<>(instance.flatMapError(error -> { EIO apply = map.andThen(EIOOf::narrowK).apply(error); return apply.instance; @@ -112,8 +112,8 @@ public EIO bimap(Function1 mapError, Functi } public EIO foldM( - Function1, ? extends B>> mapError, - Function1, ? extends B>> map) { + Function1, F>, ? extends B>> mapError, + Function1, F>, ? extends B>> map) { return new EIO<>(instance.foldM( error -> mapError.andThen(EIOOf::narrowK).apply(error).instance, value -> map.andThen(EIOOf::narrowK).apply(value).instance)); @@ -132,30 +132,30 @@ public EIO orElse(EIO other) { } @Override - public EIO> zip(Kind, ? extends B> other) { + public EIO> zip(Kind, E>, ? extends B> other) { return zipWith(other, Tuple::of); } @Override - public EIO zipLeft(Kind, ? extends B> other) { + public EIO zipLeft(Kind, E>, ? extends B> other) { return zipWith(other, first()); } @Override - public EIO zipRight(Kind, ? extends B> other) { + public EIO zipRight(Kind, E>, ? extends B> other) { return zipWith(other, second()); } @Override - public EIO zipWith(Kind, ? extends B> other, + public EIO zipWith(Kind, E>, ? extends B> other, Function2 mapper) { return parMap2(this, other.fix(EIOOf.toEIO()), mapper); } - public EIO, A>> fork() { + public EIO, E>, A>> fork() { return new EIO<>(instance.fork().map(f -> f.mapK(new FunctionK<>() { @Override - public EIO apply(Kind, E>, ? extends T> from) { + public EIO apply(Kind, Void>, E>, ? extends T> from) { return new EIO<>(from.fix(PureIOOf::narrowK)); } }))); @@ -225,39 +225,39 @@ public EIO refineOrDie(Class type) { return new EIO<>(instance.refineOrDie(type)); } - public static EIO parMap2(Kind, ? extends A> za, Kind, ? extends B> zb, + public static EIO parMap2(Kind, E>, ? extends A> za, Kind, E>, ? extends B> zb, Function2 mapper) { return parMap2(Future.DEFAULT_EXECUTOR, za, zb, mapper); } - public static EIO parMap2(Executor executor, Kind, ? extends A> za, Kind, ? extends B> zb, + public static EIO parMap2(Executor executor, Kind, E>, ? extends A> za, Kind, E>, ? extends B> zb, Function2 mapper) { return new EIO<>(PureIO.parMap2(executor, za.fix(EIOOf::narrowK).instance, zb.fix(EIOOf::narrowK).instance, mapper)); } - public static EIO> race(Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static EIO> race(Kind, E>, ? extends A> fa, Kind, E>, ? extends B> fb) { return race(Future.DEFAULT_EXECUTOR, fa, fb); } - public static EIO> race(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static EIO> race(Executor executor, Kind, E>, ? extends A> fa, Kind, E>, ? extends B> fb) { return racePair(executor, fa, fb).flatMap(either -> either.fold( ta -> ta.get2().cancel().fix(EIOOf.toEIO()).map(x -> Either.left(ta.get1())), tb -> tb.get1().cancel().fix(EIOOf.toEIO()).map(x -> Either.right(tb.get2())))); } - public static EIO, B>>, Tuple2, A>, B>>> - racePair(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static EIO, E>, B>>, Tuple2, E>, A>, B>>> + racePair(Executor executor, Kind, E>, ? extends A> fa, Kind, E>, ? extends B> fb) { PureIO instance1 = fa.fix(EIOOf.toEIO()).instance.fix(PureIOOf::narrowK); PureIO instance2 = fb.fix(EIOOf.toEIO()).instance.fix(PureIOOf::narrowK); return new EIO<>(PureIO.racePair(executor, instance1, instance2).map( either -> either.bimap(a -> a.map2(f -> f.mapK(new FunctionK<>() { @Override - public EIO apply(Kind, E>, ? extends T> from) { + public EIO apply(Kind, Void>, E>, ? extends T> from) { return new EIO<>(from.fix(PureIOOf::narrowK)); } })), b -> b.map1(f -> f.mapK(new FunctionK<>() { @Override - public EIO apply(Kind, E>, ? extends T> from) { + public EIO apply(Kind, Void>, E>, ? extends T> from) { return new EIO<>(from.fix(PureIOOf::narrowK)); } }))))); @@ -323,7 +323,7 @@ public static EIO pure(A value) { return new EIO<>(PureIO.pure(value)); } - public static EIO defer(Producer, ? extends A>> lazy) { + public static EIO defer(Producer, E>, ? extends A>> lazy) { return new EIO<>(PureIO.defer(() -> lazy.andThen(EIOOf::narrowK).get().instance)); } @@ -351,29 +351,29 @@ public static EIO throwError(Throwable error) { return new EIO<>(PureIO.throwError(error)); } - public static EIO> traverse(Sequence, ? extends A>> sequence) { + public static EIO> traverse(Sequence, E>, ? extends A>> sequence) { return traverse(Future.DEFAULT_EXECUTOR, sequence); } - public static EIO> traverse(Executor executor, Sequence, ? extends A>> sequence) { + public static EIO> traverse(Executor executor, Sequence, E>, ? extends A>> sequence) { return sequence.foldLeft(pure(ImmutableList.empty()), - (Kind, ? extends Sequence> xs, Kind, ? extends A> a) -> parMap2(executor, xs, a, Sequence::append)); + (Kind, E>, ? extends Sequence> xs, Kind, E>, ? extends A> a) -> parMap2(executor, xs, a, Sequence::append)); } - public static EIO bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use) { + public static EIO bracket(Kind, E>, ? extends A> acquire, + Function1, E>, ? extends B>> use) { return new EIO<>(PureIO.bracket(acquire.fix(EIOOf::narrowK).instance, resource -> use.andThen(EIOOf::narrowK).apply(resource).instance)); } - public static EIO bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, Consumer1 release) { + public static EIO bracket(Kind, E>, ? extends A> acquire, + Function1, E>, ? extends B>> use, Consumer1 release) { return new EIO<>(PureIO.bracket(acquire.fix(EIOOf::narrowK).instance, resource -> use.andThen(EIOOf::narrowK).apply(resource).instance, release)); } - public static EIO bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, Function1, Unit>> release) { + public static EIO bracket(Kind, E>, ? extends A> acquire, + Function1, E>, ? extends B>> use, Function1, E>, Unit>> release) { return new EIO<>(PureIO.bracket(acquire.fix(EIOOf::narrowK).instance, resource -> use.andThen(EIOOf::narrowK).apply(resource).instance, release.andThen(EIOOf::narrowK).andThen(EIO::toPureIO))); } diff --git a/effect/src/main/java/com/github/tonivade/purefun/effect/Managed.java b/effect/src/main/java/com/github/tonivade/purefun/effect/Managed.java index 9d09c4800..b21d07135 100644 --- a/effect/src/main/java/com/github/tonivade/purefun/effect/Managed.java +++ b/effect/src/main/java/com/github/tonivade/purefun/effect/Managed.java @@ -35,7 +35,7 @@ public Managed mapError(Function1 mapper) { return new Managed<>(resource.mapError(mapper)); } - public Managed flatMap(Function1, E>, ? extends B>> mapper) { + public Managed flatMap(Function1, R>, E>, ? extends B>> mapper) { PureIO>> result = resource.flatMap(t -> { Managed apply = ManagedOf.narrowK(mapper.apply(t.get1())); return apply.resource.map(r -> r.map2(ignore -> releaseAndThen(t, r))); @@ -43,11 +43,11 @@ public Managed flatMap(Function1(result); } - public Managed flatMapError(Function1, F>, ? extends A>> mapper) { + public Managed flatMapError(Function1, R>, F>, ? extends A>> mapper) { return new Managed<>(resource.flatMapError(e -> ManagedOf.narrowK(mapper.apply(e)).resource)); } - public Managed andThen(Kind, E>, B> other) { + public Managed andThen(Kind, A>, E>, B> other) { PureIO>> flatMap = resource.flatMap(a -> { Either>> next = ManagedOf.narrowK(other).resource.provide(a.get1()); return PureIO.fromEither(() -> next.map(t -> t.map2(ignore -> releaseAndThen(a, t)))); @@ -75,8 +75,8 @@ public Managed orElse(Managed other) { } public Managed foldM( - Function1, F>, ? extends B>> mapError, - Function1, F>, ? extends B>> mapper) { + Function1, R>, F>, ? extends B>> mapError, + Function1, R>, F>, ? extends B>> mapper) { PureIO>> foldM = resource.foldM( error -> ManagedOf.narrowK(mapError.apply(error)).resource, diff --git a/effect/src/main/java/com/github/tonivade/purefun/effect/PureIO.java b/effect/src/main/java/com/github/tonivade/purefun/effect/PureIO.java index 084d6d474..ca66f2276 100644 --- a/effect/src/main/java/com/github/tonivade/purefun/effect/PureIO.java +++ b/effect/src/main/java/com/github/tonivade/purefun/effect/PureIO.java @@ -45,7 +45,7 @@ import com.github.tonivade.purefun.typeclasses.Fiber; @HigherKind -public sealed interface PureIO extends PureIOOf, Effect, E>, A> { +public sealed interface PureIO extends PureIOOf, Effect, R>, E>, A> { default Either provide(@Nullable R env) { return runAsync(env).getOrElseThrow(); @@ -85,27 +85,27 @@ default PureIO bimap(Function1 mapError, } @Override - default PureIO flatMap(Function1, E>, ? extends B>> map) { + default PureIO flatMap(Function1, R>, E>, ? extends B>> map) { return foldM(PureIO::raiseError, map); } - default PureIO flatMapError(Function1, F>, ? extends A>> map) { + default PureIO flatMapError(Function1, R>, F>, ? extends A>> map) { return foldM(map, PureIO::pure); } default PureIO foldM( - Function1, F>, ? extends B>> left, - Function1, F>, ? extends B>> right) { + Function1, R>, F>, ? extends B>> left, + Function1, R>, F>, ? extends B>> right) { return new FlatMapped<>(this, left, right); } @Override - default PureIO andThen(Kind, E>, ? extends B> next) { + default PureIO andThen(Kind, R>, E>, ? extends B> next) { return flatMap(ignore -> next); } @Override - default PureIO ap(Kind, E>, ? extends Function1> apply) { + default PureIO ap(Kind, R>, E>, ? extends Function1> apply) { return parMap2(this, apply.fix(PureIOOf.toPureIO()), (v, a) -> a.apply(v)); } @@ -118,27 +118,27 @@ default URIO recover(Function1 mapError) { return fold(mapError, identity()); } - default PureIO orElse(Kind, E>, ? extends A> other) { + default PureIO orElse(Kind, R>, E>, ? extends A> other) { return foldM(Function1.cons(other), Function1.cons(this)); } @Override - default PureIO> zip(Kind, E>, ? extends B> other) { + default PureIO> zip(Kind, R>, E>, ? extends B> other) { return zipWith(other, Tuple::of); } @Override - default PureIO zipLeft(Kind, E>, ? extends B> other) { + default PureIO zipLeft(Kind, R>, E>, ? extends B> other) { return zipWith(other, first()); } @Override - default PureIO zipRight(Kind, E>, ? extends B> other) { + default PureIO zipRight(Kind, R>, E>, ? extends B> other) { return zipWith(other, second()); } @Override - default PureIO zipWith(Kind, E>, ? extends B> other, + default PureIO zipWith(Kind, R>, E>, ? extends B> other, Function2 mapper) { return parMap2(this, other.fix(PureIOOf.toPureIO()), mapper); } @@ -189,7 +189,7 @@ default PureIO> timed() { start -> map(result -> Tuple.of(Duration.ofNanos(System.nanoTime() - start), result))); } - default PureIO, E>, A>> fork() { + default PureIO, R>, E>, A>> fork() { return async((env, callback) -> { PureIOConnection connection = PureIOConnection.cancellable(); Promise> promise = runAsync(env, this, connection); @@ -243,7 +243,7 @@ default Managed toManaged(Consumer1 release) { return Managed.from(this, release); } - static PureIO accessM(Function1, E>, ? extends A>> map) { + static PureIO accessM(Function1, R>, E>, ? extends A>> map) { return new AccessM<>(value -> map.apply(value).fix(PureIOOf::narrowK)); } @@ -259,12 +259,12 @@ static PureIO forked(Executor executor) { return async((env, callback) -> executor.execute(() -> callback.accept(Try.success(Either.right(Unit.unit()))))); } - static PureIO parMap2(Kind, E>, ? extends A> za, Kind, E>, ? extends B> zb, + static PureIO parMap2(Kind, R>, E>, ? extends A> za, Kind, R>, E>, ? extends B> zb, Function2 mapper) { return parMap2(Future.DEFAULT_EXECUTOR, za, zb, mapper); } - static PureIO parMap2(Executor executor, Kind, E>, ? extends A> za, Kind, E>, ? extends B> zb, + static PureIO parMap2(Executor executor, Kind, R>, E>, ? extends A> za, Kind, R>, E>, ? extends B> zb, Function2 mapper) { return cancellable((env, callback) -> { @@ -287,18 +287,18 @@ static PureIO parMap2(Executor executor, Kind PureIO> race(Kind, E>, ? extends A> fa, Kind, E>, ? extends B> fb) { + static PureIO> race(Kind, R>, E>, ? extends A> fa, Kind, R>, E>, ? extends B> fb) { return race(Future.DEFAULT_EXECUTOR, fa, fb); } - static PureIO> race(Executor executor, Kind, E>, ? extends A> fa, Kind, E>, ? extends B> fb) { + static PureIO> race(Executor executor, Kind, R>, E>, ? extends A> fa, Kind, R>, E>, ? extends B> fb) { return racePair(executor, fa, fb).flatMap(either -> either.fold( ta -> ta.get2().cancel().fix(PureIOOf.toPureIO()).map(x -> Either.left(ta.get1())), tb -> tb.get1().cancel().fix(PureIOOf.toPureIO()).map(x -> Either.right(tb.get2())))); } - static PureIO, E>, B>>, Tuple2, E>, A>, B>>> - racePair(Executor executor, Kind, E>, ? extends A> fa, Kind, E>, ? extends B> fb) { + static PureIO, R>, E>, B>>, Tuple2, R>, E>, A>, B>>> + racePair(Executor executor, Kind, R>, E>, ? extends A> fa, Kind, R>, E>, ? extends B> fb) { return cancellable((env, callback) -> { PureIOConnection connection1 = PureIOConnection.cancellable(); @@ -310,7 +310,7 @@ static PureIO> race(Executor executor, Kind { PureIO fromPromiseB = PureIO.fromPromise(promiseB); PureIO cancelB = PureIO.run(connection2::cancel); - Fiber, E>, B> fiberB = Fiber.of(fromPromiseB, cancelB); + Fiber, R>, E>, B> fiberB = Fiber.of(fromPromiseB, cancelB); callback.accept(result.map( either -> either.map( a -> Either.left(Tuple.of(a, fiberB))))); @@ -319,7 +319,7 @@ static PureIO> race(Executor executor, Kind { PureIO fromPromiseA = PureIO.fromPromise(promiseA); PureIO cancelA = PureIO.run(connection2::cancel); - Fiber, E>, A> fiberA = Fiber.of(fromPromiseA, cancelA); + Fiber, R>, E>, A> fiberA = Fiber.of(fromPromiseA, cancelA); callback.accept(result.map( either -> either.map( b -> Either.right(Tuple.of(fiberA, b))))); @@ -335,7 +335,7 @@ static PureIO> race(Executor executor, Kind PureIO absorb(Kind, E>, Either> value) { + static PureIO absorb(Kind, R>, E>, Either> value) { return value.fix(PureIOOf::narrowK).flatMap(either -> either.fold(PureIO::raiseError, PureIO::pure)); } @@ -396,7 +396,7 @@ static PureIO pure(A value) { return new Pure<>(value); } - static PureIO defer(Producer, E>, ? extends A>> lazy) { + static PureIO defer(Producer, R>, E>, ? extends A>> lazy) { return new Suspend<>(() -> lazy.get().fix(PureIOOf::narrowK)); } @@ -428,7 +428,7 @@ static PureIO throwError(Throwable error) { return new Throw<>(error); } - static PureIO redeem(Kind, Void>, ? extends A> value) { + static PureIO redeem(Kind, R>, Void>, ? extends A> value) { return new Recover<>(value.fix(PureIOOf::narrowK), PartialFunction1.of(always(), PureIO::raiseError)); } @@ -444,29 +444,29 @@ static PureIO sleep(Executor executor, Duration delay) { }); } - static PureIO> traverse(Sequence, E>, A>> sequence) { + static PureIO> traverse(Sequence, R>, E>, A>> sequence) { return traverse(Future.DEFAULT_EXECUTOR, sequence); } - static PureIO> traverse(Executor executor, Sequence, E>, A>> sequence) { + static PureIO> traverse(Executor executor, Sequence, R>, E>, A>> sequence) { return sequence.foldLeft(PureIO.>pure(ImmutableList.empty()), - (Kind, E>, Sequence> xs, Kind, E>, A> a) -> parMap2(executor, xs, a, Sequence::append)); + (Kind, R>, E>, Sequence> xs, Kind, R>, E>, A> a) -> parMap2(executor, xs, a, Sequence::append)); } - static PureIO bracket(Kind, E>, ? extends A> acquire, - Function1, E>, ? extends B>> use) { + static PureIO bracket(Kind, R>, E>, ? extends A> acquire, + Function1, R>, E>, ? extends B>> use) { return bracket(acquire, use, AutoCloseable::close); } - static PureIO bracket(Kind, E>, ? extends A> acquire, - Function1, E>, ? extends B>> use, + static PureIO bracket(Kind, R>, E>, ? extends A> acquire, + Function1, R>, E>, ? extends B>> use, Consumer1 release) { return bracket(acquire, use, release.asFunction().andThen(PureIO::pure)); } - static PureIO bracket(Kind, E>, ? extends A> acquire, - Function1, E>, ? extends B>> use, - Function1, E>, Unit>> release) { + static PureIO bracket(Kind, R>, E>, ? extends A> acquire, + Function1, R>, E>, ? extends B>> use, + Function1, R>, E>, Unit>> release) { // TODO: cancel return cancellable((env, callback) -> { @@ -516,7 +516,7 @@ private static Promise> runAsync(@Nullable R env, PureIO< @SuppressWarnings("unchecked") private static Promise> runAsync( - @Nullable R env, Kind, E>, A> current, PureIOConnection connection, CallStack stack, Promise> promise) { + @Nullable R env, Kind, R>, E>, A> current, PureIOConnection connection, CallStack stack, Promise> promise) { while (true) { try { current = unwrap(env, current, stack, identity()); @@ -537,7 +537,7 @@ private static Promise> runAsync( stack.push(); var flatMapped = (FlatMapped) current; - Kind, F>, B> source = unwrap(env, flatMapped.current, stack, + Kind, R>, F>, B> source = unwrap(env, flatMapped.current, stack, b -> b.fix(PureIOOf::narrowK).foldM(flatMapped.nextError, flatMapped.next)); if (source instanceof Async async) { @@ -582,9 +582,9 @@ private static Promise> runAsync( } @SuppressWarnings("unchecked") - private static Kind, E>, A> unwrap( - @Nullable R env, Kind, E>, A> current, CallStack stack, - Function1, E>, ? extends A>, Kind, F>, ? extends B>> next) { + private static Kind, R>, E>, A> unwrap( + @Nullable R env, Kind, R>, E>, A> current, CallStack stack, + Function1, R>, E>, ? extends A>, Kind, R>, F>, ? extends B>> next) { while (true) { if (current instanceof Failure) { return current; @@ -616,7 +616,7 @@ private static Kind, E>, A> unwrap( } @SuppressWarnings("NullAway") - static Kind, E>, A> accessM(@Nullable R env, AccessM accessM) { + static Kind, R>, E>, A> accessM(@Nullable R env, AccessM accessM) { return accessM.function.apply(env).fix(PureIOOf::narrowK); } @@ -686,13 +686,13 @@ public String toString() { final class FlatMapped implements PureIO { - private final Kind, E>, A> current; - private final Function1, F>, ? extends B>> nextError; - private final Function1, F>, ? extends B>> next; + private final Kind, R>, E>, A> current; + private final Function1, R>, F>, ? extends B>> nextError; + private final Function1, R>, F>, ? extends B>> next; private FlatMapped(PureIO current, - Function1, F>, ? extends B>> nextError, - Function1, F>, ? extends B>> next) { + Function1, R>, F>, ? extends B>> nextError, + Function1, R>, F>, ? extends B>> next) { this.current = checkNonNull(current); this.nextError = checkNonNull(nextError); this.next = checkNonNull(next); @@ -720,9 +720,9 @@ public String toString() { final class Suspend implements PureIO { - private final Producer, E>, ? extends A>> lazy; + private final Producer, R>, E>, ? extends A>> lazy; - private Suspend(Producer, E>, ? extends A>> lazy) { + private Suspend(Producer, R>, E>, ? extends A>> lazy) { this.lazy = checkNonNull(lazy); } @@ -778,9 +778,9 @@ public String toString() { final class AccessM implements PureIO { - private final Function1, E>, ? extends A>> function; + private final Function1, R>, E>, ? extends A>> function; - private AccessM(Function1, E>, ? extends A>> function) { + private AccessM(Function1, R>, E>, ? extends A>> function) { this.function = checkNonNull(function); } @@ -943,7 +943,7 @@ public void pop() { } } - public void add(PartialFunction1, E>, ? extends A>> mapError) { + public void add(PartialFunction1, R>, E>, ? extends A>> mapError) { if (top == null) { return; } @@ -972,7 +972,7 @@ public Option> tryHandle(Throwable error) { final class StackItem { private int count = 0; - private final Deque, E>, ? extends A>>> recover = new ArrayDeque<>(); + private final Deque, R>, E>, ? extends A>>> recover = new ArrayDeque<>(); @Nullable private final StackItem prev; @@ -1006,7 +1006,7 @@ public void reset() { count = 0; } - public void add(PartialFunction1, E>, ? extends A>> mapError) { + public void add(PartialFunction1, R>, E>, ? extends A>> mapError) { recover.addFirst(mapError); } diff --git a/effect/src/main/java/com/github/tonivade/purefun/effect/RIO.java b/effect/src/main/java/com/github/tonivade/purefun/effect/RIO.java index 202de5970..00d03446f 100644 --- a/effect/src/main/java/com/github/tonivade/purefun/effect/RIO.java +++ b/effect/src/main/java/com/github/tonivade/purefun/effect/RIO.java @@ -37,7 +37,7 @@ import com.github.tonivade.purefun.typeclasses.FunctionK; @HigherKind -public final class RIO implements RIOOf, Effect, A>, Recoverable { +public final class RIO implements RIOOf, Effect, R>, A>, Recoverable { private static final RIO UNIT = new RIO<>(PureIO.unit()); @@ -82,7 +82,7 @@ public RIO map(Function1 map) { } @Override - public RIO flatMap(Function1, ? extends B>> map) { + public RIO flatMap(Function1, R>, ? extends B>> map) { return new RIO<>(instance.flatMap(x -> { RIO apply = map.andThen(RIOOf::narrowK).apply(x); return apply.instance; @@ -90,12 +90,12 @@ public RIO flatMap(Function1, } @Override - public RIO andThen(Kind, ? extends B> next) { + public RIO andThen(Kind, R>, ? extends B> next) { return new RIO<>(instance.andThen(next.fix(RIOOf.toRIO()).instance)); } @Override - public RIO ap(Kind, ? extends Function1> apply) { + public RIO ap(Kind, R>, ? extends Function1> apply) { return new RIO<>(instance.ap(apply.fix(RIOOf.toRIO()).instance)); } @@ -119,42 +119,42 @@ public URIO fold( } public RIO foldM( - Function1, ? extends B>> mapError, - Function1, ? extends B>> map) { + Function1, R>, ? extends B>> mapError, + Function1, R>, ? extends B>> map) { return new RIO<>(instance.foldM( error -> mapError.andThen(RIOOf::narrowK).apply(error).instance, value -> map.andThen(RIOOf::narrowK).apply(value).instance)); } - public RIO orElse(Kind, ? extends A> other) { + public RIO orElse(Kind, R>, ? extends A> other) { return foldM(Function1.cons(other), Function1.cons(this)); } @Override - public RIO> zip(Kind, ? extends B> other) { + public RIO> zip(Kind, R>, ? extends B> other) { return zipWith(other, Tuple::of); } @Override - public RIO zipLeft(Kind, ? extends B> other) { + public RIO zipLeft(Kind, R>, ? extends B> other) { return zipWith(other, first()); } @Override - public RIO zipRight(Kind, ? extends B> other) { + public RIO zipRight(Kind, R>, ? extends B> other) { return zipWith(other, second()); } @Override - public RIO zipWith(Kind, ? extends B> other, + public RIO zipWith(Kind, R>, ? extends B> other, Function2 mapper) { return parMap2(this, other.fix(RIOOf.toRIO()), mapper); } - public RIO, A>> fork() { + public RIO, R>, A>> fork() { return new RIO<>(instance.fork().map(f -> f.mapK(new FunctionK<>() { @Override - public RIO apply(Kind, Throwable>, ? extends T> from) { + public RIO apply(Kind, R>, Throwable>, ? extends T> from) { return new RIO<>(from.fix(PureIOOf::narrowK)); } }))); @@ -220,7 +220,7 @@ public static RIO forked(Executor executor) { return async((env, callback) -> executor.execute(() -> callback.accept(Try.success(Unit.unit())))); } - public static RIO accessM(Function1, ? extends A>> map) { + public static RIO accessM(Function1, R>, ? extends A>> map) { return new RIO<>(PureIO.accessM(map.andThen(RIOOf::narrowK).andThen(RIO::toPureIO))); } @@ -236,39 +236,39 @@ public static RIO absorb(RIO> value) { return new RIO<>(PureIO.absorb(value.instance)); } - public static RIO parMap2(Kind, ? extends A> za, Kind, ? extends B> zb, + public static RIO parMap2(Kind, R>, ? extends A> za, Kind, R>, ? extends B> zb, Function2 mapper) { return parMap2(Future.DEFAULT_EXECUTOR, za, zb, mapper); } - public static RIO parMap2(Executor executor, Kind, ? extends A> za, Kind, ? extends B> zb, + public static RIO parMap2(Executor executor, Kind, R>, ? extends A> za, Kind, R>, ? extends B> zb, Function2 mapper) { return new RIO<>(PureIO.parMap2(executor, za.fix(RIOOf::narrowK).instance, zb.fix(RIOOf::narrowK).instance, mapper)); } - public static RIO> race(Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static RIO> race(Kind, R>, ? extends A> fa, Kind, R>, ? extends B> fb) { return race(Future.DEFAULT_EXECUTOR, fa, fb); } - public static RIO> race(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static RIO> race(Executor executor, Kind, R>, ? extends A> fa, Kind, R>, ? extends B> fb) { return racePair(executor, fa, fb).flatMap(either -> either.fold( ta -> ta.get2().cancel().fix(RIOOf.toRIO()).map(x -> Either.left(ta.get1())), tb -> tb.get1().cancel().fix(RIOOf.toRIO()).map(x -> Either.right(tb.get2())))); } - public static RIO, B>>, Tuple2, A>, B>>> - racePair(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static RIO, R>, B>>, Tuple2, R>, A>, B>>> + racePair(Executor executor, Kind, R>, ? extends A> fa, Kind, R>, ? extends B> fb) { PureIO instance1 = fa.fix(RIOOf.toRIO()).instance.fix(PureIOOf::narrowK); PureIO instance2 = fb.fix(RIOOf.toRIO()).instance.fix(PureIOOf::narrowK); return new RIO<>(PureIO.racePair(executor, instance1, instance2).map( either -> either.bimap(a -> a.map2(f -> f.mapK(new FunctionK<>() { @Override - public RIO apply(Kind, Throwable>, ? extends T> from) { + public RIO apply(Kind, R>, Throwable>, ? extends T> from) { return new RIO<>(from.fix(PureIOOf::narrowK)); } })), b -> b.map1(f -> f.mapK(new FunctionK<>() { @Override - public RIO apply(Kind, Throwable>, ? extends T> from) { + public RIO apply(Kind, R>, Throwable>, ? extends T> from) { return new RIO<>(from.fix(PureIOOf::narrowK)); } }))))); @@ -334,7 +334,7 @@ public static RIO raiseError(Throwable throwable) { return new RIO<>(PureIO.raiseError(throwable)); } - public static RIO defer(Producer, ? extends A>> lazy) { + public static RIO defer(Producer, R>, ? extends A>> lazy) { return new RIO<>(PureIO.defer(() -> lazy.andThen(RIOOf::narrowK).get().instance)); } @@ -356,29 +356,29 @@ public static RIO cancellable(Function2 consumer.andThen(RIO::toPureIO).apply(env, result -> cb1.accept(result.map(Either::right))))); } - public static RIO> traverse(Sequence, A>> sequence) { + public static RIO> traverse(Sequence, R>, A>> sequence) { return traverse(Future.DEFAULT_EXECUTOR, sequence); } - public static RIO> traverse(Executor executor, Sequence, A>> sequence) { + public static RIO> traverse(Executor executor, Sequence, R>, A>> sequence) { return sequence.foldLeft(pure(ImmutableList.empty()), - (Kind, Sequence> xs, Kind, A> a) -> parMap2(executor, xs, a, Sequence::append)); + (Kind, R>, Sequence> xs, Kind, R>, A> a) -> parMap2(executor, xs, a, Sequence::append)); } - public static RIO bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use) { + public static RIO bracket(Kind, R>, ? extends A> acquire, + Function1, R>, ? extends B>> use) { return new RIO<>(PureIO.bracket(acquire.fix(RIOOf::narrowK).instance, resource -> use.andThen(RIOOf::narrowK).apply(resource).instance)); } - public static RIO bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, Consumer1 release) { + public static RIO bracket(Kind, R>, ? extends A> acquire, + Function1, R>, ? extends B>> use, Consumer1 release) { return new RIO<>(PureIO.bracket(acquire.fix(RIOOf::narrowK).instance, resource -> use.andThen(RIOOf::narrowK).apply(resource).instance, release)); } - public static RIO bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, Function1, Unit>> release) { + public static RIO bracket(Kind, R>, ? extends A> acquire, + Function1, R>, ? extends B>> use, Function1, R>, Unit>> release) { return new RIO<>(PureIO.bracket(acquire.fix(RIOOf::narrowK).instance, resource -> use.andThen(RIOOf::narrowK).apply(resource).instance, release.andThen(RIOOf::narrowK).andThen(RIO::toPureIO))); } diff --git a/effect/src/main/java/com/github/tonivade/purefun/effect/Task.java b/effect/src/main/java/com/github/tonivade/purefun/effect/Task.java index d9266cbac..2dcffd54c 100644 --- a/effect/src/main/java/com/github/tonivade/purefun/effect/Task.java +++ b/effect/src/main/java/com/github/tonivade/purefun/effect/Task.java @@ -35,7 +35,7 @@ import com.github.tonivade.purefun.typeclasses.FunctionK; @HigherKind -public final class Task implements TaskOf, Effect, Recoverable { +public final class Task implements TaskOf, Effect, A>, Recoverable { private static final Task UNIT = new Task<>(PureIO.unit()); @@ -85,7 +85,7 @@ public Task map(Function1 map) { } @Override - public Task flatMap(Function1> map) { + public Task flatMap(Function1, ? extends B>> map) { return new Task<>(instance.flatMap(value -> { Task apply = map.andThen(TaskOf::narrowK).apply(value); return apply.instance; @@ -93,18 +93,18 @@ public Task flatMap(Function1 Task andThen(Kind next) { + public Task andThen(Kind, ? extends B> next) { return new Task<>(instance.andThen(next.fix(TaskOf.toTask()).instance)); } @Override - public Task ap(Kind> apply) { + public Task ap(Kind, ? extends Function1> apply) { return new Task<>(instance.ap(apply.fix(TaskOf.toTask()).instance)); } public Task foldM( - Function1> mapError, - Function1> map) { + Function1, ? extends B>> mapError, + Function1, ? extends B>> map) { return new Task<>(instance.foldM( error -> mapError.andThen(TaskOf::narrowK).apply(error).instance, value -> map.andThen(TaskOf::narrowK).apply(value).instance)); @@ -129,35 +129,35 @@ public UIO recover(Function1 mapError) { return new UIO<>(instance.recover(mapError).toPureIO()); } - public Task orElse(Kind other) { + public Task orElse(Kind, ? extends A> other) { return new Task<>(instance.orElse(other.fix(TaskOf.toTask()).instance)); } @Override - public Task> zip(Kind other) { + public Task> zip(Kind, ? extends B> other) { return zipWith(other, Tuple::of); } @Override - public Task zipLeft(Kind other) { + public Task zipLeft(Kind, ? extends B> other) { return zipWith(other, first()); } @Override - public Task zipRight(Kind other) { + public Task zipRight(Kind, ? extends B> other) { return zipWith(other, second()); } @Override - public Task zipWith(Kind other, + public Task zipWith(Kind, ? extends B> other, Function2 mapper) { return parMap2(this, other.fix(TaskOf.toTask()), mapper); } - public Task> fork() { + public Task, A>> fork() { return new Task<>(instance.fork().map(f -> f.mapK(new FunctionK<>() { @Override - public Task apply(Kind, Throwable>, ? extends T> from) { + public Task apply(Kind, Void>, Throwable>, ? extends T> from) { return new Task<>(from.fix(PureIOOf::narrowK)); } }))); @@ -223,39 +223,39 @@ public static Task forked(Executor executor) { return async(callback -> executor.execute(() -> callback.accept(Try.success(Unit.unit())))); } - public static Task parMap2(Kind za, Kind zb, + public static Task parMap2(Kind, ? extends A> za, Kind, ? extends B> zb, Function2 mapper) { return parMap2(Future.DEFAULT_EXECUTOR, za, zb, mapper); } - public static Task parMap2(Executor executor, Kind za, Kind zb, + public static Task parMap2(Executor executor, Kind, ? extends A> za, Kind, ? extends B> zb, Function2 mapper) { return new Task<>(PureIO.parMap2(executor, za.fix(TaskOf::narrowK).instance, zb.fix(TaskOf::narrowK).instance, mapper)); } - public static Task> race(Kind fa, Kind fb) { + public static Task> race(Kind, ? extends A> fa, Kind, ? extends B> fb) { return race(Future.DEFAULT_EXECUTOR, fa, fb); } - public static Task> race(Executor executor, Kind fa, Kind fb) { + public static Task> race(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { return racePair(executor, fa, fb).flatMap(either -> either.fold( ta -> ta.get2().cancel().fix(TaskOf.toTask()).map(x -> Either.left(ta.get1())), tb -> tb.get1().cancel().fix(TaskOf.toTask()).map(x -> Either.right(tb.get2())))); } - public static Task>, Tuple2, B>>> - racePair(Executor executor, Kind fa, Kind fb) { + public static Task, B>>, Tuple2, A>, B>>> + racePair(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { PureIO instance1 = fa.fix(TaskOf.toTask()).instance.fix(PureIOOf::narrowK); PureIO instance2 = fb.fix(TaskOf.toTask()).instance.fix(PureIOOf::narrowK); return new Task<>(PureIO.racePair(executor, instance1, instance2).map( either -> either.bimap(a -> a.map2(f -> f.mapK(new FunctionK<>() { @Override - public Task apply(Kind, Throwable>, ? extends T> from) { + public Task apply(Kind, Void>, Throwable>, ? extends T> from) { return new Task<>(from.fix(PureIOOf::narrowK)); } })), b -> b.map1(f -> f.mapK(new FunctionK<>() { @Override - public Task apply(Kind, Throwable>, ? extends T> from) { + public Task apply(Kind, Void>, Throwable>, ? extends T> from) { return new Task<>(from.fix(PureIOOf::narrowK)); } }))))); @@ -321,7 +321,7 @@ public static Task pure(A value) { return new Task<>(PureIO.pure(value)); } - public static Task defer(Producer> lazy) { + public static Task defer(Producer, ? extends A>> lazy) { return new Task<>(PureIO.defer(() -> lazy.andThen(TaskOf::narrowK).get().instance)); } @@ -347,27 +347,27 @@ public static Task raiseError(Throwable error) { return new Task<>(PureIO.raiseError(error)); } - public static Task> traverse(Sequence> sequence) { + public static Task> traverse(Sequence, A>> sequence) { return traverse(Future.DEFAULT_EXECUTOR, sequence); } - public static Task> traverse(Executor executor, Sequence> sequence) { + public static Task> traverse(Executor executor, Sequence, A>> sequence) { return sequence.foldLeft(pure(ImmutableList.empty()), - (Kind> xs, Kind a) -> parMap2(executor, xs, a, Sequence::append)); + (Kind, Sequence> xs, Kind, A> a) -> parMap2(executor, xs, a, Sequence::append)); } public static Task bracket( - Kind acquire, Function1> use) { + Kind, ? extends A> acquire, Function1, ? extends B>> use) { return new Task<>(PureIO.bracket(acquire.fix(TaskOf::narrowK).instance, resource -> use.andThen(TaskOf::narrowK).apply(resource).instance)); } public static Task bracket( - Kind acquire, Function1> use, Consumer1 release) { + Kind, ? extends A> acquire, Function1, ? extends B>> use, Consumer1 release) { return new Task<>(PureIO.bracket(acquire.fix(TaskOf::narrowK).instance, resource -> use.andThen(TaskOf::narrowK).apply(resource).instance, release)); } public static Task bracket( - Kind acquire, Function1> use, Function1> release) { + Kind, ? extends A> acquire, Function1, ? extends B>> use, Function1, Unit>> release) { return new Task<>(PureIO.bracket(acquire.fix(TaskOf::narrowK).instance, resource -> use.andThen(TaskOf::narrowK).apply(resource).instance, release.andThen(TaskOf::narrowK).andThen(Task::toPureIO))); } diff --git a/effect/src/main/java/com/github/tonivade/purefun/effect/UIO.java b/effect/src/main/java/com/github/tonivade/purefun/effect/UIO.java index 16b166632..b96457678 100644 --- a/effect/src/main/java/com/github/tonivade/purefun/effect/UIO.java +++ b/effect/src/main/java/com/github/tonivade/purefun/effect/UIO.java @@ -37,7 +37,7 @@ import com.github.tonivade.purefun.typeclasses.FunctionK; @HigherKind -public final class UIO implements UIOOf, Effect, Recoverable { +public final class UIO implements UIOOf, Effect, A>, Recoverable { private static final UIO UNIT = new UIO<>(PureIO.unit()); @@ -101,7 +101,7 @@ public UIO map(Function1 map) { } @Override - public UIO flatMap(Function1> map) { + public UIO flatMap(Function1, ? extends B>> map) { return new UIO<>(instance.flatMap(x -> { UIO apply = map.andThen(UIOOf::narrowK).apply(x); return apply.instance; @@ -109,12 +109,12 @@ public UIO flatMap(Function1 } @Override - public UIO andThen(Kind next) { + public UIO andThen(Kind, ? extends B> next) { return new UIO<>(instance.andThen(next.fix(UIOOf.toUIO()).instance)); } @Override - public UIO ap(Kind> apply) { + public UIO ap(Kind, ? extends Function1> apply) { return new UIO<>(instance.ap(apply.fix(UIOOf.toUIO()).instance)); } @@ -138,38 +138,38 @@ public UIO redeem( } public UIO redeemWith( - Function1> mapError, - Function1> map) { + Function1, ? extends B>> mapError, + Function1, ? extends B>> map) { return new UIO<>(PureIO.redeem(instance).foldM( error -> mapError.andThen(UIOOf::narrowK).apply(error).instance, value -> map.andThen(UIOOf::narrowK).apply(value).instance)); } @Override - public UIO> zip(Kind other) { + public UIO> zip(Kind, ? extends B> other) { return zipWith(other, Tuple::of); } @Override - public UIO zipLeft(Kind other) { + public UIO zipLeft(Kind, ? extends B> other) { return zipWith(other, first()); } @Override - public UIO zipRight(Kind other) { + public UIO zipRight(Kind, ? extends B> other) { return zipWith(other, second()); } @Override - public UIO zipWith(Kind other, + public UIO zipWith(Kind, ? extends B> other, Function2 mapper) { return parMap2(this, other.fix(UIOOf.toUIO()), mapper); } - public UIO> fork() { + public UIO, A>> fork() { return new UIO<>(instance.fork().map(f -> f.mapK(new FunctionK<>() { @Override - public UIO apply(Kind, Void>, ? extends T> from) { + public UIO apply(Kind, Void>, Void>, ? extends T> from) { return new UIO<>(from.fix(PureIOOf::narrowK)); } }))); @@ -235,39 +235,39 @@ public static UIO forked(Executor executor) { return async(callback -> executor.execute(() -> callback.accept(Try.success(Unit.unit())))); } - public static UIO parMap2(Kind za, Kind zb, + public static UIO parMap2(Kind, ? extends A> za, Kind, ? extends B> zb, Function2 mapper) { return parMap2(Future.DEFAULT_EXECUTOR, za, zb, mapper); } - public static UIO parMap2(Executor executor, Kind za, Kind zb, + public static UIO parMap2(Executor executor, Kind, ? extends A> za, Kind, ? extends B> zb, Function2 mapper) { return new UIO<>(PureIO.parMap2(executor, za.fix(UIOOf::narrowK).instance, zb.fix(UIOOf::narrowK).instance, mapper)); } - public static UIO> race(Kind fa, Kind fb) { + public static UIO> race(Kind, ? extends A> fa, Kind, ? extends B> fb) { return race(Future.DEFAULT_EXECUTOR, fa, fb); } - public static UIO> race(Executor executor, Kind fa, Kind fb) { + public static UIO> race(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { return racePair(executor, fa, fb).flatMap(either -> either.fold( ta -> ta.get2().cancel().fix(UIOOf.toUIO()).map(x -> Either.left(ta.get1())), tb -> tb.get1().cancel().fix(UIOOf.toUIO()).map(x -> Either.right(tb.get2())))); } - public static UIO>, Tuple2, B>>> - racePair(Executor executor, Kind fa, Kind fb) { + public static UIO, B>>, Tuple2, A>, B>>> + racePair(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { PureIO instance1 = fa.fix(UIOOf.toUIO()).instance.fix(PureIOOf::narrowK); PureIO instance2 = fb.fix(UIOOf.toUIO()).instance.fix(PureIOOf::narrowK); return new UIO<>(PureIO.racePair(executor, instance1, instance2).map( either -> either.bimap(a -> a.map2(f -> f.mapK(new FunctionK<>() { @Override - public UIO apply(Kind, Void>, ? extends T> from) { + public UIO apply(Kind, Void>, Void>, ? extends T> from) { return new UIO<>(from.fix(PureIOOf::narrowK)); } })), b -> b.map1(f -> f.mapK(new FunctionK<>() { @Override - public UIO apply(Kind, Void>, ? extends T> from) { + public UIO apply(Kind, Void>, Void>, ? extends T> from) { return new UIO<>(from.fix(PureIOOf::narrowK)); } }))))); @@ -309,7 +309,7 @@ public static UIO raiseError(Throwable throwable) { return new UIO<>(PureIO.fromEither(() -> { throw throwable; })); } - public static UIO defer(Producer> lazy) { + public static UIO defer(Producer, ? extends A>> lazy) { return new UIO<>(PureIO.defer(() -> lazy.andThen(UIOOf::narrowK).get().instance)); } @@ -364,29 +364,29 @@ public static UIO>> memoize(Executor executor, Functi }); } - public static UIO> traverse(Sequence> sequence) { + public static UIO> traverse(Sequence, A>> sequence) { return traverse(Future.DEFAULT_EXECUTOR, sequence); } - public static UIO> traverse(Executor executor, Sequence> sequence) { + public static UIO> traverse(Executor executor, Sequence, A>> sequence) { return sequence.foldLeft(pure(ImmutableList.empty()), - (Kind> xs, Kind a) -> parMap2(executor, xs, a, Sequence::append)); + (Kind, Sequence> xs, Kind, A> a) -> parMap2(executor, xs, a, Sequence::append)); } public static UIO bracket( - Kind acquire, Function1> use) { + Kind, ? extends A> acquire, Function1, ? extends B>> use) { return fold(PureIO.bracket(PureIO.redeem(acquire.fix(UIOOf::narrowK).instance), resource -> PureIO.redeem(use.andThen(UIOOf::narrowK).apply(resource).instance))); } - public static UIO bracket(Kind acquire, - Function1> use, Consumer1 release) { + public static UIO bracket(Kind, ? extends A> acquire, + Function1, ? extends B>> use, Consumer1 release) { return fold(PureIO.bracket(PureIO.redeem(acquire.fix(UIOOf::narrowK).instance), resource -> PureIO.redeem(use.andThen(UIOOf::narrowK).apply(resource).instance), release)); } - public static UIO bracket(Kind acquire, - Function1> use, Function1> release) { + public static UIO bracket(Kind, ? extends A> acquire, + Function1, ? extends B>> use, Function1, Unit>> release) { return fold(PureIO.bracket(PureIO.redeem(acquire.fix(UIOOf::narrowK).instance), resource -> PureIO.redeem(use.andThen(UIOOf::narrowK).apply(resource).instance), release.andThen(UIOOf::narrowK).andThen(UIO::toPureIO))); } diff --git a/effect/src/main/java/com/github/tonivade/purefun/effect/URIO.java b/effect/src/main/java/com/github/tonivade/purefun/effect/URIO.java index d2929993b..21583364d 100644 --- a/effect/src/main/java/com/github/tonivade/purefun/effect/URIO.java +++ b/effect/src/main/java/com/github/tonivade/purefun/effect/URIO.java @@ -36,7 +36,7 @@ import com.github.tonivade.purefun.typeclasses.FunctionK; @HigherKind -public final class URIO implements URIOOf, Effect, A>, Recoverable { +public final class URIO implements URIOOf, Effect, R>, A>, Recoverable { private static final URIO UNIT = new URIO<>(PureIO.unit()); @@ -86,7 +86,7 @@ public URIO map(Function1 map) { } @Override - public URIO flatMap(Function1, ? extends B>> map) { + public URIO flatMap(Function1, R>, ? extends B>> map) { return new URIO<>(instance.flatMap(x -> { URIO apply = map.andThen(URIOOf::narrowK).apply(x); return apply.instance; @@ -94,12 +94,12 @@ public URIO flatMap(Function1 } @Override - public URIO andThen(Kind, ? extends B> next) { + public URIO andThen(Kind, R>, ? extends B> next) { return new URIO<>(instance.andThen(next.fix(URIOOf.toURIO()).instance)); } @Override - public URIO ap(Kind, ? extends Function1> apply) { + public URIO ap(Kind, R>, ? extends Function1> apply) { return new URIO<>(instance.ap(apply.fix(URIOOf.toURIO()).instance)); } @@ -124,38 +124,38 @@ public URIO redeem( } public URIO redeemWith( - Function1, ? extends B>> mapError, - Function1, ? extends B>> map) { + Function1, R>, ? extends B>> mapError, + Function1, R>, ? extends B>> map) { return new URIO<>(PureIO.redeem(instance).foldM( error -> mapError.andThen(URIOOf::narrowK).apply(error).instance, value -> map.andThen(URIOOf::narrowK).apply(value).instance)); } @Override - public URIO> zip(Kind, ? extends B> other) { + public URIO> zip(Kind, R>, ? extends B> other) { return zipWith(other, Tuple::of); } @Override - public URIO zipLeft(Kind, ? extends B> other) { + public URIO zipLeft(Kind, R>, ? extends B> other) { return zipWith(other, first()); } @Override - public URIO zipRight(Kind, ? extends B> other) { + public URIO zipRight(Kind, R>, ? extends B> other) { return zipWith(other, second()); } @Override - public URIO zipWith(Kind, ? extends B> other, + public URIO zipWith(Kind, R>, ? extends B> other, Function2 mapper) { return parMap2(this, other.fix(URIOOf.toURIO()), mapper); } - public URIO, A>> fork() { + public URIO, R>, A>> fork() { return new URIO<>(instance.fork().map(f -> f.mapK(new FunctionK<>() { @Override - public URIO apply(Kind, Void>, ? extends T> from) { + public URIO apply(Kind, R>, Void>, ? extends T> from) { return new URIO<>(from.fix(PureIOOf::narrowK)); } }))); @@ -221,7 +221,7 @@ public static URIO forked(Executor executor) { return async((env, callback) -> executor.execute(() -> callback.accept(Try.success(Unit.unit())))); } - public static URIO accessM(Function1, ? extends A>> map) { + public static URIO accessM(Function1, R>, ? extends A>> map) { return new URIO<>(PureIO.accessM(map.andThen(URIOOf::narrowK).andThen(URIO::toPureIO))); } @@ -233,39 +233,39 @@ public static URIO env() { return access(identity()); } - public static URIO parMap2(Kind, ? extends A> za, Kind, ? extends B> zb, + public static URIO parMap2(Kind, R>, ? extends A> za, Kind, R>, ? extends B> zb, Function2 mapper) { return parMap2(Future.DEFAULT_EXECUTOR, za, zb, mapper); } - public static URIO parMap2(Executor executor, Kind, ? extends A> za, Kind, ? extends B> zb, + public static URIO parMap2(Executor executor, Kind, R>, ? extends A> za, Kind, R>, ? extends B> zb, Function2 mapper) { return new URIO<>(PureIO.parMap2(executor, za.fix(URIOOf::narrowK).instance, zb.fix(URIOOf::narrowK).instance, mapper)); } - public static URIO> race(Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static URIO> race(Kind, R>, ? extends A> fa, Kind, R>, ? extends B> fb) { return race(Future.DEFAULT_EXECUTOR, fa, fb); } - public static URIO> race(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static URIO> race(Executor executor, Kind, R>, ? extends A> fa, Kind, R>, ? extends B> fb) { return racePair(executor, fa, fb).flatMap(either -> either.fold( ta -> ta.get2().cancel().fix(URIOOf.toURIO()).map(x -> Either.left(ta.get1())), tb -> tb.get1().cancel().fix(URIOOf.toURIO()).map(x -> Either.right(tb.get2())))); } - public static URIO, B>>, Tuple2, A>, B>>> - racePair(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { + public static URIO, R>, B>>, Tuple2, R>, A>, B>>> + racePair(Executor executor, Kind, R>, ? extends A> fa, Kind, R>, ? extends B> fb) { PureIO instance1 = fa.fix(URIOOf.toURIO()).instance.fix(PureIOOf::narrowK); PureIO instance2 = fb.fix(URIOOf.toURIO()).instance.fix(PureIOOf::narrowK); return new URIO<>(PureIO.racePair(executor, instance1, instance2).map( either -> either.bimap(a -> a.map2(f -> f.mapK(new FunctionK<>() { @Override - public URIO apply(Kind, Void>, ? extends T> from) { + public URIO apply(Kind, R>, Void>, ? extends T> from) { return new URIO<>(from.fix(PureIOOf::narrowK)); } })), b -> b.map1(f -> f.mapK(new FunctionK<>() { @Override - public URIO apply(Kind, Void>, ? extends T> from) { + public URIO apply(Kind, R>, Void>, ? extends T> from) { return new URIO<>(from.fix(PureIOOf::narrowK)); } }))))); @@ -307,7 +307,7 @@ public static URIO raiseError(Throwable throwable) { return new URIO<>(PureIO.fromEither(() -> { throw throwable; })); } - public static URIO defer(Producer, ? extends A>> lazy) { + public static URIO defer(Producer, R>, ? extends A>> lazy) { return new URIO<>(PureIO.defer(() -> lazy.andThen(URIOOf::narrowK).get().instance)); } @@ -341,29 +341,29 @@ public static URIO cancellable(Function2 consumer.andThen(URIO::toPureIO).apply(env, result -> cb1.accept(result.map(Either::right))))); } - public static URIO> traverse(Sequence, A>> sequence) { + public static URIO> traverse(Sequence, R>, A>> sequence) { return traverse(Future.DEFAULT_EXECUTOR, sequence); } - public static URIO> traverse(Executor executor, Sequence, A>> sequence) { + public static URIO> traverse(Executor executor, Sequence, R>, A>> sequence) { return sequence.foldLeft(pure(ImmutableList.empty()), - (Kind, Sequence> xs, Kind, A> a) -> parMap2(executor, xs, a, Sequence::append)); + (Kind, R>, Sequence> xs, Kind, R>, A> a) -> parMap2(executor, xs, a, Sequence::append)); } public static URIO bracket( - Kind, ? extends A> acquire, Function1, ? extends B>> use) { + Kind, R>, ? extends A> acquire, Function1, R>, ? extends B>> use) { return fold(PureIO.bracket(PureIO.redeem(acquire.fix(URIOOf::narrowK).instance), resource -> PureIO.redeem(use.andThen(URIOOf::narrowK).apply(resource).instance))); } - public static URIO bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, Consumer1 release) { + public static URIO bracket(Kind, R>, ? extends A> acquire, + Function1, R>, ? extends B>> use, Consumer1 release) { return fold(PureIO.bracket(PureIO.redeem(acquire.fix(URIOOf::narrowK).instance), resource -> PureIO.redeem(use.andThen(URIOOf::narrowK).apply(resource).instance), release)); } - public static URIO bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, Function1, Unit>> release) { + public static URIO bracket(Kind, R>, ? extends A> acquire, + Function1, R>, ? extends B>> use, Function1, R>, Unit>> release) { return fold(PureIO.bracket(PureIO.redeem(acquire.fix(URIOOf::narrowK).instance), resource -> PureIO.redeem(use.andThen(URIOOf::narrowK).apply(resource).instance), release.andThen(URIOOf::narrowK).andThen(URIO::toPureIO))); } diff --git a/free/src/main/java/com/github/tonivade/purefun/free/Cofree.java b/free/src/main/java/com/github/tonivade/purefun/free/Cofree.java index b4713ad61..7458fef81 100644 --- a/free/src/main/java/com/github/tonivade/purefun/free/Cofree.java +++ b/free/src/main/java/com/github/tonivade/purefun/free/Cofree.java @@ -21,7 +21,7 @@ import com.github.tonivade.purefun.typeclasses.Traverse; @HigherKind -public final class Cofree implements CofreeOf, Mappable, A> { +public final class Cofree implements CofreeOf, Mappable, F>, A> { private final Functor functor; private final A head; @@ -77,7 +77,7 @@ public Eval reduceToString(Traverse traverse, Operator2 join) } public Cofree transform(Function1 headMap, - Function1, ? extends Kind, ? extends B>> tailMap) { + Function1, ? extends Kind, F>, ? extends B>> tailMap) { return of(functor, transformHead(headMap), transformTail(tailMap)); } @@ -86,7 +86,7 @@ private B transformHead(Function1 headMap) { } private Eval>> transformTail( - Function1, ? extends Kind, ? extends B>> tailMap) { + Function1, ? extends Kind, F>, ? extends B>> tailMap) { return tail.map(t -> functor.map(t, tailMap.andThen(CofreeOf::narrowK))); } diff --git a/free/src/main/java/com/github/tonivade/purefun/free/Coyoneda.java b/free/src/main/java/com/github/tonivade/purefun/free/Coyoneda.java index 129c69fb1..37d82d9be 100644 --- a/free/src/main/java/com/github/tonivade/purefun/free/Coyoneda.java +++ b/free/src/main/java/com/github/tonivade/purefun/free/Coyoneda.java @@ -15,7 +15,7 @@ import com.github.tonivade.purefun.typeclasses.Functor; @HigherKind -public final class Coyoneda implements CoyonedaOf, Mappable, A>, B> { +public final class Coyoneda implements CoyonedaOf, Mappable, F>, A>, B> { private final Kind value; private final Function1 map; diff --git a/free/src/main/java/com/github/tonivade/purefun/free/Free.java b/free/src/main/java/com/github/tonivade/purefun/free/Free.java index 5d4ad6c13..25fe07077 100644 --- a/free/src/main/java/com/github/tonivade/purefun/free/Free.java +++ b/free/src/main/java/com/github/tonivade/purefun/free/Free.java @@ -21,7 +21,7 @@ import com.github.tonivade.purefun.typeclasses.Monad; @HigherKind -public sealed interface Free extends FreeOf, Bindable, A> { +public sealed interface Free extends FreeOf, Bindable, F>, A> { static Free pure(T value) { return new Pure<>(value); @@ -41,11 +41,11 @@ static Free defer(Producer> value) { } @SuppressWarnings("unchecked") - static Monad> monadF() { + static Monad, F>> monadF() { return FreeMonad.INSTANCE; } - static FunctionK> functionKF(FunctionK functionK) { + static FunctionK, G>> functionKF(FunctionK functionK) { return new FunctionK<>() { @Override public Free apply(Kind from) { @@ -60,10 +60,10 @@ default Free map(Function1 map) { } @Override - Free flatMap(Function1, ? extends R>> mapper); + Free flatMap(Function1, F>, ? extends R>> mapper); @Override - default Free andThen(Kind, ? extends R> next) { + default Free andThen(Kind, F>, ? extends R> next) { return flatMap(ignore -> next); } @@ -78,7 +78,7 @@ record Pure(A value) implements Free { } @Override - public Free flatMap(Function1, ? extends B>> map) { + public Free flatMap(Function1, F>, ? extends B>> map) { return new FlatMapped<>(this, map); } } @@ -90,13 +90,13 @@ record Suspend(Kind value) implements Free { } @Override - public Free flatMap(Function1, ? extends B>> map) { + public Free flatMap(Function1, F>, ? extends B>> map) { return new FlatMapped<>(this, map); } } record FlatMapped(Free value, - Function1, ? extends B>> next) implements Free { + Function1, F>, ? extends B>> next) implements Free { public FlatMapped { checkNonNull(value); @@ -104,7 +104,7 @@ record FlatMapped(Free value, } @Override - public Free flatMap(Function1, ? extends C>> map) { + public Free flatMap(Function1, F>, ? extends C>> map) { return new FlatMapped<>(value, free -> new FlatMapped<>(next.andThen(FreeOf::narrowK).apply(free), map)); } @@ -124,7 +124,7 @@ private Kind, A>> foldStep(Monad monad, FunctionK extends Monad> { +interface FreeMonad extends Monad, F>> { @SuppressWarnings("rawtypes") FreeMonad INSTANCE = new FreeMonad() {}; @@ -136,7 +136,7 @@ default Free pure(T value) { @Override default Free flatMap( - Kind, ? extends T> value, Function1, ? extends R>> map) { + Kind, F>, ? extends T> value, Function1, F>, ? extends R>> map) { return value.fix(toFree()).flatMap(map.andThen(FreeOf::narrowK)); } } diff --git a/free/src/main/java/com/github/tonivade/purefun/free/FreeAp.java b/free/src/main/java/com/github/tonivade/purefun/free/FreeAp.java index e857587f1..4f21dc9a0 100644 --- a/free/src/main/java/com/github/tonivade/purefun/free/FreeAp.java +++ b/free/src/main/java/com/github/tonivade/purefun/free/FreeAp.java @@ -17,12 +17,12 @@ import com.github.tonivade.purefun.core.Applicable; import com.github.tonivade.purefun.core.Function1; -import com.github.tonivade.purefun.type.Const_; +import com.github.tonivade.purefun.type.Const; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.FunctionK; @HigherKind -public sealed interface FreeAp extends FreeApOf, Applicable, A> { +public sealed interface FreeAp extends FreeApOf, Applicable, F>, A> { @Override default FreeAp map(Function1 mapper) { @@ -34,7 +34,7 @@ default FreeAp map(Function1 mapper) { } @Override - default FreeAp ap(Kind, ? extends Function1> apply) { + default FreeAp ap(Kind, F>, ? extends Function1> apply) { if (apply instanceof Pure> pure) { return map(pure.value); } @@ -50,11 +50,11 @@ default FreeAp compile(FunctionK transformer) { } default FreeAp flatCompile( - FunctionK> functionK, Applicative> applicative) { + FunctionK, G>> functionK, Applicative, G>> applicative) { return foldMap(functionK, applicative).fix(toFreeAp()); } - default M analyze(FunctionK> functionK, Applicative> applicative) { + default M analyze(FunctionK, M>> functionK, Applicative, M>> applicative) { return foldMap(functionK, applicative).fix(toConst()).value(); } @@ -123,12 +123,12 @@ static FreeAp lift(Kind value) { return new Lift<>(value); } - static FreeAp apply(Kind, ? extends T> value, - Kind, ? extends Function1> mapper) { + static FreeAp apply(Kind, F>, ? extends T> value, + Kind, F>, ? extends Function1> mapper) { return new Apply<>(value.fix(toFreeAp()), mapper.fix(toFreeAp())); } - static FunctionK> functionKF(FunctionK functionK) { + static FunctionK, G>> functionKF(FunctionK functionK) { return new FunctionK<>() { @Override public FreeAp apply(Kind from) { @@ -138,7 +138,7 @@ public FreeAp apply(Kind from) { } @SuppressWarnings("unchecked") - static Applicative> applicativeF() { + static Applicative, F>> applicativeF() { return FreeApplicative.INSTANCE; } @@ -200,7 +200,7 @@ record CurriedFunction(Kind> value, int remaining) { } } -interface FreeApplicative extends Applicative> { +interface FreeApplicative extends Applicative, F>> { @SuppressWarnings("rawtypes") FreeApplicative INSTANCE = new FreeApplicative() {}; @@ -212,8 +212,8 @@ default FreeAp pure(T value) { @Override default FreeAp ap( - Kind, ? extends T> value, - Kind, ? extends Function1> apply) { + Kind, F>, ? extends T> value, + Kind, F>, ? extends Function1> apply) { return FreeAp.apply(value, apply); } } diff --git a/free/src/main/java/com/github/tonivade/purefun/free/Trampoline.java b/free/src/main/java/com/github/tonivade/purefun/free/Trampoline.java index 26fe11f7a..1dfee12a4 100644 --- a/free/src/main/java/com/github/tonivade/purefun/free/Trampoline.java +++ b/free/src/main/java/com/github/tonivade/purefun/free/Trampoline.java @@ -18,7 +18,7 @@ import com.github.tonivade.purefun.type.Either; @HigherKind -public sealed interface Trampoline extends TrampolineOf, Bindable { +public sealed interface Trampoline extends TrampolineOf, Bindable, T> { @Override default Trampoline map(Function1 map) { @@ -28,7 +28,7 @@ default Trampoline map(Function1 map) { } @Override - default Trampoline flatMap(Function1> map) { + default Trampoline flatMap(Function1, ? extends R>> map) { return resume().fold( next -> more(() -> next.flatMap(map)), map.andThen(TrampolineOf::narrowK)); diff --git a/free/src/main/java/com/github/tonivade/purefun/free/Yoneda.java b/free/src/main/java/com/github/tonivade/purefun/free/Yoneda.java index e85fc0d59..a19bfe569 100644 --- a/free/src/main/java/com/github/tonivade/purefun/free/Yoneda.java +++ b/free/src/main/java/com/github/tonivade/purefun/free/Yoneda.java @@ -15,7 +15,7 @@ import com.github.tonivade.purefun.typeclasses.Functor; @HigherKind -public sealed interface Yoneda extends YonedaOf, Mappable, A> { +public sealed interface Yoneda extends YonedaOf, Mappable, F>, A> { Kind apply(Function1 map); diff --git a/free/src/test/java/com/github/tonivade/purefun/free/CofreeTest.java b/free/src/test/java/com/github/tonivade/purefun/free/CofreeTest.java index 06c2738c4..9594c2c43 100644 --- a/free/src/test/java/com/github/tonivade/purefun/free/CofreeTest.java +++ b/free/src/test/java/com/github/tonivade/purefun/free/CofreeTest.java @@ -25,9 +25,7 @@ import com.github.tonivade.purefun.instances.OptionInstances; import com.github.tonivade.purefun.type.Eval; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; import com.github.tonivade.purefun.typeclasses.For; @ExtendWith(MockitoExtension.class) @@ -35,9 +33,9 @@ public class CofreeTest { @Test public void testMap() { - Cofree cofree = Cofree.unfold(IdInstances.functor(), 0, a -> Id.of(a + 1)).map(x -> x * 2); + Cofree, Integer> cofree = Cofree.unfold(IdInstances.functor(), 0, a -> Id.of(a + 1)).map(x -> x * 2); - Id, Cofree, Cofree, Cofree>> tuple4Id = + Id, Integer>, Cofree, Integer>, Cofree, Integer>, Cofree, Integer>>> tuple4Id = For.with(IdInstances.monad()) .then(cofree.tailForced()) .flatMap(Cofree::tailForced) @@ -50,7 +48,7 @@ public void testMap() { @Test public void testFold() { - Cofree cofree = Cofree.unfold(OptionInstances.functor(), 0, + Cofree, Integer> cofree = Cofree.unfold(OptionInstances.functor(), 0, a -> (a > 100) ? Option.none() : Option.some(a + 1)); assertEquals(5151, @@ -63,7 +61,7 @@ public void testRun(@Mock Operator1 plus1) { when(plus1.apply(anyInt())) .thenReturn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - Cofree cofree = Cofree.unfold(OptionInstances.functor(), 0, + Cofree, Integer> cofree = Cofree.unfold(OptionInstances.functor(), 0, a -> (a < 10) ? Option.some(plus1.apply(a)) : Option.none()); verify(plus1, never()).apply(anyInt()); // nothing executed diff --git a/free/src/test/java/com/github/tonivade/purefun/free/CoyonedaTest.java b/free/src/test/java/com/github/tonivade/purefun/free/CoyonedaTest.java index 1eb22e44a..9d187b0cd 100644 --- a/free/src/test/java/com/github/tonivade/purefun/free/CoyonedaTest.java +++ b/free/src/test/java/com/github/tonivade/purefun/free/CoyonedaTest.java @@ -12,17 +12,16 @@ import com.github.tonivade.purefun.core.Operator1; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; public class CoyonedaTest { - + private final Operator1 concat = string -> string.concat(string); @Test public void coyoneda() { - Coyoneda coyoneda = Coyoneda.of(Option.some("string"), identity()); + Coyoneda, String, String> coyoneda = Coyoneda.of(Option.some("string"), identity()); - Coyoneda result = coyoneda.map(concat).map(concat); + Coyoneda, String, String> result = coyoneda.map(concat).map(concat); assertEquals(Option.some("stringstringstringstring"), result.run(functor())); } diff --git a/free/src/test/java/com/github/tonivade/purefun/free/EitherKTest.java b/free/src/test/java/com/github/tonivade/purefun/free/EitherKTest.java index 0e719931d..aa0a5fb0d 100644 --- a/free/src/test/java/com/github/tonivade/purefun/free/EitherKTest.java +++ b/free/src/test/java/com/github/tonivade/purefun/free/EitherKTest.java @@ -13,26 +13,22 @@ import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.Producer; -import com.github.tonivade.purefun.core.Producer_; import com.github.tonivade.purefun.instances.IdInstances; import com.github.tonivade.purefun.instances.OptionInstances; import com.github.tonivade.purefun.instances.ProducerInstances; import com.github.tonivade.purefun.instances.TryInstances; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; import com.github.tonivade.purefun.type.Option; import com.github.tonivade.purefun.type.OptionOf; -import com.github.tonivade.purefun.type.Option_; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.type.TryOf; -import com.github.tonivade.purefun.type.Try_; import com.github.tonivade.purefun.typeclasses.FunctionK; class EitherKTest { @Test public void left() { - EitherK left = EitherK.left(Option.some("hello")); + EitherK, Try, String> left = EitherK.left(Option.some("hello")); assertAll( () -> assertTrue(left.isLeft()), @@ -44,7 +40,7 @@ public void left() { @Test public void right() { - EitherK right = EitherK.right(Try.success("hello")); + EitherK, Try, String> right = EitherK.right(Try.success("hello")); assertAll( () -> assertFalse(right.isLeft()), @@ -56,8 +52,8 @@ public void right() { @Test public void extract() { - EitherK left = EitherK.left(Id.of("hola")); - EitherK right = EitherK.right(Producer.cons("hola")); + EitherK, Producer, String> left = EitherK.left(Id.of("hola")); + EitherK, Producer, String> right = EitherK.right(Producer.cons("hola")); assertAll( () -> assertEquals("hola", left.extract(IdInstances.comonad(), ProducerInstances.comonad())), @@ -67,7 +63,7 @@ public void extract() { @Test public void coflatMap() { - EitherK left = EitherK.left(Id.of("hola")); + EitherK, Producer, String> left = EitherK.left(Id.of("hola")); assertAll( () -> assertEquals( @@ -81,29 +77,29 @@ public void coflatMap() { @Test public void mapLeft() { - EitherK eitherK = EitherK.left(Option.some("hello")); + EitherK, Try, String> eitherK = EitherK.left(Option.some("hello")); - EitherK result = eitherK.map(OptionInstances.functor(), TryInstances.functor(), String::length); + EitherK, Try, Integer> result = eitherK.map(OptionInstances.functor(), TryInstances.functor(), String::length); assertEquals(Option.some(5), result.getLeft()); } @Test public void mapRight() { - EitherK eitherK = EitherK.right(Try.success("hello")); + EitherK, Try, String> eitherK = EitherK.right(Try.success("hello")); - EitherK result = eitherK.map(OptionInstances.functor(), TryInstances.functor(), String::length); + EitherK, Try, Integer> result = eitherK.map(OptionInstances.functor(), TryInstances.functor(), String::length); assertEquals(Try.success(5), result.getRight()); } @Test public void mapK() { - EitherK eitherK = EitherK.right(Try.success("hello")); + EitherK, Try, String> eitherK = EitherK.right(Try.success("hello")); - EitherK result = eitherK.mapK(new FunctionK<>() { + EitherK, Option, String> result = eitherK.mapK(new FunctionK<>() { @Override - public Option apply(Kind from) { + public Option apply(Kind, ? extends T> from) { return from.fix(TryOf::narrowK).toOption(); } }); @@ -113,11 +109,11 @@ public Option apply(Kind from) { @Test public void mapLeftK() { - EitherK eitherK = EitherK.left(Option.some("hello")); + EitherK, Try, String> eitherK = EitherK.left(Option.some("hello")); - EitherK result = eitherK.mapLeftK(new FunctionK<>() { + EitherK, Try, String> result = eitherK.mapLeftK(new FunctionK<>() { @Override - public Try apply(Kind from) { + public Try apply(Kind, ? extends T> from) { return from.fix(OptionOf::narrowK).fold(Try::failure, Try::success); } }); @@ -127,8 +123,8 @@ public Try apply(Kind from) { @Test public void swap() { - EitherK original = EitherK.left(Option.some("hello")); - EitherK expected = EitherK.right(Option.some("hello")); + EitherK, Try, String> original = EitherK.left(Option.some("hello")); + EitherK, Option, String> expected = EitherK.right(Option.some("hello")); assertAll( () -> assertEquals(expected, original.swap()), diff --git a/free/src/test/java/com/github/tonivade/purefun/free/FreeAlgTest.java b/free/src/test/java/com/github/tonivade/purefun/free/FreeAlgTest.java index 7fefd7ae0..c3851296d 100644 --- a/free/src/test/java/com/github/tonivade/purefun/free/FreeAlgTest.java +++ b/free/src/test/java/com/github/tonivade/purefun/free/FreeAlgTest.java @@ -15,22 +15,22 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.instances.IOInstances; -import com.github.tonivade.purefun.monad.IO_; +import com.github.tonivade.purefun.monad.IO; import com.github.tonivade.purefun.runtimes.ConsoleExecutor; import com.github.tonivade.purefun.typeclasses.Console; import com.github.tonivade.purefun.typeclasses.FunctionK; public class FreeAlgTest { - private static Free, EmailAlg_>, String> read() { + private static Free, ConsoleAlg>, EmailAlg>, String> read() { return Free.inject(injectEitherKLeft(), new ConsoleAlg.ReadLine()); } - private static Free, EmailAlg_>, Unit> write(String value) { + private static Free, ConsoleAlg>, EmailAlg>, Unit> write(String value) { return Free.inject(injectEitherKLeft(), new ConsoleAlg.WriteLine(value)); } - private static Free, EmailAlg_>, Unit> send(String to, String content) { + private static Free, ConsoleAlg>, EmailAlg>, Unit> send(String to, String content) { return Free.inject(injectEitherKRight(injectReflexive()), new EmailAlg.SendEmail(to, content)); } @@ -48,31 +48,31 @@ public void algebra() { } @SuppressWarnings("unchecked") - private static FunctionK, EmailAlg_>, IO_> interpreter() { - final Console console = IOInstances.console(); + private static FunctionK, ConsoleAlg>, EmailAlg>, IO> interpreter() { + final Console> console = IOInstances.console(); return new FunctionK<>() { @Override - public Kind apply(Kind, EmailAlg_>, ? extends T> from) { - return from.fix(EitherKOf::narrowK).foldK( + public Kind, T> apply(Kind, ConsoleAlg>, EmailAlg>, ? extends T> from) { + return from.fix(EitherKOf::, EmailAlg, T>narrowK).foldK( new FunctionK<>() { @Override - public Kind apply(Kind kind) { + public Kind, X> apply(Kind, ? extends X> kind) { ConsoleAlg consoleAlg = kind.fix(ConsoleAlgOf::narrowK); if (consoleAlg instanceof ConsoleAlg.ReadLine) { - return (Kind) console.readln(); + return (Kind, X>) console.readln(); } if (consoleAlg instanceof ConsoleAlg.WriteLine writeLine) { - return (Kind) console.println(writeLine.line()); + return (Kind, X>) console.println(writeLine.line()); } throw new IllegalStateException(); } }, new FunctionK<>() { @Override - public Kind apply(Kind kind) { + public Kind, X> apply(Kind, ? extends X> kind) { EmailAlg emailAlg = kind.fix(EmailAlgOf::narrowK); if (emailAlg instanceof EmailAlg.SendEmail sendEmail) { - return (Kind) console.println( + return (Kind, X>) console.println( "email to " + sendEmail.to() + " with content " + sendEmail.content()); } throw new IllegalStateException(); diff --git a/free/src/test/java/com/github/tonivade/purefun/free/FreeApTest.java b/free/src/test/java/com/github/tonivade/purefun/free/FreeApTest.java index 592d0d61a..60eb2d331 100644 --- a/free/src/test/java/com/github/tonivade/purefun/free/FreeApTest.java +++ b/free/src/test/java/com/github/tonivade/purefun/free/FreeApTest.java @@ -20,20 +20,18 @@ import com.github.tonivade.purefun.instances.ConstInstances; import com.github.tonivade.purefun.instances.IdInstances; import com.github.tonivade.purefun.type.Const; -import com.github.tonivade.purefun.type.Const_; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.FunctionK; import com.github.tonivade.purefun.typeclasses.Monoid; public class FreeApTest { - private final Applicative> applicative = FreeAp.applicativeF(); + private final Applicative, DSL>> applicative = FreeAp.applicativeF(); @Test public void map() { - FreeAp map = applicative.map(DSL.readInt(4), i -> i + 1).fix(toFreeAp()); + FreeAp, Integer> map = applicative.map(DSL.readInt(4), i -> i + 1).fix(toFreeAp()); Id foldMap = map.foldMap(idTransform(), IdInstances.applicative()).fix(toId()); @@ -42,8 +40,8 @@ public void map() { @Test public void ap() { - FreeAp freeAp = FreeAp.lift(new ReadInt(123)); - FreeAp> apply = FreeAp.pure(Object::toString); + FreeAp, Integer> freeAp = FreeAp.lift(new ReadInt(123)); + FreeAp, Function1> apply = FreeAp.pure(Object::toString); Id foldMap = freeAp.ap(apply) .map(String::length) @@ -54,7 +52,7 @@ public void ap() { @Test public void lift() { - FreeAp> tuple = + FreeAp, Tuple5> tuple = applicative.mapN( DSL.readInt(2), DSL.readBoolean(false), @@ -64,14 +62,14 @@ public void lift() { Tuple::of ).fix(toFreeAp()); - Kind> map = + Kind, Tuple5> map = tuple.foldMap(idTransform(), IdInstances.applicative()); assertEquals(Id.of(Tuple.of(2, false, 2.1, "hola mundo", unit())), map.fix(toId())); } @Test public void pure() { - FreeAp> tuple = + FreeAp, Tuple5> tuple = applicative.mapN( applicative.pure(1), applicative.pure("string"), @@ -81,7 +79,7 @@ public void pure() { Tuple::of ).fix(toFreeAp()); - Kind> map = + Kind, Tuple5> map = tuple.foldMap(idTransform(), IdInstances.applicative()); assertEquals(Id.of(Tuple.of(1, "string", 1.1, true, unit())), map.fix(toId())); @@ -89,9 +87,9 @@ public void pure() { @Test public void compile() { - FreeAp readInt = FreeAp.pure(5); + FreeAp, Integer> readInt = FreeAp.pure(5); - FreeAp compile = readInt.compile(idTransform()); + FreeAp, Integer> compile = readInt.compile(idTransform()); Id fold = compile.fold(IdInstances.applicative()).fix(toId()); assertEquals(5, fold.value()); @@ -99,7 +97,7 @@ public void compile() { @Test public void analyze() { - FreeAp> tuple = + FreeAp, Tuple5> tuple = applicative.mapN( DSL.readInt(2), DSL.readBoolean(false), @@ -120,19 +118,19 @@ public void analyze() { """, analize); } - private FunctionK idTransform() { + private FunctionK, Id> idTransform() { return new FunctionK<>() { @Override - public Kind apply(Kind from) { + public Kind, T> apply(Kind, ? extends T> from) { return Id.of(from.fix(DSLOf::narrowK).value()); } }; } - private FunctionK> constTransform() { + private FunctionK, Kind, String>> constTransform() { return new FunctionK<>() { @Override - public Const apply(Kind from) { + public Const apply(Kind, ? extends T> from) { DSL dsl = from.fix(DSLOf::narrowK); return Const.of(dsl.getClass().getSimpleName() + "(" + dsl.value() + ")\n"); } @@ -145,23 +143,23 @@ sealed interface DSL extends DSLOf { A value(); - static FreeAp readInt(int value) { + static FreeAp, Integer> readInt(int value) { return FreeAp.lift(new ReadInt(value)); } - static FreeAp readString(String value) { + static FreeAp, String> readString(String value) { return FreeAp.lift(new ReadString(value)); } - static FreeAp readBoolean(boolean value) { + static FreeAp, Boolean> readBoolean(boolean value) { return FreeAp.lift(new ReadBoolean(value)); } - static FreeAp readDouble(double value) { + static FreeAp, Double> readDouble(double value) { return FreeAp.lift(new ReadDouble(value)); } - static FreeAp readUnit() { + static FreeAp, Unit> readUnit() { return FreeAp.lift(new ReadUnit()); } } diff --git a/free/src/test/java/com/github/tonivade/purefun/free/FreeTest.java b/free/src/test/java/com/github/tonivade/purefun/free/FreeTest.java index 8e4e76376..f79ce54d4 100644 --- a/free/src/test/java/com/github/tonivade/purefun/free/FreeTest.java +++ b/free/src/test/java/com/github/tonivade/purefun/free/FreeTest.java @@ -13,7 +13,7 @@ import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.data.ImmutableList; import com.github.tonivade.purefun.instances.StateInstances; -import com.github.tonivade.purefun.monad.IO_; +import com.github.tonivade.purefun.monad.IO; import com.github.tonivade.purefun.monad.State; import com.github.tonivade.purefun.monad.StateOf; import com.github.tonivade.purefun.runtimes.ConsoleExecutor; @@ -21,7 +21,7 @@ public class FreeTest { - private final Free echo = + private final Free, Unit> echo = IOProgram.write("what's your name?") .andThen(IOProgram.read()) .flatMap(text -> IOProgram.write("Hello " + text)) @@ -40,7 +40,7 @@ public void interpretState() { @Test public void interpretIO() { - var foldMap = echo.foldMap(Instances.monad(), new IOProgramToIO()); + var foldMap = echo.foldMap(Instances.>monad(), new IOProgramToIO()); var executor = new ConsoleExecutor().read("Toni"); diff --git a/free/src/test/java/com/github/tonivade/purefun/free/IOProgram.java b/free/src/test/java/com/github/tonivade/purefun/free/IOProgram.java index 865eb94dc..1c9f79b13 100644 --- a/free/src/test/java/com/github/tonivade/purefun/free/IOProgram.java +++ b/free/src/test/java/com/github/tonivade/purefun/free/IOProgram.java @@ -15,20 +15,18 @@ import com.github.tonivade.purefun.instances.IOInstances; import com.github.tonivade.purefun.instances.StateInstances; import com.github.tonivade.purefun.monad.IO; -import com.github.tonivade.purefun.monad.IO_; import com.github.tonivade.purefun.monad.State; -import com.github.tonivade.purefun.monad.State_; import com.github.tonivade.purefun.typeclasses.Console; import com.github.tonivade.purefun.typeclasses.FunctionK; @HigherKind public sealed interface IOProgram extends IOProgramOf { - static Free read() { + static Free, String> read() { return liftF(new IOProgram.Read()); } - static Free write(String value) { + static Free, Unit> write(String value) { return liftF(new IOProgram.Write(value)); } @@ -45,12 +43,12 @@ record Write(String value) implements IOProgram { } @SuppressWarnings("unchecked") -class IOProgramToState implements FunctionK>> { +class IOProgramToState implements FunctionK, Kind, ImmutableList>> { - private final Console>> console = StateInstances.console(); + private final Console, ImmutableList>> console = StateInstances.console(); @Override - public Kind>, X> apply(Kind from) { + public Kind, ImmutableList>, X> apply(Kind, ? extends X> from) { return switch (from.fix(toIOProgram())) { case IOProgram.Read() -> (State, X>) console.readln(); case IOProgram.Write(var value) -> (State, X>) console.println(value); @@ -59,12 +57,12 @@ public Kind>, X> apply(Kind { +class IOProgramToIO implements FunctionK, IO> { - private final Console console = IOInstances.console(); + private final Console> console = IOInstances.console(); @Override - public Kind apply(Kind from) { + public Kind, X> apply(Kind, ? extends X> from) { return switch (from.fix(toIOProgram())) { case IOProgram.Read() -> (IO) console.readln(); case IOProgram.Write(var value) -> (IO) console.println(value); diff --git a/free/src/test/java/com/github/tonivade/purefun/free/YonedaTest.java b/free/src/test/java/com/github/tonivade/purefun/free/YonedaTest.java index 9434685d9..36038b627 100644 --- a/free/src/test/java/com/github/tonivade/purefun/free/YonedaTest.java +++ b/free/src/test/java/com/github/tonivade/purefun/free/YonedaTest.java @@ -12,7 +12,6 @@ import com.github.tonivade.purefun.core.Operator1; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; public class YonedaTest { @@ -20,9 +19,9 @@ public class YonedaTest { @Test public void yoneda() { - Yoneda yoneda = Yoneda.of(some("string"), functor()); + Yoneda, String> yoneda = Yoneda.of(some("string"), functor()); - Yoneda result = yoneda.map(concat).map(concat); + Yoneda, String> result = yoneda.map(concat).map(concat); assertEquals(Option.some("stringstringstringstring"), result.lower()); } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/CofreeInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/CofreeInstances.java index ab790fbe1..ff0982ab6 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/CofreeInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/CofreeInstances.java @@ -9,46 +9,45 @@ import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.free.Cofree; import com.github.tonivade.purefun.free.CofreeOf; -import com.github.tonivade.purefun.free.Cofree_; import com.github.tonivade.purefun.typeclasses.Comonad; import com.github.tonivade.purefun.typeclasses.Functor; @SuppressWarnings("unchecked") public interface CofreeInstances { - static Functor> functor() { + static Functor, F>> functor() { return CofreeFunctor.INSTANCE; } - static Comonad> comonad() { + static Comonad, F>> comonad() { return CofreeComonad.INSTANCE; } } -interface CofreeFunctor extends Functor> { +interface CofreeFunctor extends Functor, F>> { @SuppressWarnings("rawtypes") CofreeFunctor INSTANCE = new CofreeFunctor() {}; @Override - default Cofree map(Kind, ? extends T> value, Function1 map) { + default Cofree map(Kind, F>, ? extends T> value, Function1 map) { return value.fix(CofreeOf::narrowK).map(map); } } -interface CofreeComonad extends Comonad>, CofreeFunctor { +interface CofreeComonad extends Comonad, F>>, CofreeFunctor { @SuppressWarnings("rawtypes") CofreeComonad INSTANCE = new CofreeComonad() { }; @Override - default A extract(Kind, ? extends A> value) { + default A extract(Kind, F>, ? extends A> value) { return value.fix(CofreeOf::narrowK).extract(); } @Override default Cofree coflatMap( - Kind, ? extends A> value, Function1, ? extends A>, ? extends B> map) { + Kind, F>, ? extends A> value, Function1, F>, ? extends A>, ? extends B> map) { return value.fix(CofreeOf::narrowK).coflatMap(map); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/ConstInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/ConstInstances.java index 9519ae959..4bd29a3d9 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/ConstInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/ConstInstances.java @@ -11,7 +11,6 @@ import com.github.tonivade.purefun.core.Function2; import com.github.tonivade.purefun.type.Const; import com.github.tonivade.purefun.type.ConstOf; -import com.github.tonivade.purefun.type.Const_; import com.github.tonivade.purefun.type.Eval; import com.github.tonivade.purefun.type.EvalOf; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -24,43 +23,43 @@ @SuppressWarnings("unchecked") public interface ConstInstances { - static Eq, A>> eq(Eq eq) { + static Eq, T>, A>> eq(Eq eq) { return (a, b) -> eq.eqv(a.fix(ConstOf::narrowK).value(), a.fix(ConstOf::narrowK).value()); } - static Functor> functor() { + static Functor, T>> functor() { return ConstFunctor.INSTANCE; } - static Applicative> applicative(Monoid monoid) { + static Applicative, T>> applicative(Monoid monoid) { return ConstApplicative.instance(monoid); } - static Foldable> foldable() { + static Foldable, T>> foldable() { return ConstFoldable.INSTANCE; } - static Traverse> traverse() { + static Traverse, T>> traverse() { return ConstTraverse.INSTANCE; } - static Contravariant> contravariant() { + static Contravariant, T>> contravariant() { return ConstContravariant.INSTANCE; } } -interface ConstFunctor extends Functor> { +interface ConstFunctor extends Functor, T>> { @SuppressWarnings("rawtypes") ConstFunctor INSTANCE = new ConstFunctor() {}; @Override - default Kind, B> map(Kind, ? extends A> value, Function1 map) { + default Kind, T>, B> map(Kind, T>, ? extends A> value, Function1 map) { return value.fix(ConstOf::narrowK).retag(); } } -interface ConstApplicative extends Applicative> { +interface ConstApplicative extends Applicative, T>> { static ConstApplicative instance(Monoid monoid) { return () -> monoid; @@ -75,52 +74,52 @@ default Const pure(A value) { @Override default Const ap( - Kind, ? extends A> value, - Kind, ? extends Function1> apply) { + Kind, T>, ? extends A> value, + Kind, T>, ? extends Function1> apply) { return Const.of(monoid().combine( apply.fix(ConstOf::narrowK).retag().value(), value.fix(ConstOf::narrowK).retag().value())); } } -interface ConstContravariant extends Contravariant> { +interface ConstContravariant extends Contravariant, T>> { @SuppressWarnings("rawtypes") ConstContravariant INSTANCE = new ConstContravariant() {}; @Override - default Const contramap(Kind, ? extends A> value, Function1 map) { + default Const contramap(Kind, T>, ? extends A> value, Function1 map) { return value.fix(ConstOf::narrowK).retag(); } } -interface ConstFoldable extends Foldable> { +interface ConstFoldable extends Foldable, T>> { @SuppressWarnings("rawtypes") ConstFoldable INSTANCE = new ConstFoldable() {}; @Override - default B foldLeft(Kind, ? extends A> value, B initial, + default B foldLeft(Kind, T>, ? extends A> value, B initial, Function2 mapper) { return initial; } @Override default Eval foldRight( - Kind, ? extends A> value, Eval initial, + Kind, T>, ? extends A> value, Eval initial, Function2, ? extends Eval> mapper) { return EvalOf.narrowK(initial); } } -interface ConstTraverse extends Traverse>, ConstFoldable { +interface ConstTraverse extends Traverse, T>>, ConstFoldable { @SuppressWarnings("rawtypes") ConstTraverse INSTANCE = new ConstTraverse() {}; @Override - default Kind, B>> traverse( - Applicative applicative, Kind, A> value, Function1> mapper) { + default Kind, T>, B>> traverse( + Applicative applicative, Kind, T>, A> value, Function1> mapper) { return applicative.pure(value.fix(ConstOf::narrowK).retag()); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/ControlInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/ControlInstances.java index e0248de75..9d4123cd8 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/ControlInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/ControlInstances.java @@ -9,18 +9,17 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.control.Control; import com.github.tonivade.purefun.control.ControlOf; -import com.github.tonivade.purefun.control.Control_; import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.typeclasses.Monad; public interface ControlInstances { - static Monad monad() { + static Monad> monad() { return ControlMonad.INSTANCE; } } -interface ControlMonad extends Monad { +interface ControlMonad extends Monad> { ControlMonad INSTANCE = new ControlMonad() {}; @@ -31,7 +30,7 @@ default Control pure(T value) { @Override default Control flatMap( - Kind value, Function1> map) { + Kind, ? extends T> value, Function1, ? extends R>> map) { return value.fix(toControl()).flatMap(map.andThen(ControlOf::narrowK)); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/EIOInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/EIOInstances.java index 4341b43bd..f0990defb 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/EIOInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/EIOInstances.java @@ -18,7 +18,6 @@ import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.effect.EIO; import com.github.tonivade.purefun.effect.EIOOf; -import com.github.tonivade.purefun.effect.EIO_; import com.github.tonivade.purefun.effect.UIO; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Try; @@ -38,60 +37,60 @@ @SuppressWarnings("unchecked") public interface EIOInstances { - static Functor> functor() { + static Functor, E>> functor() { return EIOFunctor.INSTANCE; } - static Applicative> applicative() { + static Applicative, E>> applicative() { return EIOApplicative.INSTANCE; } - static Monad> monad() { + static Monad, E>> monad() { return EIOMonad.INSTANCE; } - static MonadError, E> monadError() { + static MonadError, E>, E> monadError() { return EIOMonadError.INSTANCE; } - static MonadThrow> monadThrow() { + static MonadThrow, Throwable>> monadThrow() { return EIOMonadThrow.INSTANCE; } - static MonadDefer> monadDefer() { + static MonadDefer, Throwable>> monadDefer() { return EIOMonadDefer.INSTANCE; } - static Async> async() { + static Async, Throwable>> async() { return EIOAsync.INSTANCE; } - static Concurrent> concurrent() { + static Concurrent, Throwable>> concurrent() { return concurrent(Future.DEFAULT_EXECUTOR); } - static Concurrent> concurrent(Executor executor) { + static Concurrent, Throwable>> concurrent(Executor executor) { return EIOConcurrent.instance(executor); } - static Runtime> runtime() { + static Runtime, E>> runtime() { return EIORuntime.INSTANCE; } } -interface EIOFunctor extends Functor> { +interface EIOFunctor extends Functor, E>> { @SuppressWarnings("rawtypes") EIOFunctor INSTANCE = new EIOFunctor() {}; @Override default EIO - map(Kind, ? extends A> value, Function1 map) { + map(Kind, E>, ? extends A> value, Function1 map) { return EIOOf.narrowK(value).map(map); } } -interface EIOPure extends Applicative> { +interface EIOPure extends Applicative, E>> { @Override default EIO pure(A value) { @@ -106,26 +105,26 @@ interface EIOApplicative extends EIOPure { @Override default EIO - ap(Kind, ? extends A> value, - Kind, ? extends Function1> apply) { + ap(Kind, E>, ? extends A> value, + Kind, E>, ? extends Function1> apply) { return value.fix(EIOOf::narrowK).ap(apply.fix(EIOOf::narrowK)); } } -interface EIOMonad extends EIOPure, Monad> { +interface EIOMonad extends EIOPure, Monad, E>> { @SuppressWarnings("rawtypes") EIOMonad INSTANCE = new EIOMonad() {}; @Override default EIO - flatMap(Kind, ? extends A> value, - Function1, ? extends B>> map) { + flatMap(Kind, E>, ? extends A> value, + Function1, E>, ? extends B>> map) { return value.fix(toEIO()).flatMap(map.andThen(EIOOf::narrowK)); } } -interface EIOMonadError extends EIOMonad, MonadError, E> { +interface EIOMonadError extends EIOMonad, MonadError, E>, E> { @SuppressWarnings("rawtypes") EIOMonadError INSTANCE = new EIOMonadError() {}; @@ -137,8 +136,8 @@ default EIO raiseError(E error) { @Override default EIO handleErrorWith( - Kind, A> value, - Function1, ? extends A>> handler) { + Kind, E>, A> value, + Function1, E>, ? extends A>> handler) { // XXX: java8 fails to infer types, I have to do this in steps Function1> mapError = handler.andThen(EIOOf::narrowK); Function1> map = EIO::pure; @@ -149,33 +148,33 @@ default EIO handleErrorWith( interface EIOMonadThrow extends EIOMonadError, - MonadThrow> { + MonadThrow, Throwable>> { EIOMonadThrow INSTANCE = new EIOMonadThrow() {}; } -interface EIODefer extends Defer> { +interface EIODefer extends Defer, E>> { @Override default EIO - defer(Producer, ? extends A>> defer) { + defer(Producer, E>, ? extends A>> defer) { return EIO.defer(defer::get); } } -interface EIOBracket extends EIOMonadError, Bracket, E> { +interface EIOBracket extends EIOMonadError, Bracket, E>, E> { @Override default EIO - bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, - Function1, Unit>> release) { + bracket(Kind, E>, ? extends A> acquire, + Function1, E>, ? extends B>> use, + Function1, E>, Unit>> release) { return EIO.bracket(acquire, use, release); } } interface EIOMonadDefer - extends MonadDefer>, EIODefer, EIOBracket { + extends MonadDefer, Throwable>>, EIODefer, EIOBracket { EIOMonadDefer INSTANCE = new EIOMonadDefer() {}; @@ -185,17 +184,17 @@ default EIO sleep(Duration duration) { } } -interface EIOAsync extends Async>, EIOMonadDefer { +interface EIOAsync extends Async, Throwable>>, EIOMonadDefer { EIOAsync INSTANCE = new EIOAsync() {}; @Override - default EIO asyncF(Function1>, Kind, Unit>> consumer) { + default EIO asyncF(Function1>, Kind, Throwable>, Unit>> consumer) { return EIO.cancellable(cb -> consumer.andThen(EIOOf::narrowK).apply(e -> cb.accept(Try.success(e.toEither())))); } } -interface EIOConcurrent extends EIOAsync, Concurrent> { +interface EIOConcurrent extends EIOAsync, Concurrent, Throwable>> { static EIOConcurrent instance(Executor executor) { return () -> executor; @@ -204,40 +203,40 @@ static EIOConcurrent instance(Executor executor) { Executor executor(); @Override - default EIO, B>>, Tuple2, A>, B>>> racePair( - Kind, ? extends A> fa, Kind, ? extends B> fb) { + default EIO, Throwable>, B>>, Tuple2, Throwable>, A>, B>>> racePair( + Kind, Throwable>, ? extends A> fa, Kind, Throwable>, ? extends B> fb) { return EIO.racePair(executor(), fa, fb); } @Override - default EIO, A>> fork(Kind, ? extends A> value) { + default EIO, Throwable>, A>> fork(Kind, Throwable>, ? extends A> value) { EIO fix = value.fix(EIOOf::narrowK); return fix.fork(); } } -interface EIORuntime extends Runtime> { +interface EIORuntime extends Runtime, E>> { @SuppressWarnings("rawtypes") EIORuntime INSTANCE = new EIORuntime() {}; @Override - default T run(Kind, T> value) { + default T run(Kind, E>, T> value) { return value.fix(toEIO()).safeRunSync().getRight(); } @Override - default Sequence run(Sequence, T>> values) { + default Sequence run(Sequence, E>, T>> values) { return run(EIO.traverse(values.map(EIOOf::narrowK))); } @Override - default Future parRun(Kind, T> value, Executor executor) { + default Future parRun(Kind, E>, T> value, Executor executor) { return value.fix(toEIO()).runAsync().map(Either::get); } @Override - default Future> parRun(Sequence, T>> values, Executor executor) { + default Future> parRun(Sequence, E>, T>> values, Executor executor) { return parRun(EIO.traverse(values.map(EIOOf::narrowK)), executor); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/EitherInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/EitherInstances.java index adfec599e..11cf86797 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/EitherInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/EitherInstances.java @@ -14,7 +14,6 @@ import com.github.tonivade.purefun.free.Trampoline; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.EitherOf; -import com.github.tonivade.purefun.type.Either_; import com.github.tonivade.purefun.type.Eval; import com.github.tonivade.purefun.type.EvalOf; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -29,7 +28,7 @@ @SuppressWarnings("unchecked") public interface EitherInstances { - static Eq, R>> eq(Eq leftEq, Eq rightEq) { + static Eq, L>, R>> eq(Eq leftEq, Eq rightEq) { return (a, b) -> { if (a instanceof Either.Left(var leftA) && b instanceof Either.Left(var leftB)) { return leftEq.eqv(leftA, leftB); @@ -41,62 +40,62 @@ static Eq, R>> eq(Eq leftEq, Eq rightEq) { }; } - static Functor> functor() { + static Functor, L>> functor() { return EitherFunctor.INSTANCE; } - static Bifunctor bifunctor() { + static Bifunctor> bifunctor() { return EitherBifunctor.INSTANCE; } - static Applicative> applicative() { + static Applicative, L>> applicative() { return EitherApplicative.INSTANCE; } - static Monad> monad() { + static Monad, L>> monad() { return EitherMonad.INSTANCE; } - static MonadError, L> monadError() { + static MonadError, L>, L> monadError() { return EitherMonadError.INSTANCE; } - static MonadThrow> monadThrow() { + static MonadThrow, Throwable>> monadThrow() { return EitherMonadThrow.INSTANCE; } - static Foldable> foldable() { + static Foldable, L>> foldable() { return EitherFoldable.INSTANCE; } - static Traverse> traverse() { + static Traverse, L>> traverse() { return EitherTraverse.INSTANCE; } } -interface EitherFunctor extends Functor> { +interface EitherFunctor extends Functor, L>> { @SuppressWarnings("rawtypes") EitherFunctor INSTANCE = new EitherFunctor() {}; @Override - default Either map(Kind, ? extends T> value, Function1 map) { + default Either map(Kind, L>, ? extends T> value, Function1 map) { return EitherOf.narrowK(value).map(map); } } -interface EitherBifunctor extends Bifunctor { +interface EitherBifunctor extends Bifunctor> { EitherBifunctor INSTANCE = new EitherBifunctor() {}; @Override - default Either bimap(Kind, ? extends B> value, + default Either bimap(Kind, A>, ? extends B> value, Function1 leftMap, Function1 rightMap) { return EitherOf.narrowK(value).bimap(leftMap, rightMap); } } -interface EitherPure extends Applicative> { +interface EitherPure extends Applicative, L>> { @Override default Either pure(T value) { @@ -110,30 +109,30 @@ interface EitherApplicative extends EitherPure { EitherApplicative INSTANCE = new EitherApplicative() {}; @Override - default Either ap(Kind, ? extends T> value, - Kind, ? extends Function1> apply) { + default Either ap(Kind, L>, ? extends T> value, + Kind, L>, ? extends Function1> apply) { return EitherOf.narrowK(value).flatMap(t -> EitherOf.narrowK(apply).map(f -> f.apply(t))); } } -interface EitherMonad extends EitherPure, Monad> { +interface EitherMonad extends EitherPure, Monad, L>> { @SuppressWarnings("rawtypes") EitherMonad INSTANCE = new EitherMonad() {}; @Override - default Either flatMap(Kind, ? extends T> value, - Function1, ? extends R>> map) { + default Either flatMap(Kind, L>, ? extends T> value, + Function1, L>, ? extends R>> map) { return EitherOf.narrowK(value).flatMap(map.andThen(EitherOf::narrowK)); } @Override - default Kind, R> tailRecM(T value, - Function1, Either>> map) { + default Kind, L>, R> tailRecM(T value, + Function1, L>, Either>> map) { return loop(value, map).run(); } - private Trampoline, R>> loop(T value, Function1, Either>> map) { + private Trampoline, L>, R>> loop(T value, Function1, L>, Either>> map) { return switch (map.andThen(EitherOf::narrowK).apply(value)) { case Either.Left>(var left) -> Trampoline.done(Either.left(left)); case Either.Right>(Either.Right(var right)) -> Trampoline.done(Either.right(right)); @@ -142,7 +141,7 @@ private Trampoline, R>> loop(T value, Function1 extends EitherMonad, MonadError, L> { +interface EitherMonadError extends EitherMonad, MonadError, L>, L> { @SuppressWarnings("rawtypes") EitherMonadError INSTANCE = new EitherMonadError() {}; @@ -153,29 +152,29 @@ default Either raiseError(L error) { } @Override - default Either handleErrorWith(Kind, A> value, - Function1, ? extends A>> handler) { + default Either handleErrorWith(Kind, L>, A> value, + Function1, L>, ? extends A>> handler) { return EitherOf.narrowK(value).fold(handler.andThen(EitherOf::narrowK), Either::right); } } -interface EitherMonadThrow extends EitherMonadError, MonadThrow> { +interface EitherMonadThrow extends EitherMonadError, MonadThrow, Throwable>> { EitherMonadThrow INSTANCE = new EitherMonadThrow() {}; } -interface EitherFoldable extends Foldable> { +interface EitherFoldable extends Foldable, L>> { @SuppressWarnings("rawtypes") EitherFoldable INSTANCE = new EitherFoldable() {}; @Override - default B foldLeft(Kind, ? extends A> value, B initial, Function2 mapper) { + default B foldLeft(Kind, L>, ? extends A> value, B initial, Function2 mapper) { return EitherOf.narrowK(value).fold(cons(initial), a -> mapper.apply(initial, a)); } @Override - default Eval foldRight(Kind, ? extends A> value, Eval initial, + default Eval foldRight(Kind, L>, ? extends A> value, Eval initial, Function2, ? extends Eval> mapper) { return EitherOf.narrowK(value).fold( cons(initial).andThen(EvalOf::narrowK), @@ -183,14 +182,14 @@ default Eval foldRight(Kind, ? extends A> value, Eval } } -interface EitherTraverse extends Traverse>, EitherFoldable { +interface EitherTraverse extends Traverse, L>>, EitherFoldable { @SuppressWarnings("rawtypes") EitherTraverse INSTANCE = new EitherTraverse() {}; @Override - default Kind, R>> traverse( - Applicative applicative, Kind, T> value, + default Kind, L>, R>> traverse( + Applicative applicative, Kind, L>, T> value, Function1> mapper) { return EitherOf.narrowK(value).fold( l -> applicative.pure(Either.left(l).kind()), diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/EitherKInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/EitherKInstances.java index a22da9254..9ad0c8a32 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/EitherKInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/EitherKInstances.java @@ -12,7 +12,6 @@ import com.github.tonivade.purefun.core.Pattern2; import com.github.tonivade.purefun.free.EitherK; import com.github.tonivade.purefun.free.EitherKOf; -import com.github.tonivade.purefun.free.EitherK_; import com.github.tonivade.purefun.typeclasses.Comonad; import com.github.tonivade.purefun.typeclasses.Contravariant; import com.github.tonivade.purefun.typeclasses.Functor; @@ -20,7 +19,7 @@ public interface EitherKInstances { - static Eq, G>, T>> eq( + static Eq, F>, G>, T>> eq( Eq> leftEq, Eq> rightEq) { return (a, b) -> Pattern2., EitherK, Boolean>build() .when((x, y) -> x.isLeft() && y.isLeft()) @@ -32,33 +31,33 @@ static Eq, G>, T>> eq( .apply(EitherKOf.narrowK(a), EitherKOf.narrowK(b)); } - static Functor, G>> functor( + static Functor, F>, G>> functor( Functor functorF, Functor functorG) { return EitherKFunctor.instance(checkNonNull(functorF), checkNonNull(functorG)); } - static Contravariant, G>> contravariant( + static Contravariant, F>, G>> contravariant( Contravariant contravariantF, Contravariant contravariantG) { return EitherKContravariant.instance(checkNonNull(contravariantF), checkNonNull(contravariantG)); } - static Comonad, G>> comonad( + static Comonad, F>, G>> comonad( Comonad comonadF, Comonad comonadG) { return EitherKComonad.instance(checkNonNull(comonadF), checkNonNull(comonadG)); } @SuppressWarnings("unchecked") - static InjectK, G>> injectEitherKLeft() { + static InjectK, F>, G>> injectEitherKLeft() { return EitherKInjectKLeft.INSTANCE; } static - InjectK, R>> injectEitherKRight(InjectK inject) { + InjectK, G>, R>> injectEitherKRight(InjectK inject) { return EitherKInjectKRight.instance(checkNonNull(inject)); } } -interface EitherKFunctor extends Functor, G>> { +interface EitherKFunctor extends Functor, F>, G>> { static EitherKFunctor instance(Functor functorF, Functor functorG) { return new EitherKFunctor<>() { @@ -74,13 +73,13 @@ static EitherKFunctor instance(Functor functorF, Functor func @Override default EitherK map( - Kind, G>, ? extends T> value, Function1 map) { + Kind, F>, G>, ? extends T> value, Function1 map) { return value.fix(EitherKOf::narrowK).map(f(), g(), map); } } interface EitherKContravariant - extends Contravariant, G>> { + extends Contravariant, F>, G>> { static EitherKContravariant instance( Contravariant contravariantF, Contravariant contravariantG) { @@ -97,13 +96,13 @@ static EitherKContravariant instance( @Override default EitherK contramap( - Kind, G>, ? extends A> value, Function1 map) { + Kind, F>, G>, ? extends A> value, Function1 map) { return value.fix(EitherKOf::narrowK).contramap(f(), g(), map); } } interface EitherKComonad - extends Comonad, G>>, EitherKFunctor { + extends Comonad, F>, G>>, EitherKFunctor { static EitherKComonad instance(Comonad comonadF, Comonad comonadG) { return new EitherKComonad<>() { @@ -121,19 +120,19 @@ static EitherKComonad instance(Comonad comonadF, Comonad como @Override default EitherK coflatMap( - Kind, G>, ? extends A> value, - Function1, G>, ? extends A>, ? extends B> map) { + Kind, F>, G>, ? extends A> value, + Function1, F>, G>, ? extends A>, ? extends B> map) { return value.fix(EitherKOf::narrowK).coflatMap(f(), g(), map); } @Override - default A extract(Kind, G>, ? extends A> value) { + default A extract(Kind, F>, G>, ? extends A> value) { return value.fix(EitherKOf::narrowK).extract(f(), g()); } } interface EitherKInjectKRight - extends InjectK, R>> { + extends InjectK, G>, R>> { static EitherKInjectKRight instance(InjectK injectK) { return () -> injectK; @@ -147,7 +146,7 @@ default EitherK inject(Kind value) { } } -interface EitherKInjectKLeft extends InjectK, G>> { +interface EitherKInjectKLeft extends InjectK, F>, G>> { @SuppressWarnings("rawtypes") EitherKInjectKLeft INSTANCE = new EitherKInjectKLeft() {}; diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/EitherTInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/EitherTInstances.java index e15e362df..2c1684801 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/EitherTInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/EitherTInstances.java @@ -20,7 +20,6 @@ import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.transformer.EitherT; import com.github.tonivade.purefun.transformer.EitherTOf; -import com.github.tonivade.purefun.transformer.EitherT_; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.typeclasses.Bracket; import com.github.tonivade.purefun.typeclasses.Defer; @@ -33,42 +32,42 @@ public interface EitherTInstances { - static Eq, L>, R>> eq(Eq>> eq) { + static Eq, F>, L>, R>> eq(Eq>> eq) { return (a, b) -> eq.eqv(EitherTOf.narrowK(a).value(), EitherTOf.narrowK(b).value()); } - static Monad, L>> monad(Monad monadF) { + static Monad, F>, L>> monad(Monad monadF) { return EitherTMonad.instance(checkNonNull(monadF)); } - static MonadError, L>, L> monadError(Monad monadF) { + static MonadError, F>, L>, L> monadError(Monad monadF) { return EitherTMonadErrorFromMonad.instance(checkNonNull(monadF)); } - static MonadError, L>, L> monadError(MonadError monadErrorF) { + static MonadError, F>, L>, L> monadError(MonadError monadErrorF) { return EitherTMonadErrorFromMonadError.instance(checkNonNull(monadErrorF)); } - static MonadThrow, Throwable>> monadThrow(Monad monadF) { + static MonadThrow, F>, Throwable>> monadThrow(Monad monadF) { return EitherTMonadThrowFromMonad.instance(checkNonNull(monadF)); } - static MonadThrow, Throwable>> monadThrow(MonadThrow monadF) { + static MonadThrow, F>, Throwable>> monadThrow(MonadThrow monadF) { return EitherTMonadThrowFromMonadThrow.instance(checkNonNull(monadF)); } - static MonadDefer, Throwable>> monadDefer(MonadDefer monadDeferF) { + static MonadDefer, F>, Throwable>> monadDefer(MonadDefer monadDeferF) { return EitherTMonadDefer.instance(checkNonNull(monadDeferF)); } static - Reference, Throwable>, A> + Reference, F>, Throwable>, A> refFromMonadThrow(MonadDefer monadDeferF, A value) { return Reference.of(monadDefer(monadDeferF), value); } } -interface EitherTMonad extends Monad, L>> { +interface EitherTMonad extends Monad, F>, L>> { static EitherTMonad instance(Monad monadF) { return () -> monadF; @@ -82,14 +81,14 @@ default EitherT pure(T value) { } @Override - default EitherT flatMap(Kind, L>, ? extends T> value, - Function1, L>, ? extends R>> map) { + default EitherT flatMap(Kind, F>, L>, ? extends T> value, + Function1, F>, L>, ? extends R>> map) { return EitherTOf.narrowK(value).flatMap(map.andThen(EitherTOf::narrowK)); } } interface EitherTMonadErrorFromMonad - extends MonadError, E>, E>, EitherTMonad { + extends MonadError, F>, E>, E>, EitherTMonad { static EitherTMonadErrorFromMonad instance(Monad monadF) { return () -> monadF; @@ -101,8 +100,8 @@ default EitherT raiseError(E error) { } @Override - default EitherT handleErrorWith(Kind, E>, A> value, - Function1, E>, ? extends A>> handler) { + default EitherT handleErrorWith(Kind, F>, E>, A> value, + Function1, F>, E>, ? extends A>> handler) { return EitherT.of(monadF(), monadF().flatMap(EitherTOf.narrowK(value).value(), either -> either.fold( @@ -112,7 +111,7 @@ default EitherT handleErrorWith(Kind, E>, A> } interface EitherTMonadErrorFromMonadError - extends MonadError, E>, E>, + extends MonadError, F>, E>, E>, EitherTMonad { static EitherTMonadErrorFromMonadError instance(MonadError monadErrorF) { @@ -128,8 +127,8 @@ default EitherT raiseError(E error) { } @Override - default EitherT handleErrorWith(Kind, E>, A> value, - Function1, E>, ? extends A>> handler) { + default EitherT handleErrorWith(Kind, F>, E>, A> value, + Function1, F>, E>, ? extends A>> handler) { return EitherT.of(monadF(), monadF().handleErrorWith(EitherTOf.narrowK(value).value(), error -> handler.andThen(EitherTOf::narrowK).apply(error).value())); @@ -138,7 +137,7 @@ default EitherT handleErrorWith(Kind, E>, A> interface EitherTMonadThrowFromMonad extends EitherTMonadErrorFromMonad, - MonadThrow, Throwable>> { + MonadThrow, F>, Throwable>> { static EitherTMonadThrowFromMonad instance(Monad monadF) { return () -> monadF; @@ -147,24 +146,24 @@ static EitherTMonadThrowFromMonad instance(Monad monadF) { interface EitherTMonadThrowFromMonadThrow extends EitherTMonadErrorFromMonadError, - MonadThrow, Throwable>> { + MonadThrow, F>, Throwable>> { static EitherTMonadThrowFromMonadThrow instance(MonadThrow monadThrowF) { return () -> monadThrowF; } } -interface EitherTDefer extends Defer, E>> { +interface EitherTDefer extends Defer, F>, E>> { MonadDefer monadF(); @Override - default EitherT defer(Producer, E>, ? extends A>> defer) { + default EitherT defer(Producer, F>, E>, ? extends A>> defer) { return EitherT.of(monadF(), monadF().defer(() -> defer.map(EitherTOf::narrowK).get().value())); } } -interface EitherTBracket extends Bracket, E>, E> { +interface EitherTBracket extends Bracket, F>, E>, E> { MonadDefer monadF(); @@ -172,9 +171,9 @@ interface EitherTBracket extends Bracket, E>, E> { @Override default EitherT - bracket(Kind, E>, ? extends A> acquire, - Function1, E>, ? extends B>> use, - Function1, E>, Unit>> release) { + bracket(Kind, F>, E>, ? extends A> acquire, + Function1, F>, E>, ? extends B>> use, + Function1, F>, E>, Unit>> release) { Kind> bracket = monadF().bracket( acquire.fix(EitherTOf::narrowK).value(), @@ -182,7 +181,7 @@ interface EitherTBracket extends Bracket, E>, E> { this::acquireRecover, value -> use.andThen(EitherTOf::narrowK).apply(value).value()), either -> { - Kind, E>, Unit> fold = either.fold(error -> EitherT.left(monadF(), error), release); + Kind, F>, E>, Unit> fold = either.fold(error -> EitherT.left(monadF(), error), release); Kind> value = fold.fix(toEitherT()).value(); return monadF().map(value, x -> x.fold(cons(unit()), identity())); }); @@ -190,7 +189,7 @@ interface EitherTBracket extends Bracket, E>, E> { } } -interface EitherTTimer extends Timer, Throwable>> { +interface EitherTTimer extends Timer, F>, Throwable>> { MonadDefer monadF(); @@ -205,7 +204,7 @@ interface EitherTMonadDefer EitherTDefer, EitherTBracket, EitherTTimer, - MonadDefer, Throwable>> { + MonadDefer, F>, Throwable>> { static EitherTMonadDefer instance(MonadDefer monadDeferF) { return () -> monadDeferF; diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/EvalInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/EvalInstances.java index 8af5af966..6d1f350e5 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/EvalInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/EvalInstances.java @@ -9,7 +9,6 @@ import com.github.tonivade.purefun.core.Producer; import com.github.tonivade.purefun.type.Eval; import com.github.tonivade.purefun.type.EvalOf; -import com.github.tonivade.purefun.type.Eval_; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Comonad; @@ -21,49 +20,49 @@ public interface EvalInstances { - static Functor functor() { + static Functor> functor() { return EvalFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return EvalApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return EvalMonad.INSTANCE; } - static MonadError monadError() { + static MonadError, Throwable> monadError() { return EvalMonadError.INSTANCE; } - static MonadThrow monadThrow() { + static MonadThrow> monadThrow() { return EvalMonadThrow.INSTANCE; } - static Comonad comonad() { + static Comonad> comonad() { return EvalComonad.INSTANCE; } - static Defer defer() { + static Defer> defer() { return EvalDefer.INSTANCE; } } -interface EvalFunctor extends Functor { +interface EvalFunctor extends Functor> { EvalFunctor INSTANCE = new EvalFunctor() {}; @Override - default Eval map(Kind value, Function1 mapper) { + default Eval map(Kind, ? extends T> value, Function1 mapper) { return EvalOf.narrowK(value).map(mapper); } } -interface EvalPure extends Applicative { +interface EvalPure extends Applicative> { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return Eval.now(value); } } @@ -73,65 +72,65 @@ interface EvalApplicative extends EvalPure { EvalApplicative INSTANCE = new EvalApplicative() {}; @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return EvalOf.narrowK(value).flatMap(t -> EvalOf.narrowK(apply).map(f -> f.apply(t))); } } -interface EvalMonad extends EvalPure, Monad { +interface EvalMonad extends EvalPure, Monad> { EvalMonad INSTANCE = new EvalMonad() {}; @Override - default Kind flatMap(Kind value, Function1> map) { + default Kind, R> flatMap(Kind, ? extends T> value, Function1, ? extends R>> map) { return EvalOf.narrowK(value).flatMap(map.andThen(EvalOf::narrowK)); } } -interface EvalMonadError extends EvalMonad, MonadError { +interface EvalMonadError extends EvalMonad, MonadError, Throwable> { EvalMonadError INSTANCE = new EvalMonadError() {}; @Override - default Kind raiseError(Throwable error) { + default Kind, A> raiseError(Throwable error) { return Eval.raiseError(error); } @Override - default Kind handleErrorWith( - Kind value, Function1> handler) { + default Kind, A> handleErrorWith( + Kind, A> value, Function1, ? extends A>> handler) { Eval> attempt = Eval.always(() -> Try.of(value.fix(EvalOf::narrowK)::value)); return attempt.flatMap(try_ -> try_.fold(handler.andThen(EvalOf::narrowK), Eval::now)); } } -interface EvalMonadThrow extends EvalMonadError, MonadThrow { +interface EvalMonadThrow extends EvalMonadError, MonadThrow> { EvalMonadThrow INSTANCE = new EvalMonadThrow() {}; } -interface EvalComonad extends EvalFunctor, Comonad { +interface EvalComonad extends EvalFunctor, Comonad> { EvalComonad INSTANCE = new EvalComonad() {}; @Override - default Kind coflatMap(Kind value, Function1, ? extends B> map) { + default Kind, B> coflatMap(Kind, ? extends A> value, Function1, ? extends A>, ? extends B> map) { return Eval.later(() -> map.apply(value)); } @Override - default A extract(Kind value) { + default A extract(Kind, ? extends A> value) { return EvalOf.narrowK(value).value(); } } -interface EvalDefer extends Defer { +interface EvalDefer extends Defer> { EvalDefer INSTANCE = new EvalDefer() {}; @Override - default Kind defer(Producer> defer) { + default Kind, A> defer(Producer, ? extends A>> defer) { return Eval.defer(defer.map(EvalOf::narrowK)); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/Function1Instances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/Function1Instances.java index 93c7713f7..ab09e38e9 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/Function1Instances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/Function1Instances.java @@ -9,7 +9,6 @@ import static com.github.tonivade.purefun.typeclasses.Conested.counnest; import com.github.tonivade.purefun.core.Function1Of; -import com.github.tonivade.purefun.core.Function1_; import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -22,41 +21,41 @@ @SuppressWarnings("unchecked") public interface Function1Instances { - static Functor> functor() { + static Functor, T>> functor() { return Function1Functor.INSTANCE; } - static Applicative> applicative() { + static Applicative, T>> applicative() { return Function1Applicative.INSTANCE; } - static Monad> monad() { + static Monad, T>> monad() { return Function1Monad.INSTANCE; } - static Contravariant> contravariant() { + static Contravariant, T>> contravariant() { return Function1Contravariant.INSTANCE; } - static Profunctor profunctor() { + static Profunctor> profunctor() { return Function1Profunctor.INSTANCE; } } -interface Function1Functor extends Functor> { +interface Function1Functor extends Functor, T>> { @SuppressWarnings("rawtypes") Function1Functor INSTANCE = new Function1Functor() {}; @Override - default Function1 map(Kind, ? extends A> value, + default Function1 map(Kind, T>, ? extends A> value, Function1 map) { Function1 function = value.fix(Function1Of::narrowK); return function.andThen(map); } } -interface Function1Pure extends Applicative> { +interface Function1Pure extends Applicative, T>> { @Override default Function1 pure(A value) { @@ -70,45 +69,45 @@ interface Function1Applicative extends Function1Pure { Function1Applicative INSTANCE = new Function1Applicative() {}; @Override - default Function1 ap(Kind, ? extends A> value, - Kind, ? extends Function1> apply) { + default Function1 ap(Kind, T>, ? extends A> value, + Kind, T>, ? extends Function1> apply) { return value.fix(toFunction1()) .flatMap(a -> apply.fix(Function1Of::narrowK).andThen(f -> f.apply(a))); } } -interface Function1Monad extends Function1Pure, Monad> { +interface Function1Monad extends Function1Pure, Monad, T>> { @SuppressWarnings("rawtypes") Function1Monad INSTANCE = new Function1Monad() {}; @Override - default Function1 flatMap(Kind, ? extends A> value, - Function1, ? extends R>> map) { + default Function1 flatMap(Kind, T>, ? extends A> value, + Function1, T>, ? extends R>> map) { Function1 function = value.fix(Function1Of::narrowK); return function.flatMap(map.andThen(Function1Of::narrowK)); } } -interface Function1Contravariant extends Contravariant> { +interface Function1Contravariant extends Contravariant, R>> { @SuppressWarnings("rawtypes") Function1Contravariant INSTANCE = new Function1Contravariant() {}; @Override - default Kind, B> contramap(Kind, ? extends A> value, Function1 map) { - Kind, R> counnest = counnest(value); + default Kind, R>, B> contramap(Kind, R>, ? extends A> value, Function1 map) { + Kind, A>, R> counnest = counnest(value); Function1 function = counnest.fix(Function1Of::narrowK); return conest(function.compose(map)); } } -interface Function1Profunctor extends Profunctor { +interface Function1Profunctor extends Profunctor> { Function1Profunctor INSTANCE = new Function1Profunctor() {}; @Override - default Function1 dimap(Kind, ? extends B> value, Function1 contramap, Function1 map) { + default Function1 dimap(Kind, A>, ? extends B> value, Function1 contramap, Function1 map) { return value.fix(Function1Of::narrowK).dimap(contramap, map); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/FutureInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/FutureInstances.java index 17a07ac3b..0b0feb7a3 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/FutureInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/FutureInstances.java @@ -13,7 +13,6 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.concurrent.Future; import com.github.tonivade.purefun.concurrent.FutureOf; -import com.github.tonivade.purefun.concurrent.Future_; import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Functor; @@ -23,41 +22,41 @@ public interface FutureInstances { - static Functor functor() { + static Functor> functor() { return FutureFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return applicative(Future.DEFAULT_EXECUTOR); } - static Applicative applicative(Executor executor) { + static Applicative> applicative(Executor executor) { return FutureApplicative.instance(checkNonNull(executor)); } - static Monad monad() { + static Monad> monad() { return monad(Future.DEFAULT_EXECUTOR); } - static Monad monad(Executor executor) { + static Monad> monad(Executor executor) { return FutureMonad.instance(checkNonNull(executor)); } - static MonadError monadError() { + static MonadError, Throwable> monadError() { return monadError(Future.DEFAULT_EXECUTOR); } - static MonadError monadError(Executor executor) { + static MonadError, Throwable> monadError(Executor executor) { return FutureMonadThrow.instance(checkNonNull(executor)); } } -interface FutureFunctor extends Functor { +interface FutureFunctor extends Functor> { FutureFunctor INSTANCE = new FutureFunctor() {}; @Override - default Kind map(Kind value, Function1 mapper) { + default Kind, R> map(Kind, ? extends T> value, Function1 mapper) { return value.fix(toFuture()).map(mapper); } } @@ -66,10 +65,10 @@ interface ExecutorHolder { Executor executor(); } -interface FuturePure extends Applicative, ExecutorHolder { +interface FuturePure extends Applicative>, ExecutorHolder { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return Future.success(executor(), value); } } @@ -81,21 +80,21 @@ static FutureApplicative instance(Executor executor) { } @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return value.fix(FutureOf::narrowK).ap(apply.fix(FutureOf::narrowK)); } } -interface FutureMonad extends FuturePure, Monad { +interface FutureMonad extends FuturePure, Monad> { static FutureMonad instance(Executor executor) { return () -> executor; } @Override - default Kind flatMap(Kind value, - Function1> map) { + default Kind, R> flatMap(Kind, ? extends T> value, + Function1, ? extends R>> map) { return value.fix(toFuture()).flatMap(map.andThen(FutureOf::narrowK)); } @@ -104,27 +103,27 @@ default Kind flatMap(Kind value, * applicative version of the ap method */ @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return FutureInstances.applicative(executor()).ap(value, apply); } } -interface FutureMonadThrow extends FutureMonad, MonadThrow { +interface FutureMonadThrow extends FutureMonad, MonadThrow> { static FutureMonadThrow instance(Executor executor) { return () -> executor; } @Override - default Kind raiseError(Throwable error) { + default Kind, A> raiseError(Throwable error) { return Future.failure(executor(), error); } @Override - default Kind handleErrorWith( - Kind value, - Function1> handler) { + default Kind, A> handleErrorWith( + Kind, A> value, + Function1, ? extends A>> handler) { return value.fix(toFuture()).fold(handler.andThen(FutureOf::narrowK), success -> Future.success(executor(), success)).flatMap(identity()); } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/IOInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/IOInstances.java index 8ea47aba1..23c835481 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/IOInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/IOInstances.java @@ -18,7 +18,6 @@ import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.monad.IO; import com.github.tonivade.purefun.monad.IOOf; -import com.github.tonivade.purefun.monad.IO_; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -38,70 +37,70 @@ public interface IOInstances { - static Functor functor() { + static Functor> functor() { return IOFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return applicative(Future.DEFAULT_EXECUTOR); } - static Applicative applicative(Executor executor) { + static Applicative> applicative(Executor executor) { return IOApplicative.instance(executor); } - static Monad monad() { + static Monad> monad() { return IOMonad.INSTANCE; } - static MonadError monadError() { + static MonadError, Throwable> monadError() { return IOMonadError.INSTANCE; } - static MonadThrow monadThrow() { + static MonadThrow> monadThrow() { return IOMonadThrow.INSTANCE; } - static Timer timer() { + static Timer> timer() { return IOMonadDefer.INSTANCE; } - static MonadDefer monadDefer() { + static MonadDefer> monadDefer() { return IOMonadDefer.INSTANCE; } - static Async async() { + static Async> async() { return IOAsync.INSTANCE; } - static Concurrent concurrent() { + static Concurrent> concurrent() { return concurrent(Future.DEFAULT_EXECUTOR); } - static Concurrent concurrent(Executor executor) { + static Concurrent> concurrent(Executor executor) { return IOConcurrent.instance(executor); } - static Console console() { + static Console> console() { return IOConsole.INSTANCE; } - - static Runtime runtime() { + + static Runtime> runtime() { return IORuntime.INSTANCE; } } -interface IOFunctor extends Functor { +interface IOFunctor extends Functor> { IOFunctor INSTANCE = new IOFunctor() {}; @Override - default Kind map(Kind value, Function1 map) { + default Kind, R> map(Kind, ? extends T> value, Function1 map) { return value.fix(toIO()).map(map); } } -interface IOPure extends Applicative { +interface IOPure extends Applicative> { @Override default IO pure(T value) { @@ -109,8 +108,8 @@ default IO pure(T value) { } } -interface IOApplicative extends IOPure, Applicative { - +interface IOApplicative extends IOPure, Applicative> { + static IOApplicative instance(Executor executor) { return () -> executor; } @@ -118,25 +117,25 @@ static IOApplicative instance(Executor executor) { Executor executor(); @Override - default IO ap(Kind value, - Kind> apply) { + default IO ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return IO.parMap2(executor(), value.fix(toIO()), apply.fix(toIO()), (v, a) -> a.apply(v)); } } -interface IOMonad extends Monad, IOPure { +interface IOMonad extends Monad>, IOPure { IOMonad INSTANCE = new IOMonad() {}; @Override default IO flatMap( - Kind value, - Function1> map) { + Kind, ? extends T> value, + Function1, ? extends R>> map) { return value.fix(toIO()).flatMap(map.andThen(IOOf::narrowK)); } } -interface IOMonadError extends MonadError, IOMonad { +interface IOMonadError extends MonadError, Throwable>, IOMonad { IOMonadError INSTANCE = new IOMonadError() {}; @@ -147,37 +146,37 @@ default IO raiseError(Throwable error) { @Override default IO handleErrorWith( - Kind value, - Function1> handler) { + Kind, A> value, + Function1, ? extends A>> handler) { return IOOf.narrowK(value).redeemWith(handler.andThen(IOOf::narrowK), IO::pure); } } -interface IOMonadThrow extends MonadThrow, IOMonadError { +interface IOMonadThrow extends MonadThrow>, IOMonadError { IOMonadThrow INSTANCE = new IOMonadThrow() {}; } -interface IODefer extends Defer { +interface IODefer extends Defer> { @Override - default IO defer(Producer> defer) { + default IO defer(Producer, ? extends A>> defer) { return IO.suspend(defer.map(IOOf::narrowK)); } } -interface IOBracket extends IOMonadError, Bracket { +interface IOBracket extends IOMonadError, Bracket, Throwable> { @Override default IO bracket( - Kind acquire, - Function1> use, - Function1> release) { + Kind, ? extends A> acquire, + Function1, ? extends B>> use, + Function1, Unit>> release) { return IO.bracket(acquire, use, release); } } -interface IOMonadDefer extends MonadDefer, IODefer, IOBracket { +interface IOMonadDefer extends MonadDefer>, IODefer, IOBracket { IOMonadDefer INSTANCE = new IOMonadDefer() {}; @@ -187,37 +186,37 @@ default IO sleep(Duration duration) { } } -interface IOAsync extends Async, IOMonadDefer { +interface IOAsync extends Async>, IOMonadDefer { IOAsync INSTANCE = new IOAsync() {}; - + @Override - default IO asyncF(Function1>, Kind> consumer) { + default IO asyncF(Function1>, Kind, Unit>> consumer) { return IO.cancellable(consumer.andThen(IOOf::narrowK)); } } -interface IOConcurrent extends Concurrent, IOAsync { - +interface IOConcurrent extends Concurrent>, IOAsync { + static IOConcurrent instance(Executor executor) { return () -> executor; } - + Executor executor(); - + @Override - default IO>, Tuple2, B>>> racePair(Kind fa, Kind fb) { + default IO, B>>, Tuple2, A>, B>>> racePair(Kind, ? extends A> fa, Kind, ? extends B> fb) { return IO.racePair(executor(), fa, fb); } - + @Override - default IO> fork(Kind value) { + default IO, A>> fork(Kind, ? extends A> value) { IO fix = value.fix(IOOf::narrowK); return fix.fork(); } } -final class IOConsole implements Console { +final class IOConsole implements Console> { public static final IOConsole INSTANCE = new IOConsole(); @@ -234,27 +233,27 @@ public IO println(String text) { } } -interface IORuntime extends Runtime { - +interface IORuntime extends Runtime> { + IORuntime INSTANCE = new IORuntime() {}; @Override - default T run(Kind value) { + default T run(Kind, T> value) { return value.fix(toIO()).unsafeRunSync(); } - + @Override - default Sequence run(Sequence> values) { + default Sequence run(Sequence, T>> values) { return run(IO.traverse(values.map(IOOf::narrowK))); } @Override - default Future parRun(Kind value, Executor executor) { + default Future parRun(Kind, T> value, Executor executor) { return value.fix(toIO()).runAsync(executor); } - + @Override - default Future> parRun(Sequence> values, Executor executor) { + default Future> parRun(Sequence, T>> values, Executor executor) { return parRun(IO.traverse(values.map(IOOf::narrowK)), executor); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/IdInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/IdInstances.java index 2d716a447..c2affd711 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/IdInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/IdInstances.java @@ -15,7 +15,6 @@ import com.github.tonivade.purefun.type.EvalOf; import com.github.tonivade.purefun.type.Id; import com.github.tonivade.purefun.type.IdOf; -import com.github.tonivade.purefun.type.Id_; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Comonad; import com.github.tonivade.purefun.typeclasses.Foldable; @@ -25,49 +24,49 @@ public interface IdInstances { - static Eq> eq(Eq idEq) { + static Eq, T>> eq(Eq idEq) { return (a, b) -> idEq.eqv(IdOf.narrowK(a).value(), IdOf.narrowK(b).value()); } - static Functor functor() { + static Functor> functor() { return IdFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return IdApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return IdMonad.INSTANCE; } - static Comonad comonad() { + static Comonad> comonad() { return IdComonad.INSTANCE; } - static Foldable foldable() { + static Foldable> foldable() { return IdFoldable.INSTANCE; } - static Traverse traverse() { + static Traverse> traverse() { return IdTraverse.INSTANCE; } } -interface IdFunctor extends Functor { +interface IdFunctor extends Functor> { IdFunctor INSTANCE = new IdFunctor() {}; @Override - default Kind map(Kind value, Function1 map) { + default Kind, R> map(Kind, ? extends T> value, Function1 map) { return IdOf.narrowK(value).map(map); } } -interface IdPure extends Applicative { +interface IdPure extends Applicative> { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return Id.of(value); } } @@ -77,59 +76,59 @@ interface IdApplicative extends IdPure { IdApplicative INSTANCE = new IdApplicative() {}; @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return IdOf.narrowK(value).flatMap(t -> IdOf.narrowK(apply).map(f -> f.apply(t))); } } -interface IdMonad extends IdPure, Monad { +interface IdMonad extends IdPure, Monad> { IdMonad INSTANCE = new IdMonad() {}; @Override - default Kind flatMap(Kind value, Function1> map) { + default Kind, R> flatMap(Kind, ? extends T> value, Function1, ? extends R>> map) { return IdOf.narrowK(value).flatMap(map.andThen(IdOf::narrowK)); } } -interface IdComonad extends IdFunctor, Comonad { +interface IdComonad extends IdFunctor, Comonad> { IdComonad INSTANCE = new IdComonad() {}; @Override - default Kind coflatMap(Kind value, Function1, ? extends B> map) { + default Kind, B> coflatMap(Kind, ? extends A> value, Function1, ? extends A>, ? extends B> map) { return Id.of(map.apply(value)); } @Override - default A extract(Kind value) { + default A extract(Kind, ? extends A> value) { return IdOf.narrowK(value).value(); } } -interface IdFoldable extends Foldable { +interface IdFoldable extends Foldable> { IdFoldable INSTANCE = new IdFoldable() {}; @Override - default B foldLeft(Kind value, B initial, Function2 mapper) { + default B foldLeft(Kind, ? extends A> value, B initial, Function2 mapper) { return mapper.apply(initial, value.fix(toId()).value()); } @Override - default Eval foldRight(Kind value, Eval initial, Function2, ? extends Eval> mapper) { + default Eval foldRight(Kind, ? extends A> value, Eval initial, Function2, ? extends Eval> mapper) { return EvalOf.narrowK(mapper.apply(value.fix(toId()).value(), initial)); } } -interface IdTraverse extends Traverse, IdFoldable { +interface IdTraverse extends Traverse>, IdFoldable { IdTraverse INSTANCE = new IdTraverse() {}; @Override - default Kind> traverse( - Applicative applicative, Kind value, + default Kind, R>> traverse( + Applicative applicative, Kind, T> value, Function1> mapper) { Kind apply = mapper.apply(value.fix(toId()).value()); return applicative.map(apply, Id::of); diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/KleisliInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/KleisliInstances.java index 429b31ee2..a11c678fd 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/KleisliInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/KleisliInstances.java @@ -13,7 +13,6 @@ import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.transformer.Kleisli; import com.github.tonivade.purefun.transformer.KleisliOf; -import com.github.tonivade.purefun.transformer.Kleisli_; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.MonadError; import com.github.tonivade.purefun.typeclasses.MonadReader; @@ -21,24 +20,24 @@ public interface KleisliInstances { - static Monad, Z>> monad(Monad monadF) { + static Monad, F>, Z>> monad(Monad monadF) { return KleisliMonad.instance(checkNonNull(monadF)); } - static MonadError, Z>, E> monadError(MonadError monadErrorF) { + static MonadError, F>, Z>, E> monadError(MonadError monadErrorF) { return KleisliMonadError.instance(monadErrorF); } - static MonadReader, Z>, Z> monadReader(Monad monadF) { + static MonadReader, F>, Z>, Z> monadReader(Monad monadF) { return KleisliMonadReader.instance(monadF); } - static MonadState, Z>, S> monadState(MonadState monadStateF) { + static MonadState, F>, Z>, S> monadState(MonadState monadStateF) { return KleisliMonadState.instance(monadStateF); } } -interface KleisliMonad extends Monad, Z>> { +interface KleisliMonad extends Monad, F>, Z>> { static KleisliMonad instance(Monad monadF) { return () -> monadF; @@ -52,13 +51,13 @@ default Kleisli pure(T value) { } @Override - default Kleisli flatMap(Kind, Z>, ? extends T> value, - Function1, Z>, ? extends R>> map) { + default Kleisli flatMap(Kind, F>, Z>, ? extends T> value, + Function1, F>, Z>, ? extends R>> map) { return value.fix(toKleisli()).flatMap(map.andThen(KleisliOf::narrowK)); } } -interface KleisliMonadError extends MonadError, R>, E>, KleisliMonad { +interface KleisliMonadError extends MonadError, F>, R>, E>, KleisliMonad { static KleisliMonadError instance(MonadError monadErrorF) { return () -> monadErrorF; @@ -74,8 +73,8 @@ default Kleisli raiseError(E error) { @Override default Kleisli handleErrorWith( - Kind, R>, A> value, - Function1, R>, ? extends A>> handler) { + Kind, F>, R>, A> value, + Function1, F>, R>, ? extends A>> handler) { Kleisli kleisli = value.fix(KleisliOf::narrowK); return Kleisli.of(monadF(), reader -> monadF().handleErrorWith(kleisli.run(reader), @@ -83,7 +82,7 @@ default Kleisli handleErrorWith( } } -interface KleisliMonadReader extends MonadReader, R>, R>, KleisliMonad { +interface KleisliMonadReader extends MonadReader, F>, R>, R>, KleisliMonad { static KleisliMonadReader instance(Monad monadF) { return () -> monadF; @@ -95,7 +94,7 @@ default Kleisli ask() { } } -interface KleisliMonadState extends MonadState, R>, S>, KleisliMonad { +interface KleisliMonadState extends MonadState, F>, R>, S>, KleisliMonad { static KleisliMonadState instance(MonadState monadStateF) { return () -> monadStateF; diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/MonadMTL.java b/instances/src/main/java/com/github/tonivade/purefun/instances/MonadMTL.java index 4037b9dd5..7f3bb8c94 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/MonadMTL.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/MonadMTL.java @@ -16,16 +16,11 @@ import com.github.tonivade.purefun.core.Tuple2; import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.instances.MonadMTL.EffectE; -import com.github.tonivade.purefun.instances.MonadMTL.EffectE_; import com.github.tonivade.purefun.instances.MonadMTL.EffectR; -import com.github.tonivade.purefun.instances.MonadMTL.EffectR_; -import com.github.tonivade.purefun.instances.MonadMTL.EffectS_; +import com.github.tonivade.purefun.instances.MonadMTL.EffectS; import com.github.tonivade.purefun.transformer.EitherT; -import com.github.tonivade.purefun.transformer.EitherT_; import com.github.tonivade.purefun.transformer.Kleisli; -import com.github.tonivade.purefun.transformer.Kleisli_; import com.github.tonivade.purefun.transformer.StateT; -import com.github.tonivade.purefun.transformer.StateT_; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.MonadError; @@ -33,14 +28,17 @@ import com.github.tonivade.purefun.typeclasses.MonadState; public class MonadMTL - implements Monad, MonadError, MonadState, MonadReader { + implements Monad>, + MonadError, E>, + MonadState, S>, + MonadReader, R> { private final Monad monad; - private final MonadError monadErrorE; - private final Monad monadR; - private final MonadError, S>, E> monadErrorS; - private final MonadReader, S>, R> monadReaderS; - private final MonadState, S>, S> monadStateS; + private final MonadError, E> monadErrorE; + private final Monad> monadR; + private final MonadError, EffectR>, S>, E> monadErrorS; + private final MonadReader, EffectR>, S>, R> monadReaderS; + private final MonadState, EffectR>, S>, S> monadStateS; public MonadMTL(Monad monad) { this.monad = checkNonNull(monad); @@ -57,8 +55,8 @@ public EffectS pure(A value) { } @Override - public EffectS flatMap(Kind value, - Function1> map) { + public EffectS flatMap(Kind, ? extends A> value, + Function1, ? extends B>> map) { return new EffectS<>(monadStateS.flatMap( value.fix(EffectS::narrowK).value(), x -> map.apply(x).fix(EffectS::narrowK).value())); @@ -81,7 +79,7 @@ public EffectS raiseError(E error) { @Override public EffectS handleErrorWith( - Kind value, Function1> handler) { + Kind, A> value, Function1, ? extends A>> handler) { return new EffectS<>(monadErrorS.handleErrorWith( value.fix(EffectS::narrowK).value(), error -> handler.apply(error).fix(EffectS::narrowK).value())); @@ -108,15 +106,11 @@ public EffectS effect(Kind> value) { return effectS(effectR(effectE(value))); } - public static final class EffectE_ { } - public static final class EffectR_ { } - public static final class EffectS_ { } - - public static final class EffectE implements Kind { + public static final class EffectE implements Kind, A> { private final EitherT value; - public EffectE(Kind, E>, A> value) { + public EffectE(Kind, F>, E>, A> value) { this.value = value.fix(toEitherT()); } @@ -129,20 +123,20 @@ public Kind> run() { } @SuppressWarnings("unchecked") - public static EffectE narrowK(Kind hkt) { + public static EffectE narrowK(Kind, ? extends A> hkt) { return (EffectE) hkt; } } - public static final class EffectR implements Kind { + public static final class EffectR implements Kind, A> { - private final Kleisli value; + private final Kleisli, R, A> value; - public EffectR(Kind, R>, A> value) { + public EffectR(Kind, EffectE>, R>, A> value) { this.value = value.fix(toKleisli()); } - public Kleisli value() { + public Kleisli, R, A> value() { return value; } @@ -151,20 +145,20 @@ public EffectE run(R config) { } @SuppressWarnings("unchecked") - public static EffectR narrowK(Kind hkt) { + public static EffectR narrowK(Kind, ? extends A> hkt) { return (EffectR) hkt; } } - public static final class EffectS implements Kind { + public static final class EffectS implements Kind, A> { - private final StateT value; + private final StateT, S, A> value; - public EffectS(Kind, S>, A> value) { + public EffectS(Kind, EffectR>, S>, A> value) { this.value = value.fix(toStateT()); } - public StateT value() { + public StateT, S, A> value() { return value; } @@ -173,15 +167,15 @@ public EffectR> run(S state) { } @SuppressWarnings("unchecked") - public static EffectS narrowK(Kind hkt) { + public static EffectS narrowK(Kind, ? extends A> hkt) { return (EffectS) hkt; } } } -class EffectEMonadError implements MonadError { +class EffectEMonadError implements MonadError, E> { - private final MonadError, E>, E> monad; + private final MonadError, F>, E>, E> monad; public EffectEMonadError(Monad monad) { this.monad = EitherTInstances.monadError(monad); @@ -193,8 +187,8 @@ public EffectE pure(A value) { } @Override - public EffectE flatMap(Kind value, - Function1> map) { + public EffectE flatMap(Kind, ? extends A> value, + Function1, ? extends B>> map) { return new EffectE<>(monad.flatMap( value.fix(EffectE::narrowK).value(), x -> map.apply(x).fix(EffectE::narrowK).value())); @@ -206,16 +200,16 @@ public EffectE raiseError(E error) { } @Override - public EffectE handleErrorWith(Kind value, - Function1> handler) { + public EffectE handleErrorWith(Kind, A> value, + Function1, ? extends A>> handler) { return new EffectE<>(monad.handleErrorWith(value.fix(EffectE::narrowK).value(), error -> handler.apply(error).fix(EffectE::narrowK).value())); } } -class EffectRMonad implements Monad { +class EffectRMonad implements Monad> { - private final Monad, R>> monad; + private final Monad, EffectE>, R>> monad; public EffectRMonad(Monad monad) { this.monad = KleisliInstances.monad(new EffectEMonadError(monad)); @@ -228,16 +222,16 @@ public EffectR pure(A value) { } @Override - public EffectR flatMap(Kind value, - Function1> map) { + public EffectR flatMap(Kind, ? extends A> value, + Function1, ? extends B>> map) { return new EffectR<>(monad.flatMap(value.fix(EffectR::narrowK).value(), t -> map.apply(t).fix(EffectR::narrowK).value())); } } -class EffectRMonadReader extends EffectRMonad implements MonadReader { +class EffectRMonadReader extends EffectRMonad implements MonadReader, R> { - private final MonadReader, R>, R> monad; + private final MonadReader, EffectE>, R>, R> monad; public EffectRMonadReader(Monad monad) { super(monad); @@ -250,9 +244,9 @@ public EffectR ask() { } } -class EffectRMonadError extends EffectRMonad implements MonadError { +class EffectRMonadError extends EffectRMonad implements MonadError, E> { - private final MonadError, R>, E> monadError; + private final MonadError, EffectE>, R>, E> monadError; public EffectRMonadError(Monad monad) { super(monad); @@ -266,7 +260,7 @@ public EffectR raiseError(E error) { @Override public EffectR handleErrorWith( - Kind value, Function1> handler) { + Kind, A> value, Function1, ? extends A>> handler) { return new EffectR<>(monadError.handleErrorWith( value.fix(EffectR::narrowK).value(), error -> handler.apply(error).fix(EffectR::narrowK).value())); diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/OptionInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/OptionInstances.java index a3ede8eef..92173d91a 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/OptionInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/OptionInstances.java @@ -22,7 +22,6 @@ import com.github.tonivade.purefun.type.EvalOf; import com.github.tonivade.purefun.type.Option; import com.github.tonivade.purefun.type.OptionOf; -import com.github.tonivade.purefun.type.Option_; import com.github.tonivade.purefun.typeclasses.Alternative; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Foldable; @@ -36,7 +35,7 @@ public interface OptionInstances { - static Eq> eq(Eq eqSome) { + static Eq, T>> eq(Eq eqSome) { return (a, b) -> { if (a instanceof Option.Some(var valueA) && b instanceof Option.Some(var valueB)) { return eqSome.eqv(valueA, valueB); @@ -45,53 +44,53 @@ static Eq> eq(Eq eqSome) { }; } - static Functor functor() { + static Functor> functor() { return OptionFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return OptionApplicative.INSTANCE; } - static Alternative alternative() { + static Alternative> alternative() { return OptionAlternative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return OptionMonad.INSTANCE; } - static MonadError monadError() { + static MonadError, Unit> monadError() { return OptionMonadError.INSTANCE; } - static Traverse traverse() { + static Traverse> traverse() { return OptionTraverse.INSTANCE; } - static Semigroupal semigroupal() { + static Semigroupal> semigroupal() { return OptionSemigroupal.INSTANCE; } - static Foldable foldable() { + static Foldable> foldable() { return OptionFoldable.INSTANCE; } } -interface OptionFunctor extends Functor { +interface OptionFunctor extends Functor> { OptionFunctor INSTANCE = new OptionFunctor() {}; @Override - default Kind map(Kind value, Function1 mapper) { + default Kind, R> map(Kind, ? extends T> value, Function1 mapper) { return OptionOf.narrowK(value).map(mapper); } } -interface OptionPure extends Applicative { +interface OptionPure extends Applicative> { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return Option.some(value); } } @@ -101,28 +100,28 @@ interface OptionApplicative extends OptionPure { OptionApplicative INSTANCE = new OptionApplicative() {}; @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return value.fix(toOption()).flatMap(t -> OptionOf.narrowK(apply).map(f -> f.apply(t))); } } -interface OptionMonad extends OptionPure, Monad { +interface OptionMonad extends OptionPure, Monad> { OptionMonad INSTANCE = new OptionMonad() {}; @Override - default Kind flatMap(Kind value, - Function1> map) { + default Kind, R> flatMap(Kind, ? extends T> value, + Function1, ? extends R>> map) { return value.fix(toOption()).flatMap(map.andThen(OptionOf::narrowK)); } @Override - default Kind tailRecM(T value, Function1>> map) { + default Kind, R> tailRecM(T value, Function1, Either>> map) { return loop(value, map).run(); } - private Trampoline> loop(T value, Function1>> map) { + private Trampoline, R>> loop(T value, Function1, Either>> map) { return switch (map.andThen(OptionOf::narrowK).apply(value)) { case Option.None> n -> Trampoline.done(Option.none()); case Option.Some>(Either.Right(var right)) -> Trampoline.done(Option.some(right)); @@ -131,71 +130,71 @@ private Trampoline> loop(T value, Function1 { +interface OptionSemigroupK extends SemigroupK> { OptionSemigroupK INSTANCE = new OptionSemigroupK() {}; @Override - default Kind combineK(Kind t1, Kind t2) { + default Kind, T> combineK(Kind, ? extends T> t1, Kind, ? extends T> t2) { return OptionOf.narrowK(t1).fold(cons(OptionOf.narrowK(t2)), Option::some); } } -interface OptionMonoidK extends OptionSemigroupK, MonoidK { +interface OptionMonoidK extends OptionSemigroupK, MonoidK> { OptionMonoidK INSTANCE = new OptionMonoidK() {}; @Override - default Kind zero() { + default Kind, T> zero() { return Option.none(); } } -interface OptionAlternative extends OptionMonoidK, OptionApplicative, Alternative { +interface OptionAlternative extends OptionMonoidK, OptionApplicative, Alternative> { OptionAlternative INSTANCE = new OptionAlternative() {}; } -interface OptionMonadError extends OptionMonad, MonadError { +interface OptionMonadError extends OptionMonad, MonadError, Unit> { OptionMonadError INSTANCE = new OptionMonadError() {}; @Override - default Kind raiseError(Unit error) { + default Kind, A> raiseError(Unit error) { return Option.none(); } @Override - default Kind handleErrorWith(Kind value, - Function1> handler) { + default Kind, A> handleErrorWith(Kind, A> value, + Function1, ? extends A>> handler) { return OptionOf.narrowK(value).fold(() -> OptionOf.narrowK(handler.apply(unit())), this::pure); } } -interface OptionFoldable extends Foldable { +interface OptionFoldable extends Foldable> { OptionFoldable INSTANCE = new OptionFoldable() {}; @Override - default B foldLeft(Kind value, B initial, Function2 mapper) { + default B foldLeft(Kind, ? extends A> value, B initial, Function2 mapper) { return OptionOf.narrowK(value).fold(cons(initial), a -> mapper.apply(initial, a)); } @Override - default Eval foldRight(Kind value, Eval initial, + default Eval foldRight(Kind, ? extends A> value, Eval initial, Function2, ? extends Eval> mapper) { return OptionOf.narrowK(value).fold( cons(initial).andThen(EvalOf::narrowK), a -> mapper.andThen(EvalOf::narrowK).apply(a, initial)); } } -interface OptionTraverse extends Traverse, OptionFoldable { +interface OptionTraverse extends Traverse>, OptionFoldable { OptionTraverse INSTANCE = new OptionTraverse() {}; @Override - default Kind> traverse( - Applicative applicative, Kind value, + default Kind, R>> traverse( + Applicative applicative, Kind, T> value, Function1> mapper) { return value.fix(toOption()).fold( () -> applicative.pure(Option.none().kind()), @@ -206,12 +205,12 @@ default Kind> traverse( } } -interface OptionSemigroupal extends Semigroupal { +interface OptionSemigroupal extends Semigroupal> { OptionSemigroupal INSTANCE = new OptionSemigroupal() {}; @Override - default Kind> product(Kind fa, Kind fb) { + default Kind, Tuple2> product(Kind, ? extends A> fa, Kind, ? extends B> fb) { return OptionOf.narrowK(fa).flatMap(a -> OptionOf.narrowK(fb).map(b -> Tuple.of(a, b))); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/OptionTInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/OptionTInstances.java index 54350d5b7..253cb82c1 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/OptionTInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/OptionTInstances.java @@ -20,7 +20,6 @@ import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.transformer.OptionT; import com.github.tonivade.purefun.transformer.OptionTOf; -import com.github.tonivade.purefun.transformer.OptionT_; import com.github.tonivade.purefun.type.Option; import com.github.tonivade.purefun.typeclasses.Bracket; import com.github.tonivade.purefun.typeclasses.Defer; @@ -32,36 +31,36 @@ public interface OptionTInstances { - static Eq, T>> eq(Eq>> eq) { + static Eq, F>, T>> eq(Eq>> eq) { return (a, b) -> eq.eqv(OptionTOf.narrowK(a).value(), OptionTOf.narrowK(b).value()); } - static Monad> monad(Monad monadF) { + static Monad, F>> monad(Monad monadF) { return OptionTMonad.instance(checkNonNull(monadF)); } - static MonadError, Unit> monadError(Monad monadF) { + static MonadError, F>, Unit> monadError(Monad monadF) { return OptionTMonadErrorFromMonad.instance(checkNonNull(monadF)); } - static MonadError, E> monadError(MonadError monadErrorF) { + static MonadError, F>, E> monadError(MonadError monadErrorF) { return OptionTMonadErrorFromMonadError.instance(checkNonNull(monadErrorF)); } - static MonadThrow> monadThrow(MonadThrow monadThrowF) { + static MonadThrow, F>> monadThrow(MonadThrow monadThrowF) { return OptionTMonadThrow.instance(checkNonNull(checkNonNull(monadThrowF))); } - static MonadDefer> monadDefer(MonadDefer monadDeferF) { + static MonadDefer, F>> monadDefer(MonadDefer monadDeferF) { return OptionTMonadDefer.instance(checkNonNull(monadDeferF)); } - static Reference, A> ref(MonadDefer monadF, A value) { + static Reference, F>, A> ref(MonadDefer monadF, A value) { return Reference.of(monadDefer(monadF), value); } } -interface OptionTMonad extends Monad> { +interface OptionTMonad extends Monad, F>> { static OptionTMonad instance(Monad monadF) { return () -> monadF; @@ -75,14 +74,14 @@ default OptionT pure(T value) { } @Override - default OptionT flatMap(Kind, ? extends T> value, - Function1, ? extends R>> map) { + default OptionT flatMap(Kind, F>, ? extends T> value, + Function1, F>, ? extends R>> map) { return OptionTOf.narrowK(value).flatMap(map.andThen(OptionTOf::narrowK)); } } interface OptionTMonadErrorFromMonad - extends MonadError, Unit>, OptionTMonad { + extends MonadError, F>, Unit>, OptionTMonad { static OptionTMonadErrorFromMonad instance(Monad monadF) { return () -> monadF; @@ -94,8 +93,8 @@ default OptionT raiseError(Unit error) { } @Override - default OptionT handleErrorWith(Kind, A> value, - Function1, ? extends A>> handler) { + default OptionT handleErrorWith(Kind, F>, A> value, + Function1, F>, ? extends A>> handler) { return OptionT.of(monadF(), monadF().flatMap(OptionTOf.narrowK(value).value(), option -> option.fold( @@ -105,7 +104,7 @@ default OptionT handleErrorWith(Kind, A> value, } interface OptionTMonadErrorFromMonadError - extends MonadError, E>, OptionTMonad { + extends MonadError, F>, E>, OptionTMonad { static OptionTMonadErrorFromMonadError instance(MonadError monadF) { return () -> monadF; @@ -120,8 +119,8 @@ default OptionT raiseError(E error) { } @Override - default OptionT handleErrorWith(Kind, A> value, - Function1, ? extends A>> handler) { + default OptionT handleErrorWith(Kind, F>, A> value, + Function1, F>, ? extends A>> handler) { return OptionT.of(monadF(), monadF().handleErrorWith( OptionTOf.narrowK(value).value(), error -> handler.andThen(OptionTOf::narrowK).apply(error).value())); @@ -129,7 +128,7 @@ default OptionT handleErrorWith(Kind, A> value, } interface OptionTMonadThrow - extends MonadThrow>, + extends MonadThrow, F>>, OptionTMonadErrorFromMonadError { static OptionTMonadThrow instance(MonadThrow monadThrowF) { @@ -137,25 +136,25 @@ static OptionTMonadThrow instance(MonadThrow monadThrowF) { } } -interface OptionTDefer extends Defer> { +interface OptionTDefer extends Defer, F>> { MonadDefer monadF(); @Override - default OptionT defer(Producer, ? extends A>> defer) { + default OptionT defer(Producer, F>, ? extends A>> defer) { return OptionT.of(monadF(), monadF().defer(() -> defer.map(OptionTOf::narrowK).get().value())); } } -interface OptionTBracket extends Bracket, Throwable> { +interface OptionTBracket extends Bracket, F>, Throwable> { Bracket monadF(); @Override default OptionT bracket( - Kind, ? extends A> acquire, - Function1, ? extends B>> use, - Function1, Unit>> release) { + Kind, F>, ? extends A> acquire, + Function1, F>, ? extends B>> use, + Function1, F>, Unit>> release) { Kind> bracket = monadF().bracket( acquire.fix(OptionTOf::narrowK).value(), @@ -163,7 +162,7 @@ default OptionT bracket( () -> monadF().raiseError(new NoSuchElementException("could not acquire resource")), value -> use.andThen(OptionTOf::narrowK).apply(value).value()), option -> { - Kind, Unit> fold = option.fold(() -> pure(Unit.unit()), release); + Kind, F>, Unit> fold = option.fold(() -> pure(Unit.unit()), release); Kind> value = fold.fix(toOptionT()).value(); return monadF().map(value, x -> x.fold(Unit::unit, identity())); }); @@ -175,7 +174,7 @@ interface OptionTMonadDefer extends OptionTMonadThrow, OptionTDefer, OptionTBracket, - MonadDefer> { + MonadDefer, F>> { static OptionTMonadDefer instance(MonadDefer monadDeferF) { return () -> monadDeferF; diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/ParInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/ParInstances.java index 4103a61ae..6e9bd51d3 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/ParInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/ParInstances.java @@ -11,7 +11,6 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.concurrent.Par; import com.github.tonivade.purefun.concurrent.ParOf; -import com.github.tonivade.purefun.concurrent.Par_; import com.github.tonivade.purefun.core.Consumer1; import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.core.Producer; @@ -28,46 +27,46 @@ public interface ParInstances { - static Functor functor() { + static Functor> functor() { return ParFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return PureApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return ParMonad.INSTANCE; } - static MonadDefer monadDefer() { + static MonadDefer> monadDefer() { return ParMonadDefer.INSTANCE; } - static Reference reference(A value) { + static Reference, A> reference(A value) { return Reference.of(monadDefer(), value); } - static Resource resource(Par acquire) { + static Resource, A> resource(Par acquire) { return resource(acquire, AutoCloseable::close); } - static Resource resource(Par acquire, Consumer1 release) { + static Resource, A> resource(Par acquire, Consumer1 release) { return Resource.from(monadDefer(), acquire, release); } } -interface ParFunctor extends Functor { +interface ParFunctor extends Functor> { ParFunctor INSTANCE = new ParFunctor() {}; @Override - default Par map(Kind value, Function1 mapper) { + default Par map(Kind, ? extends T> value, Function1 mapper) { return value.fix(ParOf::narrowK).map(mapper); } } -interface ParPure extends Applicative { +interface ParPure extends Applicative> { @Override default Par pure(T value) { return Par.success(value); @@ -79,18 +78,18 @@ interface PureApplicative extends ParPure { PureApplicative INSTANCE = new PureApplicative() {}; @Override - default Par ap(Kind value, - Kind> apply) { + default Par ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return value.fix(ParOf::narrowK).ap(apply.fix(ParOf::narrowK)); } } -interface ParMonad extends ParPure, Monad { +interface ParMonad extends ParPure, Monad> { ParMonad INSTANCE = new ParMonad() {}; @Override - default Par flatMap(Kind value, Function1> map) { + default Par flatMap(Kind, ? extends T> value, Function1, ? extends R>> map) { return value.fix(ParOf::narrowK).flatMap(x -> map.apply(x).fix(ParOf::narrowK)); } @@ -99,13 +98,13 @@ default Par flatMap(Kind value, Function1 Par ap(Kind value, - Kind> apply) { + default Par ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return ParInstances.applicative().ap(value, apply).fix(ParOf::narrowK); } } -interface ParMonadThrow extends ParMonad, MonadThrow { +interface ParMonadThrow extends ParMonad, MonadThrow> { ParMonadThrow INSTANCE = new ParMonadThrow() {}; @@ -115,32 +114,32 @@ default Par raiseError(Throwable error) { } @Override - default Par handleErrorWith(Kind value, - Function1> handler) { + default Par handleErrorWith(Kind, A> value, + Function1, ? extends A>> handler) { return ParOf.narrowK(value).fold(handler.andThen(ParOf::narrowK), Par::success).flatMap(identity()); } } -interface ParDefer extends Defer { +interface ParDefer extends Defer> { @Override - default Par defer(Producer> defer) { + default Par defer(Producer, ? extends A>> defer) { return Par.defer(defer.map(ParOf::narrowK)); } } -interface ParBracket extends Bracket { +interface ParBracket extends Bracket, Throwable> { @Override default Par bracket( - Kind acquire, - Function1> use, - Function1> release) { + Kind, ? extends A> acquire, + Function1, ? extends B>> use, + Function1, Unit>> release) { return Par.bracket(ParOf.narrowK(acquire), use.andThen(ParOf::narrowK), release::apply); } } -interface ParMonadDefer extends ParMonadThrow, ParDefer, ParBracket, MonadDefer { +interface ParMonadDefer extends ParMonadThrow, ParDefer, ParBracket, MonadDefer> { ParMonadDefer INSTANCE = new ParMonadDefer() {}; diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/ProducerInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/ProducerInstances.java index 6c4faa53e..19b266fab 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/ProducerInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/ProducerInstances.java @@ -6,7 +6,6 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.ProducerOf; -import com.github.tonivade.purefun.core.Producer_; import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.core.Producer; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -16,37 +15,37 @@ public interface ProducerInstances { - static Functor functor() { + static Functor> functor() { return ProducerFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return ProducerApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return ProducerMonad.INSTANCE; } - static Comonad comonad() { + static Comonad> comonad() { return ProducerComonad.INSTANCE; } } -interface ProducerFunctor extends Functor { +interface ProducerFunctor extends Functor> { ProducerFunctor INSTANCE = new ProducerFunctor() {}; @Override - default Kind map(Kind value, Function1 mapper) { + default Kind, R> map(Kind, ? extends T> value, Function1 mapper) { return value.fix(ProducerOf::narrowK).map(mapper); } } -interface ProducerPure extends Applicative { +interface ProducerPure extends Applicative> { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return Producer.cons(value); } } @@ -56,33 +55,33 @@ interface ProducerApplicative extends ProducerPure { ProducerApplicative INSTANCE = new ProducerApplicative() {}; @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return ProducerOf.narrowK(value).flatMap(t -> ProducerOf.narrowK(apply).map(f -> f.apply(t))); } } -interface ProducerMonad extends ProducerPure, Monad { +interface ProducerMonad extends ProducerPure, Monad> { ProducerMonad INSTANCE = new ProducerMonad() {}; @Override - default Kind flatMap(Kind value, Function1> mapper) { + default Kind, R> flatMap(Kind, ? extends T> value, Function1, ? extends R>> mapper) { return value.fix(ProducerOf::narrowK).flatMap(mapper.andThen(ProducerOf::narrowK)); } } -interface ProducerComonad extends ProducerFunctor, Comonad { +interface ProducerComonad extends ProducerFunctor, Comonad> { ProducerComonad INSTANCE = new ProducerComonad() {}; @Override - default Kind coflatMap(Kind value, Function1, ? extends B> map) { + default Kind, B> coflatMap(Kind, ? extends A> value, Function1, ? extends A>, ? extends B> map) { return Producer.cons(map.apply(value)); } @Override - default A extract(Kind value) { + default A extract(Kind, ? extends A> value) { return value.fix(ProducerOf::narrowK).get(); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/PromiseInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/PromiseInstances.java index 29c6aaf89..8a214328e 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/PromiseInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/PromiseInstances.java @@ -13,7 +13,6 @@ import com.github.tonivade.purefun.concurrent.Future; import com.github.tonivade.purefun.concurrent.Promise; import com.github.tonivade.purefun.concurrent.PromiseOf; -import com.github.tonivade.purefun.concurrent.Promise_; import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Functor; @@ -21,33 +20,33 @@ public interface PromiseInstances { - static Functor functor() { + static Functor> functor() { return PromiseFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return applicative(Future.DEFAULT_EXECUTOR); } - static Applicative applicative(Executor executor) { + static Applicative> applicative(Executor executor) { return PromiseApplicative.instance(checkNonNull(executor)); } - static Monad monad() { + static Monad> monad() { return monad(Future.DEFAULT_EXECUTOR); } - static Monad monad(Executor executor) { + static Monad> monad(Executor executor) { return PromiseMonad.instance(checkNonNull(executor)); } } -interface PromiseFunctor extends Functor { +interface PromiseFunctor extends Functor> { PromiseFunctor INSTANCE = new PromiseFunctor() {}; @Override - default Kind map(Kind value, Function1 mapper) { + default Kind, R> map(Kind, ? extends T> value, Function1 mapper) { return value.fix(toPromise()).map(mapper); } } @@ -56,10 +55,10 @@ interface PromiseExecutorHolder { Executor executor(); } -interface PromisePure extends Applicative, PromiseExecutorHolder { +interface PromisePure extends Applicative>, PromiseExecutorHolder { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return Promise.make(executor()).succeeded(value); } } @@ -71,21 +70,21 @@ static PromiseApplicative instance(Executor executor) { } @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return value.fix(PromiseOf::narrowK).ap(apply.fix(PromiseOf::narrowK)); } } -interface PromiseMonad extends PromisePure, Monad { +interface PromiseMonad extends PromisePure, Monad> { static PromiseMonad instance(Executor executor) { return () -> executor; } @Override - default Kind flatMap(Kind value, - Function1> map) { + default Kind, R> flatMap(Kind, ? extends T> value, + Function1, ? extends R>> map) { return value.fix(toPromise()).flatMap(map.andThen(PromiseOf::narrowK)); } @@ -94,8 +93,8 @@ default Kind flatMap(Kind value, * applicative version of the ap method */ @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return PromiseInstances.applicative(executor()).ap(value, apply); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/PureIOInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/PureIOInstances.java index 717f7da95..ea83b3d11 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/PureIOInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/PureIOInstances.java @@ -19,7 +19,6 @@ import com.github.tonivade.purefun.effect.UIO; import com.github.tonivade.purefun.effect.PureIO; import com.github.tonivade.purefun.effect.PureIOOf; -import com.github.tonivade.purefun.effect.PureIO_; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -39,65 +38,65 @@ @SuppressWarnings("unchecked") public interface PureIOInstances { - static Functor, E>> functor() { + static Functor, R>, E>> functor() { return PureIOFunctor.INSTANCE; } - static Applicative, E>> applicative() { + static Applicative, R>, E>> applicative() { return PureIOApplicative.INSTANCE; } - static Monad, E>> monad() { + static Monad, R>, E>> monad() { return PureIOMonad.INSTANCE; } - static MonadError, E>, E> monadError() { + static MonadError, R>, E>, E> monadError() { return PureIOMonadError.INSTANCE; } - static MonadThrow, Throwable>> monadThrow() { + static MonadThrow, R>, Throwable>> monadThrow() { return PureIOMonadThrow.INSTANCE; } - static MonadDefer, Throwable>> monadDefer() { + static MonadDefer, R>, Throwable>> monadDefer() { return PureIOMonadDefer.INSTANCE; } - static Async, Throwable>> async() { + static Async, R>, Throwable>> async() { return PureIOAsync.INSTANCE; } - static Concurrent, Throwable>> concurrent() { + static Concurrent, R>, Throwable>> concurrent() { return concurrent(Future.DEFAULT_EXECUTOR); } - static Concurrent, Throwable>> concurrent(Executor executor) { + static Concurrent, R>, Throwable>> concurrent(Executor executor) { return PureIOConcurrent.instance(executor); } - static Console, Throwable>> console() { + static Console, R>, Throwable>> console() { return PureIOConsole.INSTANCE; } - static Runtime, E>> runtime(R env) { + static Runtime, R>, E>> runtime(R env) { return PureIORuntime.instance(env); } } -interface PureIOFunctor extends Functor, E>> { +interface PureIOFunctor extends Functor, R>, E>> { @SuppressWarnings("rawtypes") PureIOFunctor INSTANCE = new PureIOFunctor() {}; @Override default PureIO map( - Kind, E>, ? extends A> value, + Kind, R>, E>, ? extends A> value, Function1 map) { return PureIOOf.narrowK(value).map(map); } } -interface PureIOPure extends Applicative, E>> { +interface PureIOPure extends Applicative, R>, E>> { @Override default PureIO pure(A value) { @@ -112,26 +111,26 @@ interface PureIOApplicative extends PureIOPure { @Override default PureIO - ap(Kind, E>, ? extends A> value, - Kind, E>, ? extends Function1> apply) { + ap(Kind, R>, E>, ? extends A> value, + Kind, R>, E>, ? extends Function1> apply) { return value.fix(PureIOOf::narrowK).ap(apply); } } -interface PureIOMonad extends PureIOPure, Monad, E>> { +interface PureIOMonad extends PureIOPure, Monad, R>, E>> { @SuppressWarnings("rawtypes") PureIOMonad INSTANCE = new PureIOMonad() {}; @Override default PureIO - flatMap(Kind, E>, ? extends A> value, - Function1, E>, ? extends B>> map) { + flatMap(Kind, R>, E>, ? extends A> value, + Function1, R>, E>, ? extends B>> map) { return value.fix(toPureIO()).flatMap(map.andThen(PureIOOf::narrowK)); } } -interface PureIOMonadError extends PureIOMonad, MonadError, E>, E> { +interface PureIOMonadError extends PureIOMonad, MonadError, R>, E>, E> { @SuppressWarnings("rawtypes") PureIOMonadError INSTANCE = new PureIOMonadError() {}; @@ -143,40 +142,40 @@ default PureIO raiseError(E error) { @Override default PureIO handleErrorWith( - Kind, E>, A> value, - Function1, E>, ? extends A>> handler) { + Kind, R>, E>, A> value, + Function1, R>, E>, ? extends A>> handler) { return PureIOOf.narrowK(value).foldM(handler, PureIO::pure); } } interface PureIOMonadThrow - extends PureIOMonadError, MonadThrow, Throwable>> { + extends PureIOMonadError, MonadThrow, R>, Throwable>> { @SuppressWarnings("rawtypes") PureIOMonadThrow INSTANCE = new PureIOMonadThrow() {}; } -interface PureIODefer extends Defer, E>> { +interface PureIODefer extends Defer, R>, E>> { @Override default PureIO - defer(Producer, E>, ? extends A>> defer) { + defer(Producer, R>, E>, ? extends A>> defer) { return PureIO.defer(() -> defer.map(PureIOOf::narrowK).get()); } } -interface PureIOBracket extends PureIOMonadError, Bracket, E>, E> { +interface PureIOBracket extends PureIOMonadError, Bracket, R>, E>, E> { @Override default PureIO - bracket(Kind, E>, ? extends A> acquire, - Function1, E>, ? extends B>> use, - Function1, E>, Unit>> release) { + bracket(Kind, R>, E>, ? extends A> acquire, + Function1, R>, E>, ? extends B>> use, + Function1, R>, E>, Unit>> release) { return PureIO.bracket(acquire, use, release); } } interface PureIOMonadDefer - extends MonadDefer, Throwable>>, PureIODefer, PureIOBracket { + extends MonadDefer, R>, Throwable>>, PureIODefer, PureIOBracket { @SuppressWarnings("rawtypes") PureIOMonadDefer INSTANCE = new PureIOMonadDefer() {}; @@ -187,19 +186,19 @@ default PureIO sleep(Duration duration) { } } -interface PureIOAsync extends Async, Throwable>>, PureIOMonadDefer { +interface PureIOAsync extends Async, R>, Throwable>>, PureIOMonadDefer { @SuppressWarnings("rawtypes") PureIOAsync INSTANCE = new PureIOAsync<>() { }; @Override - default PureIO asyncF(Function1>, Kind, Throwable>, Unit>> consumer) { + default PureIO asyncF(Function1>, Kind, R>, Throwable>, Unit>> consumer) { return PureIO.cancellable((env, cb) -> consumer.andThen(PureIOOf::narrowK).apply(e -> cb.accept(Try.success(e.toEither())))); } } -interface PureIOConcurrent extends Concurrent, Throwable>>, PureIOAsync { +interface PureIOConcurrent extends Concurrent, R>, Throwable>>, PureIOAsync { static PureIOConcurrent instance(Executor executor) { return () -> executor; @@ -208,18 +207,18 @@ static PureIOConcurrent instance(Executor executor) { Executor executor(); @Override - default PureIO, Throwable>, B>>, Tuple2, Throwable>, A>, B>>> racePair( - Kind, Throwable>, ? extends A> fa, Kind, Throwable>, ? extends B> fb) { + default PureIO, R>, Throwable>, B>>, Tuple2, R>, Throwable>, A>, B>>> racePair( + Kind, R>, Throwable>, ? extends A> fa, Kind, R>, Throwable>, ? extends B> fb) { return PureIO.racePair(executor(), fa, fb); } @Override - default PureIO, Throwable>, A>> fork(Kind, Throwable>, ? extends A> value) { + default PureIO, R>, Throwable>, A>> fork(Kind, R>, Throwable>, ? extends A> value) { return value.fix(PureIOOf::narrowK).fork(); } } -final class PureIOConsole implements Console, Throwable>> { +final class PureIOConsole implements Console, R>, Throwable>> { @SuppressWarnings("rawtypes") static final PureIOConsole INSTANCE = new PureIOConsole(); @@ -237,7 +236,7 @@ public PureIO println(String text) { } } -interface PureIORuntime extends Runtime, E>> { +interface PureIORuntime extends Runtime, R>, E>> { static PureIORuntime instance(R env) { return () -> env; @@ -246,22 +245,22 @@ static PureIORuntime instance(R env) { R env(); @Override - default T run(Kind, E>, T> value) { + default T run(Kind, R>, E>, T> value) { return value.fix(toPureIO()).provide(env()).getRight(); } @Override - default Sequence run(Sequence, E>, T>> values) { + default Sequence run(Sequence, R>, E>, T>> values) { return run(PureIO.traverse(values)); } @Override - default Future parRun(Kind, E>, T> value, Executor executor) { + default Future parRun(Kind, R>, E>, T> value, Executor executor) { return value.fix(toPureIO()).runAsync(env()).map(Either::get); } @Override - default Future> parRun(Sequence, E>, T>> values, Executor executor) { + default Future> parRun(Sequence, R>, E>, T>> values, Executor executor) { return parRun(PureIO.traverse(values), executor); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/PureStreamInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/PureStreamInstances.java index 55975465f..031df0c87 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/PureStreamInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/PureStreamInstances.java @@ -7,74 +7,72 @@ import static com.github.tonivade.purefun.core.Precondition.checkNonNull; import com.github.tonivade.purefun.Kind; - import com.github.tonivade.purefun.core.Function1; -import com.github.tonivade.purefun.effect.EIO_; -import com.github.tonivade.purefun.effect.RIO_; -import com.github.tonivade.purefun.effect.Task_; -import com.github.tonivade.purefun.effect.UIO_; -import com.github.tonivade.purefun.effect.URIO_; -import com.github.tonivade.purefun.effect.PureIO_; -import com.github.tonivade.purefun.monad.IO_; +import com.github.tonivade.purefun.effect.EIO; +import com.github.tonivade.purefun.effect.PureIO; +import com.github.tonivade.purefun.effect.RIO; +import com.github.tonivade.purefun.effect.Task; +import com.github.tonivade.purefun.effect.UIO; +import com.github.tonivade.purefun.effect.URIO; +import com.github.tonivade.purefun.monad.IO; import com.github.tonivade.purefun.stream.PureStream; import com.github.tonivade.purefun.stream.PureStreamOf; -import com.github.tonivade.purefun.stream.PureStream_; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Functor; import com.github.tonivade.purefun.typeclasses.Monad; public interface PureStreamInstances { - static PureStream.Of ofIO() { + static PureStream.Of> ofIO() { return PureStream.of(IOInstances.monadDefer()); } - static PureStream.Of, Throwable>> ofPureIO() { + static PureStream.Of, R>, Throwable>> ofPureIO() { return PureStream.of(PureIOInstances.monadDefer()); } - static PureStream.Of ofUIO() { + static PureStream.Of> ofUIO() { return PureStream.of(UIOInstances.monadDefer()); } - static PureStream.Of> ofEIO() { + static PureStream.Of, Throwable>> ofEIO() { return PureStream.of(EIOInstances.monadDefer()); } - static PureStream.Of ofTask() { + static PureStream.Of> ofTask() { return PureStream.of(TaskInstances.monadDefer()); } - static PureStream.Of> ofURIO() { + static PureStream.Of, R>> ofURIO() { return PureStream.of(URIOInstances.monadDefer()); } - static PureStream.Of> ofRIO() { + static PureStream.Of, R>> ofRIO() { return PureStream.of(RIOInstances.monadDefer()); } @SuppressWarnings("unchecked") - static Functor> functor() { + static Functor, F>> functor() { return PureStreamFunctor.INSTANCE; } - static Monad> monad(PureStream.Of streamOf) { + static Monad, F>> monad(PureStream.Of streamOf) { return PureStreamMonad.instance(checkNonNull(streamOf)); } } -interface PureStreamFunctor extends Functor> { +interface PureStreamFunctor extends Functor, F>> { @SuppressWarnings("rawtypes") PureStreamFunctor INSTANCE = new PureStreamFunctor() {}; @Override - default PureStream map(Kind, ? extends T> value, Function1 mapper) { + default PureStream map(Kind, F>, ? extends T> value, Function1 mapper) { return PureStreamOf.narrowK(value).map(mapper); } } -interface PureStreamPure extends Applicative> { +interface PureStreamPure extends Applicative, F>> { PureStream.Of streamOf(); @@ -84,15 +82,15 @@ default PureStream pure(T value) { } } -interface PureStreamMonad extends Monad>, PureStreamPure { +interface PureStreamMonad extends Monad, F>>, PureStreamPure { static PureStreamMonad instance(PureStream.Of streamOf) { return () -> streamOf; } @Override - default PureStream flatMap(Kind, ? extends T> value, - Function1, ? extends R>> mapper) { + default PureStream flatMap(Kind, F>, ? extends T> value, + Function1, F>, ? extends R>> mapper) { return PureStreamOf.narrowK(value).flatMap(mapper.andThen(PureStreamOf::narrowK)); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/RIOInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/RIOInstances.java index 4801caed7..ebea22058 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/RIOInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/RIOInstances.java @@ -18,7 +18,6 @@ import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.effect.RIO; import com.github.tonivade.purefun.effect.RIOOf; -import com.github.tonivade.purefun.effect.RIO_; import com.github.tonivade.purefun.effect.UIO; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Try; @@ -39,60 +38,60 @@ @SuppressWarnings("unchecked") public interface RIOInstances { - static Functor> functor() { + static Functor, R>> functor() { return RIOFunctor.INSTANCE; } - static Applicative> applicative() { + static Applicative, R>> applicative() { return RIOApplicative.INSTANCE; } - static Monad> monad() { + static Monad, R>> monad() { return RIOMonad.INSTANCE; } - static MonadThrow> monadThrow() { + static MonadThrow, R>> monadThrow() { return RIOMonadThrow.INSTANCE; } - static MonadDefer> monadDefer() { + static MonadDefer, R>> monadDefer() { return RIOMonadDefer.INSTANCE; } - static Async> async() { + static Async, R>> async() { return RIOAsync.INSTANCE; } - static Concurrent> concurrent() { + static Concurrent, R>> concurrent() { return concurrent(Future.DEFAULT_EXECUTOR); } - static Concurrent> concurrent(Executor executor) { + static Concurrent, R>> concurrent(Executor executor) { return RIOConcurrent.instance(executor); } - static Console, Throwable>> console() { + static Console, R>, Throwable>> console() { return RIOConsole.INSTANCE; } - static Runtime> runtime(R env) { + static Runtime, R>> runtime(R env) { return RIORuntime.instance(env); } } -interface RIOFunctor extends Functor> { +interface RIOFunctor extends Functor, R>> { @SuppressWarnings("rawtypes") RIOFunctor INSTANCE = new RIOFunctor() {}; @Override default RIO - map(Kind, ? extends A> value, Function1 map) { + map(Kind, R>, ? extends A> value, Function1 map) { return RIOOf.narrowK(value).map(map); } } -interface RIOPure extends Applicative> { +interface RIOPure extends Applicative, R>> { @Override default RIO pure(A value) { @@ -107,26 +106,26 @@ interface RIOApplicative extends RIOPure { @Override default RIO - ap(Kind, ? extends A> value, - Kind, ? extends Function1> apply) { + ap(Kind, R>, ? extends A> value, + Kind, R>, ? extends Function1> apply) { return value.fix(RIOOf::narrowK).ap(apply.fix(RIOOf::narrowK)); } } -interface RIOMonad extends RIOPure, Monad> { +interface RIOMonad extends RIOPure, Monad, R>> { @SuppressWarnings("rawtypes") RIOMonad INSTANCE = new RIOMonad() {}; @Override default RIO - flatMap(Kind, ? extends A> value, - Function1, ? extends B>> map) { + flatMap(Kind, R>, ? extends A> value, + Function1, R>, ? extends B>> map) { return value.fix(toRIO()).flatMap(map.andThen(RIOOf::narrowK)); } } -interface RIOMonadError extends RIOMonad, MonadError, Throwable> { +interface RIOMonadError extends RIOMonad, MonadError, R>, Throwable> { @SuppressWarnings("rawtypes") RIOMonadError INSTANCE = new RIOMonadError<>() { @@ -139,8 +138,8 @@ default RIO raiseError(Throwable error) { @Override default RIO handleErrorWith( - Kind, A> value, - Function1, ? extends A>> handler) { + Kind, R>, A> value, + Function1, R>, ? extends A>> handler) { // XXX: java8 fails to infer types, I have to do this in steps Function1> mapError = handler.andThen(RIOOf::narrowK); Function1> map = RIO::pure; @@ -151,34 +150,34 @@ default RIO handleErrorWith( interface RIOMonadThrow extends RIOMonadError, - MonadThrow> { + MonadThrow, R>> { @SuppressWarnings("rawtypes") RIOMonadThrow INSTANCE = new RIOMonadThrow<>() { }; } -interface RIODefer extends Defer> { +interface RIODefer extends Defer, R>> { @Override default RIO - defer(Producer, ? extends A>> defer) { + defer(Producer, R>, ? extends A>> defer) { return RIO.defer(defer::get); } } -interface RIOBracket extends RIOMonadError, Bracket, Throwable> { +interface RIOBracket extends RIOMonadError, Bracket, R>, Throwable> { @Override default RIO - bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, - Function1, Unit>> release) { + bracket(Kind, R>, ? extends A> acquire, + Function1, R>, ? extends B>> use, + Function1, R>, Unit>> release) { return RIO.bracket(acquire, use, release); } } interface RIOMonadDefer - extends MonadDefer>, RIODefer, RIOBracket { + extends MonadDefer, R>>, RIODefer, RIOBracket { @SuppressWarnings("rawtypes") RIOMonadDefer INSTANCE = new RIOMonadDefer<>() { @@ -190,19 +189,19 @@ default RIO sleep(Duration duration) { } } -interface RIOAsync extends Async>, RIOMonadDefer { +interface RIOAsync extends Async, R>>, RIOMonadDefer { @SuppressWarnings("rawtypes") RIOAsync INSTANCE = new RIOAsync<>() { }; @Override - default RIO asyncF(Function1>, Kind, Unit>> consumer) { + default RIO asyncF(Function1>, Kind, R>, Unit>> consumer) { return RIO.cancellable((env, cb) -> consumer.andThen(RIOOf::narrowK).apply(cb)); } } -interface RIOConcurrent extends RIOAsync, Concurrent> { +interface RIOConcurrent extends RIOAsync, Concurrent, R>> { static RIOConcurrent instance(Executor executor) { return () -> executor; @@ -211,19 +210,19 @@ static RIOConcurrent instance(Executor executor) { Executor executor(); @Override - default RIO, B>>, Tuple2, A>, B>>> racePair( - Kind, ? extends A> fa, Kind, ? extends B> fb) { + default RIO, R>, B>>, Tuple2, R>, A>, B>>> racePair( + Kind, R>, ? extends A> fa, Kind, R>, ? extends B> fb) { return RIO.racePair(executor(), fa, fb); } @Override - default RIO, A>> fork(Kind, ? extends A> value) { + default RIO, R>, A>> fork(Kind, R>, ? extends A> value) { RIO fix = value.fix(RIOOf::narrowK); return fix.fork(); } } -final class RIOConsole implements Console> { +final class RIOConsole implements Console, R>> { @SuppressWarnings("rawtypes") static final RIOConsole INSTANCE = new RIOConsole(); @@ -241,7 +240,7 @@ public RIO println(String text) { } } -interface RIORuntime extends Runtime> { +interface RIORuntime extends Runtime, R>> { static RIORuntime instance(R env) { return () -> env; @@ -250,22 +249,22 @@ static RIORuntime instance(R env) { R env(); @Override - default T run(Kind, T> value) { + default T run(Kind, R>, T> value) { return value.fix(toRIO()).safeRunSync(env()).getOrElseThrow(); } @Override - default Sequence run(Sequence, T>> values) { + default Sequence run(Sequence, R>, T>> values) { return run(RIO.traverse(values.map(RIOOf::narrowK))); } @Override - default Future parRun(Kind, T> value, Executor executor) { + default Future parRun(Kind, R>, T> value, Executor executor) { return value.fix(toRIO()).runAsync(env()); } @Override - default Future> parRun(Sequence, T>> values, Executor executor) { + default Future> parRun(Sequence, R>, T>> values, Executor executor) { return parRun(RIO.traverse(values.map(RIOOf::narrowK)), executor); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/ReaderInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/ReaderInstances.java index 95794602a..da2c15456 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/ReaderInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/ReaderInstances.java @@ -8,23 +8,22 @@ import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.monad.Reader; import com.github.tonivade.purefun.monad.ReaderOf; -import com.github.tonivade.purefun.monad.Reader_; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.MonadReader; @SuppressWarnings("unchecked") public interface ReaderInstances { - static Monad> monad() { + static Monad, R>> monad() { return ReaderMonad.INSTANCE; } - static MonadReader, R> monadReader() { + static MonadReader, R>, R> monadReader() { return ReaderMonadReader.INSTANCE; } } -interface ReaderMonad extends Monad> { +interface ReaderMonad extends Monad, R>> { @SuppressWarnings("rawtypes") ReaderMonad INSTANCE = new ReaderMonad() {}; @@ -35,19 +34,19 @@ default Reader pure(T value) { } @Override - default Reader flatMap(Kind, ? extends T> value, - Function1, ? extends V>> map) { + default Reader flatMap(Kind, R>, ? extends T> value, + Function1, R>, ? extends V>> map) { return ReaderOf.narrowK(value).flatMap(map.andThen(ReaderOf::narrowK)); } } -interface ReaderMonadReader extends MonadReader, R>, ReaderMonad { +interface ReaderMonadReader extends MonadReader, R>, R>, ReaderMonad { @SuppressWarnings("rawtypes") ReaderMonadReader INSTANCE = new ReaderMonadReader() {}; @Override - default Kind, R> ask() { + default Kind, R>, R> ask() { return Reader.env(); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/ScheduleInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/ScheduleInstances.java index b89c3a763..d7d59eca8 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/ScheduleInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/ScheduleInstances.java @@ -8,42 +8,41 @@ import com.github.tonivade.purefun.typeclasses.Functor; import com.github.tonivade.purefun.typeclasses.Profunctor; import com.github.tonivade.purefun.typeclasses.Schedule; -import com.github.tonivade.purefun.typeclasses.Schedule_; @SuppressWarnings("unchecked") public interface ScheduleInstances { - - static Functor, A>> functor() { + + static Functor, F>, A>> functor() { return ScheduleFunctor.INSTANCE; } - - static Profunctor> profunctor() { + + static Profunctor, F>> profunctor() { return ScheduleProfunctor.INSTANCE; } } -interface ScheduleFunctor extends Functor, A>> { - +interface ScheduleFunctor extends Functor, F>, A>> { + @SuppressWarnings("rawtypes") ScheduleFunctor INSTANCE = new ScheduleFunctor() {}; - + @Override default Schedule map( - Kind, A>, ? extends T> value, + Kind, F>, A>, ? extends T> value, Function1 mapper) { return value.fix(toSchedule()).map(mapper); } } -interface ScheduleProfunctor extends Profunctor> { +interface ScheduleProfunctor extends Profunctor, F>> { @SuppressWarnings("rawtypes") ScheduleProfunctor INSTANCE = new ScheduleProfunctor() {}; - + @Override default Schedule dimap( - Kind, A>, ? extends B> value, - Function1 contramap, + Kind, F>, A>, ? extends B> value, + Function1 contramap, Function1 map) { return value.fix(toSchedule()).dimap(contramap, map); } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/SequenceInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/SequenceInstances.java index c29b3212d..dafba79e3 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/SequenceInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/SequenceInstances.java @@ -14,7 +14,6 @@ import com.github.tonivade.purefun.data.ImmutableList; import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.data.SequenceOf; -import com.github.tonivade.purefun.data.Sequence_; import com.github.tonivade.purefun.type.Eval; import com.github.tonivade.purefun.type.EvalOf; import com.github.tonivade.purefun.typeclasses.Alternative; @@ -31,7 +30,7 @@ @SuppressWarnings("unchecked") public interface SequenceInstances { - static Eq> eq(Eq eqElement) { + static Eq, T>> eq(Eq eqElement) { return (a, b) -> { Sequence seq1 = SequenceOf.narrowK(a); Sequence seq2 = SequenceOf.narrowK(b); @@ -48,35 +47,35 @@ static Monoid> monoid() { return (SequenceMonoid) SequenceMonoid.INSTANCE; } - static SemigroupK semigroupK() { + static SemigroupK> semigroupK() { return SequenceSemigroupK.INSTANCE; } - static MonoidK monoidK() { + static MonoidK> monoidK() { return SequenceMonoidK.INSTANCE; } - static Functor functor() { + static Functor> functor() { return SequenceFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return SequenceApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return SequenceMonad.INSTANCE; } - static Alternative alternative() { + static Alternative> alternative() { return SequenceAlternative.INSTANCE; } - static Traverse traverse() { + static Traverse> traverse() { return SequenceTraverse.INSTANCE; } - static Foldable foldable() { + static Foldable> foldable() { return SequenceFoldable.INSTANCE; } } @@ -103,96 +102,96 @@ default Sequence zero() { } } -interface SequenceSemigroupK extends SemigroupK { +interface SequenceSemigroupK extends SemigroupK> { SequenceSemigroupK INSTANCE = new SequenceSemigroupK() {}; @Override - default Kind combineK(Kind t1, Kind t2) { + default Kind, T> combineK(Kind, ? extends T> t1, Kind, ? extends T> t2) { return SequenceOf.narrowK(t1).appendAll(SequenceOf.narrowK(t2)); } } -interface SequenceMonoidK extends MonoidK, SequenceSemigroupK { +interface SequenceMonoidK extends MonoidK>, SequenceSemigroupK { SequenceMonoidK INSTANCE = new SequenceMonoidK() {}; @Override - default Kind zero() { + default Kind, T> zero() { return ImmutableList.empty(); } } -interface SequenceFunctor extends Functor { +interface SequenceFunctor extends Functor> { SequenceFunctor INSTANCE = new SequenceFunctor() {}; @Override - default Kind map(Kind value, Function1 map) { + default Kind, R> map(Kind, ? extends T> value, Function1 map) { return SequenceOf.narrowK(value).map(map); } } -interface SequencePure extends Applicative { +interface SequencePure extends Applicative> { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return ImmutableList.of(value); } } -interface SequenceApplicative extends SequencePure, Applicative { +interface SequenceApplicative extends SequencePure, Applicative> { SequenceApplicative INSTANCE = new SequenceApplicative() {}; @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return SequenceOf.narrowK(apply).flatMap(map -> SequenceOf.narrowK(value).map(map)); } } -interface SequenceMonad extends SequencePure, Monad { +interface SequenceMonad extends SequencePure, Monad> { SequenceMonad INSTANCE = new SequenceMonad() {}; @Override - default Kind flatMap(Kind value, Function1> map) { + default Kind, R> flatMap(Kind, ? extends T> value, Function1, ? extends R>> map) { return SequenceOf.narrowK(value).flatMap(map.andThen(SequenceOf::narrowK)); } } interface SequenceAlternative - extends SequenceApplicative, SequenceMonoidK, Alternative { + extends SequenceApplicative, SequenceMonoidK, Alternative> { SequenceAlternative INSTANCE = new SequenceAlternative() {}; } -interface SequenceFoldable extends Foldable { +interface SequenceFoldable extends Foldable> { SequenceFoldable INSTANCE = new SequenceFoldable() {}; @Override - default B foldLeft(Kind value, B initial, Function2 mapper) { + default B foldLeft(Kind, ? extends A> value, B initial, Function2 mapper) { return SequenceOf.narrowK(value).foldLeft(initial, mapper); } @Override - default Eval foldRight(Kind value, Eval initial, + default Eval foldRight(Kind, ? extends A> value, Eval initial, Function2, ? extends Eval> mapper) { Eval foldRight = SequenceOf.narrowK(value).foldRight(initial, mapper); return EvalOf.narrowK(foldRight); } } -interface SequenceTraverse extends Traverse, SequenceFoldable { +interface SequenceTraverse extends Traverse>, SequenceFoldable { SequenceTraverse INSTANCE = new SequenceTraverse() {}; @Override - default Kind> traverse( - Applicative applicative, Kind value, + default Kind, R>> traverse( + Applicative applicative, Kind, T> value, Function1> mapper) { return value.fix(toSequence()).foldLeft( applicative.pure(Sequence.emptyList()), diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/StateInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/StateInstances.java index 71db60f6c..1506b44cf 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/StateInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/StateInstances.java @@ -13,7 +13,6 @@ import com.github.tonivade.purefun.data.ImmutableList; import com.github.tonivade.purefun.monad.State; import com.github.tonivade.purefun.monad.StateOf; -import com.github.tonivade.purefun.monad.State_; import com.github.tonivade.purefun.typeclasses.Console; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.MonadState; @@ -21,20 +20,20 @@ @SuppressWarnings("unchecked") public interface StateInstances { - static Monad> monad() { + static Monad, S>> monad() { return StateMonad.INSTANCE; } - static MonadState, S> monadState() { + static MonadState, S>, S> monadState() { return StateMonadState.INSTANCE; } - static Console>> console() { + static Console, ImmutableList>> console() { return StateConsole.INSTANCE; } } -interface StateMonad extends Monad> { +interface StateMonad extends Monad, S>> { @SuppressWarnings("rawtypes") StateMonad INSTANCE = new StateMonad() {}; @@ -45,13 +44,13 @@ default State pure(T value) { } @Override - default State flatMap(Kind, ? extends T> value, - Function1, ? extends R>> map) { + default State flatMap(Kind, S>, ? extends T> value, + Function1, S>, ? extends R>> map) { return StateOf.narrowK(value).flatMap(map.andThen(StateOf::narrowK)); } } -interface StateMonadState extends MonadState, S>, StateMonad { +interface StateMonadState extends MonadState, S>, S>, StateMonad { @SuppressWarnings("rawtypes") StateMonadState INSTANCE = new StateMonadState() {}; @@ -67,7 +66,7 @@ default State set(S state) { } } -final class StateConsole implements Console>> { +final class StateConsole implements Console, ImmutableList>> { static final StateConsole INSTANCE = new StateConsole(); diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/StateTInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/StateTInstances.java index 9a0fb4253..821e9789a 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/StateTInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/StateTInstances.java @@ -13,7 +13,6 @@ import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.transformer.StateT; import com.github.tonivade.purefun.transformer.StateTOf; -import com.github.tonivade.purefun.transformer.StateT_; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.MonadError; import com.github.tonivade.purefun.typeclasses.MonadReader; @@ -21,24 +20,24 @@ public interface StateTInstances { - static Monad, S>> monad(Monad monadF) { + static Monad, F>, S>> monad(Monad monadF) { return StateTMonad.instance(checkNonNull(monadF)); } - static MonadState, S>, S> monadState(Monad monadF) { + static MonadState, F>, S>, S> monadState(Monad monadF) { return StateTMonadState.instance(checkNonNull(monadF)); } - static MonadError, S>, E> monadError(MonadError monadErrorF) { + static MonadError, F>, S>, E> monadError(MonadError monadErrorF) { return StateTMonadError.instance(checkNonNull(monadErrorF)); } - static MonadReader, S>, R> monadReader(MonadReader monadReaderF) { + static MonadReader, F>, S>, R> monadReader(MonadReader monadReaderF) { return StateTMonadReader.instance(checkNonNull(monadReaderF)); } } -interface StateTMonad extends Monad, S>> { +interface StateTMonad extends Monad, F>, S>> { static StateTMonad instance(Monad monadF) { return () -> monadF; @@ -52,13 +51,13 @@ default StateT pure(T value) { } @Override - default StateT flatMap(Kind, S>, ? extends T> value, - Function1, S>, ? extends R>> map) { + default StateT flatMap(Kind, F>, S>, ? extends T> value, + Function1, F>, S>, ? extends R>> map) { return StateTOf.narrowK(value).flatMap(map.andThen(StateTOf::narrowK)); } } -interface StateTMonadError extends MonadError, S>, E>, StateTMonad { +interface StateTMonadError extends MonadError, F>, S>, E>, StateTMonad { static StateTMonadError instance(MonadError monadErrorF) { return () -> monadErrorF; @@ -75,8 +74,8 @@ default StateT raiseError(E error) { @Override default StateT handleErrorWith( - Kind, S>, A> value, - Function1, S>, ? extends A>> handler) { + Kind, F>, S>, A> value, + Function1, F>, S>, ? extends A>> handler) { StateT stateT = value.fix(StateTOf::narrowK); return StateT.state(monadF(), state -> monadF().handleErrorWith(stateT.run(state), @@ -84,7 +83,7 @@ default StateT handleErrorWith( } } -interface StateTMonadState extends MonadState, S>, S>, StateTMonad { +interface StateTMonadState extends MonadState, F>, S>, S>, StateTMonad { static StateTMonadState instance(Monad monadF) { return () -> monadF; @@ -101,7 +100,7 @@ default StateT set(S state) { } } -interface StateTMonadReader extends MonadReader, S>, R>, StateTMonad { +interface StateTMonadReader extends MonadReader, F>, S>, R>, StateTMonad { static StateTMonadReader instance(MonadReader monadReaderF) { return () -> monadReaderF; diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/TaskInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/TaskInstances.java index a9eaa2178..e4cbd8d67 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/TaskInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/TaskInstances.java @@ -18,7 +18,6 @@ import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.effect.Task; import com.github.tonivade.purefun.effect.TaskOf; -import com.github.tonivade.purefun.effect.Task_; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -39,74 +38,74 @@ public interface TaskInstances { - static Functor functor() { + static Functor> functor() { return TaskFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return TaskApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return TaskMonad.INSTANCE; } - static MonadError monadError() { + static MonadError, Throwable> monadError() { return TaskMonadError.INSTANCE; } - static MonadThrow monadThrow() { + static MonadThrow> monadThrow() { return TaskMonadThrow.INSTANCE; } - static MonadDefer monadDefer() { + static MonadDefer> monadDefer() { return TaskMonadDefer.INSTANCE; } - static Async async() { + static Async> async() { return TaskAsync.INSTANCE; } - static Concurrent concurrent() { + static Concurrent> concurrent() { return TaskConcurrent.instance(Future.DEFAULT_EXECUTOR); } - static Concurrent concurrent(Executor executor) { + static Concurrent> concurrent(Executor executor) { return TaskConcurrent.instance(executor); } - static Reference ref(A value) { + static Reference, A> ref(A value) { return Reference.of(monadDefer(), value); } - static Resource resource(Task acquire) { + static Resource, A> resource(Task acquire) { return resource(acquire, AutoCloseable::close); } - static Resource resource(Task acquire, Consumer1 release) { + static Resource, A> resource(Task acquire, Consumer1 release) { return Resource.from(monadDefer(), acquire, release); } - static Console console() { + static Console> console() { return TaskConsole.INSTANCE; } - static Runtime runtime() { + static Runtime> runtime() { return TaskRuntime.INSTANCE; } } -interface TaskFunctor extends Functor { +interface TaskFunctor extends Functor> { TaskFunctor INSTANCE = new TaskFunctor() {}; @Override - default Task map(Kind value, Function1 map) { + default Task map(Kind, ? extends A> value, Function1 map) { return TaskOf.narrowK(value).map(map); } } -interface TaskPure extends Applicative { +interface TaskPure extends Applicative> { @Override default Task pure(A value) { @@ -120,25 +119,25 @@ interface TaskApplicative extends TaskPure { @Override default Task - ap(Kind value, - Kind> apply) { + ap(Kind, ? extends A> value, + Kind, ? extends Function1> apply) { return value.fix(TaskOf::narrowK).ap(apply.fix(TaskOf::narrowK)); } } -interface TaskMonad extends TaskPure, Monad { +interface TaskMonad extends TaskPure, Monad> { TaskMonad INSTANCE = new TaskMonad() {}; @Override default Task - flatMap(Kind value, - Function1> map) { + flatMap(Kind, ? extends A> value, + Function1, ? extends B>> map) { return TaskOf.narrowK(value).flatMap(map.andThen(TaskOf::narrowK)); } } -interface TaskMonadError extends TaskMonad, MonadError { +interface TaskMonadError extends TaskMonad, MonadError, Throwable> { TaskMonadError INSTANCE = new TaskMonadError() {}; @@ -149,8 +148,8 @@ default Task raiseError(Throwable error) { @Override default Task handleErrorWith( - Kind value, - Function1> handler) { + Kind, A> value, + Function1, ? extends A>> handler) { // XXX: java8 fails to infer types, I have to do this in steps Function1> mapError = handler.andThen(TaskOf::narrowK); Function1> map = Task::pure; @@ -159,33 +158,33 @@ default Task handleErrorWith( } } -interface TaskMonadThrow extends TaskMonadError, MonadThrow { +interface TaskMonadThrow extends TaskMonadError, MonadThrow> { TaskMonadThrow INSTANCE = new TaskMonadThrow() {}; } -interface TaskDefer extends Defer { +interface TaskDefer extends Defer> { @Override default Task - defer(Producer> defer) { + defer(Producer, ? extends A>> defer) { return Task.defer(defer); } } -interface TaskBracket extends TaskMonadError, Bracket { +interface TaskBracket extends TaskMonadError, Bracket, Throwable> { @Override default Task - bracket(Kind acquire, - Function1> use, - Function1> release) { + bracket(Kind, ? extends A> acquire, + Function1, ? extends B>> use, + Function1, Unit>> release) { return Task.bracket(acquire, use, release); } } interface TaskMonadDefer - extends MonadDefer, TaskDefer, TaskBracket { + extends MonadDefer>, TaskDefer, TaskBracket { TaskMonadDefer INSTANCE = new TaskMonadDefer() {}; @@ -195,17 +194,17 @@ default Task sleep(Duration duration) { } } -interface TaskAsync extends Async, TaskMonadDefer { +interface TaskAsync extends Async>, TaskMonadDefer { TaskAsync INSTANCE = new TaskAsync() {}; @Override - default Task asyncF(Function1>, Kind> consumer) { + default Task asyncF(Function1>, Kind, Unit>> consumer) { return Task.asyncF(consumer.andThen(TaskOf::narrowK)); } } -interface TaskConcurrent extends TaskAsync, Concurrent { +interface TaskConcurrent extends TaskAsync, Concurrent> { static TaskConcurrent instance(Executor executor) { return () -> executor; @@ -214,20 +213,20 @@ static TaskConcurrent instance(Executor executor) { Executor executor(); @Override - default Task>, Tuple2, B>>> racePair(Kind fa, - Kind fb) { + default Task, B>>, Tuple2, A>, B>>> racePair(Kind, ? extends A> fa, + Kind, ? extends B> fb) { return Task.racePair(executor(), fa, fb); } @Override - default Task> fork(Kind value) { + default Task, A>> fork(Kind, ? extends A> value) { Task fix = value.fix(TaskOf::narrowK); return fix.fork(); } } -final class TaskConsole implements Console { +final class TaskConsole implements Console> { public static final TaskConsole INSTANCE = new TaskConsole(); @@ -244,27 +243,27 @@ public Task println(String text) { } } -interface TaskRuntime extends Runtime { +interface TaskRuntime extends Runtime> { TaskRuntime INSTANCE = new TaskRuntime() {}; @Override - default T run(Kind value) { + default T run(Kind, T> value) { return value.fix(toTask()).safeRunSync().getOrElseThrow(); } @Override - default Sequence run(Sequence> values) { + default Sequence run(Sequence, T>> values) { return run(Task.traverse(values.map(TaskOf::narrowK))); } @Override - default Future parRun(Kind value, Executor executor) { + default Future parRun(Kind, T> value, Executor executor) { return value.fix(toTask()).runAsync(); } @Override - default Future> parRun(Sequence> values, Executor executor) { + default Future> parRun(Sequence, T>> values, Executor executor) { return parRun(Task.traverse(values.map(TaskOf::narrowK)), executor); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/TrampolineInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/TrampolineInstances.java index be4e46ee4..387048143 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/TrampolineInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/TrampolineInstances.java @@ -9,7 +9,6 @@ import com.github.tonivade.purefun.core.Producer; import com.github.tonivade.purefun.free.Trampoline; import com.github.tonivade.purefun.free.TrampolineOf; -import com.github.tonivade.purefun.free.Trampoline_; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Defer; import com.github.tonivade.purefun.typeclasses.Functor; @@ -17,38 +16,38 @@ public interface TrampolineInstances { - static Functor functor() { + static Functor> functor() { return TrampolineFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return TrampolineApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return TrampolineMonad.INSTANCE; } - static Defer defer() { + static Defer> defer() { return TrampolineDefer.INSTANCE; } } -interface TrampolineFunctor extends Functor { +interface TrampolineFunctor extends Functor> { TrampolineFunctor INSTANCE = new TrampolineFunctor() {}; @Override - default Kind map( - Kind value, Function1 mapper) { + default Kind, R> map( + Kind, ? extends T> value, Function1 mapper) { return TrampolineOf.narrowK(value).map(mapper); } } -interface TrampolinePure extends Applicative { +interface TrampolinePure extends Applicative> { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return Trampoline.done(value); } } @@ -58,29 +57,29 @@ interface TrampolineApplicative extends TrampolinePure { TrampolineApplicative INSTANCE = new TrampolineApplicative() {}; @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return TrampolineOf.narrowK(value).flatMap(t -> TrampolineOf.narrowK(apply).map(f -> f.apply(t))); } } -interface TrampolineMonad extends TrampolinePure, Monad { +interface TrampolineMonad extends TrampolinePure, Monad> { TrampolineMonad INSTANCE = new TrampolineMonad() {}; @Override - default Kind flatMap(Kind value, - Function1> map) { + default Kind, R> flatMap(Kind, ? extends T> value, + Function1, ? extends R>> map) { return TrampolineOf.narrowK(value).flatMap(map.andThen(TrampolineOf::narrowK)); } } -interface TrampolineDefer extends Defer { +interface TrampolineDefer extends Defer> { TrampolineDefer INSTANCE = new TrampolineDefer() {}; @Override - default Kind defer(Producer> defer) { + default Kind, A> defer(Producer, ? extends A>> defer) { return Trampoline.more(() -> defer.get().fix(TrampolineOf::narrowK)); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/TryInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/TryInstances.java index 5da54d8c8..ce1b6e907 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/TryInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/TryInstances.java @@ -17,7 +17,6 @@ import com.github.tonivade.purefun.type.EvalOf; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.type.TryOf; -import com.github.tonivade.purefun.type.Try_; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Foldable; import com.github.tonivade.purefun.typeclasses.Functor; @@ -28,7 +27,7 @@ public interface TryInstances { - static Eq> eq(Eq eqSuccess) { + static Eq, T>> eq(Eq eqSuccess) { final Eq eqFailure = Eq.throwable(); return (a, b) -> { if (a instanceof Try.Failure(var causeA) && b instanceof Try.Failure(var causeB)) { @@ -41,49 +40,49 @@ static Eq> eq(Eq eqSuccess) { }; } - static Functor functor() { + static Functor> functor() { return TryFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return TryApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return TryMonad.INSTANCE; } - static MonadError monadError() { + static MonadError, Throwable> monadError() { return TryMonadError.INSTANCE; } - static MonadThrow monadThrow() { + static MonadThrow> monadThrow() { return TryMonadThrow.INSTANCE; } - static Foldable foldable() { + static Foldable> foldable() { return TryFoldable.INSTANCE; } - static Traverse traverse() { + static Traverse> traverse() { return TryTraverse.INSTANCE; } } -interface TryFunctor extends Functor { +interface TryFunctor extends Functor> { TryFunctor INSTANCE = new TryFunctor() {}; @Override - default Kind map(Kind value, Function1 mapper) { + default Kind, R> map(Kind, ? extends T> value, Function1 mapper) { return TryOf.narrowK(value).map(mapper); } } -interface TryPure extends Applicative { +interface TryPure extends Applicative> { @Override - default Kind pure(T value) { + default Kind, T> pure(T value) { return Try.success(value); } } @@ -93,28 +92,28 @@ interface TryApplicative extends TryPure { TryApplicative INSTANCE = new TryApplicative() {}; @Override - default Kind ap(Kind value, - Kind> apply) { + default Kind, R> ap(Kind, ? extends T> value, + Kind, ? extends Function1> apply) { return TryOf.narrowK(value).flatMap(t -> TryOf.narrowK(apply).map(f -> f.apply(t))); } } -interface TryMonad extends TryPure, Monad { +interface TryMonad extends TryPure, Monad> { TryMonad INSTANCE = new TryMonad() {}; @Override - default Kind flatMap(Kind value, - Function1> map) { + default Kind, R> flatMap(Kind, ? extends T> value, + Function1, ? extends R>> map) { return TryOf.narrowK(value).flatMap(map.andThen(TryOf::narrowK)); } @Override - default Kind tailRecM(T value, Function1>> map) { + default Kind, R> tailRecM(T value, Function1, Either>> map) { return loop(value, map).run(); } - private Trampoline> loop(T value, Function1>> map) { + private Trampoline, R>> loop(T value, Function1, Either>> map) { return switch (map.andThen(TryOf::narrowK).apply(value)) { case Try.Failure>(var error) -> Trampoline.done(Try.failure(error)); case Try.Success>(Either.Right(var right)) -> Trampoline.done(Try.success(right)); @@ -123,38 +122,38 @@ private Trampoline> loop(T value, Function1 { +interface TryMonadError extends TryMonad, MonadError, Throwable> { TryMonadError INSTANCE = new TryMonadError() {}; @Override - default Kind raiseError(Throwable error) { + default Kind, A> raiseError(Throwable error) { return Try.failure(error); } @Override - default Kind handleErrorWith(Kind value, - Function1> handler) { + default Kind, A> handleErrorWith(Kind, A> value, + Function1, ? extends A>> handler) { return TryOf.narrowK(value).fold(handler.andThen(TryOf::narrowK), Try::success); } } -interface TryMonadThrow extends TryMonadError, MonadThrow { +interface TryMonadThrow extends TryMonadError, MonadThrow> { TryMonadThrow INSTANCE = new TryMonadThrow() {}; } -interface TryFoldable extends Foldable { +interface TryFoldable extends Foldable> { TryFoldable INSTANCE = new TryFoldable() {}; @Override - default B foldLeft(Kind value, B initial, Function2 mapper) { + default B foldLeft(Kind, ? extends A> value, B initial, Function2 mapper) { return TryOf.narrowK(value).fold(t -> initial, a -> mapper.apply(initial, a)); } @Override - default Eval foldRight(Kind value, Eval initial, + default Eval foldRight(Kind, ? extends A> value, Eval initial, Function2, ? extends Eval> mapper) { return TryOf.narrowK(value).fold( cons(initial).andThen(EvalOf::narrowK), @@ -162,13 +161,13 @@ default Eval foldRight(Kind value, Eval, TryFoldable { +interface TryTraverse extends Traverse>, TryFoldable { TryTraverse INSTANCE = new TryTraverse() {}; @Override - default Kind> traverse( - Applicative applicative, Kind value, + default Kind, R>> traverse( + Applicative applicative, Kind, T> value, Function1> mapper) { return TryOf.narrowK(value).fold( t -> applicative.pure(Try.failure(t).kind()), diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/TupleInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/TupleInstances.java index 774aa14d8..8656e1267 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/TupleInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/TupleInstances.java @@ -5,42 +5,41 @@ package com.github.tonivade.purefun.instances; import com.github.tonivade.purefun.Kind; -import com.github.tonivade.purefun.core.Tuple1Of; -import com.github.tonivade.purefun.core.Tuple1_; -import com.github.tonivade.purefun.core.Tuple2Of; -import com.github.tonivade.purefun.core.Tuple2_; import com.github.tonivade.purefun.core.Function1; +import com.github.tonivade.purefun.core.Tuple1; +import com.github.tonivade.purefun.core.Tuple1Of; import com.github.tonivade.purefun.core.Tuple2; +import com.github.tonivade.purefun.core.Tuple2Of; import com.github.tonivade.purefun.typeclasses.Bifunctor; import com.github.tonivade.purefun.typeclasses.Functor; public interface TupleInstances { - static Functor functor() { + static Functor> functor() { return Tuple1Functor.INSTANCE; } - static Bifunctor bifunctor() { + static Bifunctor> bifunctor() { return Tuple2Bifunctor.INSTANCE; } } -interface Tuple1Functor extends Functor { +interface Tuple1Functor extends Functor> { Tuple1Functor INSTANCE = new Tuple1Functor() {}; @Override - default Kind map(Kind value, Function1 map) { + default Kind, R> map(Kind, ? extends T> value, Function1 map) { return value.fix(Tuple1Of::narrowK).map1(map); } } -interface Tuple2Bifunctor extends Bifunctor { +interface Tuple2Bifunctor extends Bifunctor> { Tuple2Bifunctor INSTANCE = new Tuple2Bifunctor() {}; @Override - default Tuple2 bimap(Kind, ? extends B> value, + default Tuple2 bimap(Kind, A>, ? extends B> value, Function1 leftMap, Function1 rightMap) { return value.fix(Tuple2Of::narrowK).map(leftMap, rightMap); diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/UIOInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/UIOInstances.java index 2392132db..aaa05e499 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/UIOInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/UIOInstances.java @@ -18,7 +18,6 @@ import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.effect.UIO; import com.github.tonivade.purefun.effect.UIOOf; -import com.github.tonivade.purefun.effect.UIO_; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -37,62 +36,62 @@ public interface UIOInstances { - static Functor functor() { + static Functor> functor() { return UIOFunctor.INSTANCE; } - static Applicative applicative() { + static Applicative> applicative() { return UIOApplicative.INSTANCE; } - static Monad monad() { + static Monad> monad() { return UIOMonad.INSTANCE; } - static MonadError monadError() { + static MonadError, Throwable> monadError() { return UIOMonadError.INSTANCE; } - static MonadThrow monadThrow() { + static MonadThrow> monadThrow() { return UIOMonadThrow.INSTANCE; } - static MonadDefer monadDefer() { + static MonadDefer> monadDefer() { return UIOMonadDefer.INSTANCE; } - static Async async() { + static Async> async() { return UIOAsync.INSTANCE; } - static Concurrent concurrent() { + static Concurrent> concurrent() { return concurrent(Future.DEFAULT_EXECUTOR); } - static Concurrent concurrent(Executor executor) { + static Concurrent> concurrent(Executor executor) { return UIOConcurrent.instance(executor); } - - static Runtime runtime() { + + static Runtime> runtime() { return UIORuntime.INSTANCE; } - - static Console console() { + + static Console> console() { return UIOConsole.INSTANCE; } } -interface UIOFunctor extends Functor { +interface UIOFunctor extends Functor> { UIOFunctor INSTANCE = new UIOFunctor() {}; @Override - default UIO map(Kind value, Function1 map) { + default UIO map(Kind, ? extends A> value, Function1 map) { return UIOOf.narrowK(value).map(map); } } -interface UIOPure extends Applicative { +interface UIOPure extends Applicative> { @Override default UIO pure(A value) { @@ -105,24 +104,24 @@ interface UIOApplicative extends UIOPure { UIOApplicative INSTANCE = new UIOApplicative() {}; @Override - default UIO ap(Kind value, - Kind> apply) { + default UIO ap(Kind, ? extends A> value, + Kind, ? extends Function1> apply) { return value.fix(UIOOf::narrowK).ap(apply.fix(UIOOf::narrowK)); } } -interface UIOMonad extends UIOPure, Monad { +interface UIOMonad extends UIOPure, Monad> { UIOMonad INSTANCE = new UIOMonad() {}; @Override - default UIO flatMap(Kind value, - Function1> map) { + default UIO flatMap(Kind, ? extends A> value, + Function1, ? extends B>> map) { return value.fix(toUIO()).flatMap(map.andThen(UIOOf::narrowK)); } } -interface UIOMonadError extends UIOMonad, MonadError { +interface UIOMonadError extends UIOMonad, MonadError, Throwable> { UIOMonadError INSTANCE = new UIOMonadError() {}; @@ -133,8 +132,8 @@ default UIO raiseError(Throwable error) { @Override default UIO handleErrorWith( - Kind value, - Function1> handler) { + Kind, A> value, + Function1, ? extends A>> handler) { Function1> mapError = handler.andThen(UIOOf::narrowK); Function1> map = UIO::pure; UIO uio = UIOOf.narrowK(value); @@ -142,33 +141,33 @@ default UIO handleErrorWith( } } -interface UIOMonadThrow extends UIOMonadError, MonadThrow { +interface UIOMonadThrow extends UIOMonadError, MonadThrow> { UIOMonadThrow INSTANCE = new UIOMonadThrow() {}; } -interface UIODefer extends Defer { +interface UIODefer extends Defer> { @Override default UIO - defer(Producer> defer) { + defer(Producer, ? extends A>> defer) { return UIO.defer(defer::get); } } -interface UIOBracket extends UIOMonadError, Bracket { +interface UIOBracket extends UIOMonadError, Bracket, Throwable> { @Override default UIO - bracket(Kind acquire, - Function1> use, - Function1> release) { + bracket(Kind, ? extends A> acquire, + Function1, ? extends B>> use, + Function1, Unit>> release) { return UIO.bracket(acquire, use, release); } } interface UIOMonadDefer - extends MonadDefer, UIODefer, UIOBracket { + extends MonadDefer>, UIODefer, UIOBracket { UIOMonadDefer INSTANCE = new UIOMonadDefer() {}; @@ -178,37 +177,37 @@ default UIO sleep(Duration duration) { } } -interface UIOAsync extends Async, UIOMonadDefer { +interface UIOAsync extends Async>, UIOMonadDefer { UIOAsync INSTANCE = new UIOAsync() {}; - + @Override - default UIO asyncF(Function1>, Kind> consumer) { + default UIO asyncF(Function1>, Kind, Unit>> consumer) { return UIO.cancellable(consumer.andThen(UIOOf::narrowK)); } } -interface UIOConcurrent extends Concurrent, UIOAsync { - +interface UIOConcurrent extends Concurrent>, UIOAsync { + static UIOConcurrent instance(Executor executor) { return () -> executor; } - + Executor executor(); - + @Override - default UIO>, Tuple2, B>>> racePair(Kind fa, Kind fb) { + default UIO, B>>, Tuple2, A>, B>>> racePair(Kind, ? extends A> fa, Kind, ? extends B> fb) { return UIO.racePair(executor(), fa, fb); } - + @Override - default UIO> fork(Kind value) { + default UIO, A>> fork(Kind, ? extends A> value) { UIO fix = value.fix(UIOOf::narrowK); return fix.fork(); } } -final class UIOConsole implements Console { +final class UIOConsole implements Console> { public static final UIOConsole INSTANCE = new UIOConsole(); @@ -225,27 +224,27 @@ public UIO println(String text) { } } -interface UIORuntime extends Runtime { - +interface UIORuntime extends Runtime> { + UIORuntime INSTANCE = new UIORuntime() {}; @Override - default T run(Kind value) { + default T run(Kind, T> value) { return value.fix(toUIO()).unsafeRunSync(); } - + @Override - default Sequence run(Sequence> values) { + default Sequence run(Sequence, T>> values) { return run(UIO.traverse(values.map(UIOOf::narrowK))); } @Override - default Future parRun(Kind value, Executor executor) { + default Future parRun(Kind, T> value, Executor executor) { return value.fix(toUIO()).runAsync(); } - + @Override - default Future> parRun(Sequence> values, Executor executor) { + default Future> parRun(Sequence, T>> values, Executor executor) { return parRun(UIO.traverse(values.map(UIOOf::narrowK)), executor); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/URIOInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/URIOInstances.java index 9558a7f6d..922c6f23e 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/URIOInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/URIOInstances.java @@ -19,7 +19,6 @@ import com.github.tonivade.purefun.effect.UIO; import com.github.tonivade.purefun.effect.URIO; import com.github.tonivade.purefun.effect.URIOOf; -import com.github.tonivade.purefun.effect.URIO_; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.typeclasses.Applicative; @@ -39,60 +38,60 @@ @SuppressWarnings("unchecked") public interface URIOInstances { - static Functor> functor() { + static Functor, R>> functor() { return URIOFunctor.INSTANCE; } - static Applicative> applicative() { + static Applicative, R>> applicative() { return URIOApplicative.INSTANCE; } - static Monad> monad() { + static Monad, R>> monad() { return URIOMonad.INSTANCE; } - static MonadThrow> monadThrow() { + static MonadThrow, R>> monadThrow() { return URIOMonadThrow.INSTANCE; } - static MonadDefer> monadDefer() { + static MonadDefer, R>> monadDefer() { return URIOMonadDefer.INSTANCE; } - static Async> async() { + static Async, R>> async() { return URIOAsync.INSTANCE; } - static Concurrent> concurrent() { + static Concurrent, R>> concurrent() { return concurrent(Future.DEFAULT_EXECUTOR); } - static Concurrent> concurrent(Executor executor) { + static Concurrent, R>> concurrent(Executor executor) { return URIOConcurrent.instance(executor); } - static Console, Throwable>> console() { + static Console, R>, Throwable>> console() { return URIOConsole.INSTANCE; } - static Runtime> runtime(R env) { + static Runtime, R>> runtime(R env) { return URIORuntime.instance(env); } } -interface URIOFunctor extends Functor> { +interface URIOFunctor extends Functor, R>> { @SuppressWarnings("rawtypes") URIOFunctor INSTANCE = new URIOFunctor() {}; @Override - default URIO map(Kind, ? extends A> value, + default URIO map(Kind, R>, ? extends A> value, Function1 map) { return URIOOf.narrowK(value).map(map); } } -interface URIOPure extends Applicative> { +interface URIOPure extends Applicative, R>> { @Override default URIO pure(A value) { @@ -107,26 +106,26 @@ interface URIOApplicative extends URIOPure { @Override default URIO - ap(Kind, ? extends A> value, - Kind, ? extends Function1> apply) { + ap(Kind, R>, ? extends A> value, + Kind, R>, ? extends Function1> apply) { return value.fix(URIOOf::narrowK).ap(apply.fix(URIOOf::narrowK)); } } -interface URIOMonad extends URIOPure, Monad> { +interface URIOMonad extends URIOPure, Monad, R>> { @SuppressWarnings("rawtypes") URIOMonad INSTANCE = new URIOMonad() {}; @Override default URIO - flatMap(Kind, ? extends A> value, - Function1, ? extends B>> map) { + flatMap(Kind, R>, ? extends A> value, + Function1, R>, ? extends B>> map) { return value.fix(toURIO()).flatMap(map.andThen(URIOOf::narrowK)); } } -interface URIOMonadError extends URIOMonad, MonadError, Throwable> { +interface URIOMonadError extends URIOMonad, MonadError, R>, Throwable> { @SuppressWarnings("rawtypes") URIOMonadError INSTANCE = new URIOMonadError<>() { @@ -139,8 +138,8 @@ default URIO raiseError(Throwable error) { @Override default URIO handleErrorWith( - Kind, A> value, - Function1, ? extends A>> handler) { + Kind, R>, A> value, + Function1, R>, ? extends A>> handler) { // XXX: java8 fails to infer types, I have to do this in steps Function1> mapError = handler.andThen(URIOOf::narrowK); Function1> map = URIO::pure; @@ -151,34 +150,34 @@ default URIO handleErrorWith( interface URIOMonadThrow extends URIOMonadError, - MonadThrow> { + MonadThrow, R>> { @SuppressWarnings("rawtypes") URIOMonadThrow INSTANCE = new URIOMonadThrow<>() { }; } -interface URIODefer extends Defer> { +interface URIODefer extends Defer, R>> { @Override default URIO - defer(Producer, ? extends A>> defer) { + defer(Producer, R>, ? extends A>> defer) { return URIO.defer(defer::get); } } -interface URIOBracket extends URIOMonadError, Bracket, Throwable> { +interface URIOBracket extends URIOMonadError, Bracket, R>, Throwable> { @Override default URIO - bracket(Kind, ? extends A> acquire, - Function1, ? extends B>> use, - Function1, Unit>> release) { + bracket(Kind, R>, ? extends A> acquire, + Function1, R>, ? extends B>> use, + Function1, R>, Unit>> release) { return URIO.bracket(acquire, use, release); } } interface URIOMonadDefer - extends MonadDefer>, URIODefer, URIOBracket { + extends MonadDefer, R>>, URIODefer, URIOBracket { @SuppressWarnings("rawtypes") URIOMonadDefer INSTANCE = new URIOMonadDefer<>() { @@ -190,19 +189,19 @@ default URIO sleep(Duration duration) { } } -interface URIOAsync extends Async>, URIOMonadDefer { +interface URIOAsync extends Async, R>>, URIOMonadDefer { @SuppressWarnings("rawtypes") URIOAsync INSTANCE = new URIOAsync<>() { }; @Override - default URIO asyncF(Function1>, Kind, Unit>> consumer) { + default URIO asyncF(Function1>, Kind, R>, Unit>> consumer) { return URIO.cancellable((env, cb) -> consumer.andThen(URIOOf::narrowK).apply(cb)); } } -interface URIOConcurrent extends URIOAsync, Concurrent> { +interface URIOConcurrent extends URIOAsync, Concurrent, R>> { static URIOConcurrent instance(Executor executor) { return () -> executor; @@ -211,20 +210,20 @@ static URIOConcurrent instance(Executor executor) { Executor executor(); @Override - default URIO, B>>, Tuple2, A>, B>>> racePair( - Kind, ? extends A> fa, Kind, ? extends B> fb) { + default URIO, R>, B>>, Tuple2, R>, A>, B>>> racePair( + Kind, R>, ? extends A> fa, Kind, R>, ? extends B> fb) { return URIO.racePair(executor(), fa, fb); } @Override - default URIO, A>> fork(Kind, ? extends A> value) { + default URIO, R>, A>> fork(Kind, R>, ? extends A> value) { URIO fix = value.fix(URIOOf::narrowK); return fix.fork(); } } -final class URIOConsole implements Console> { +final class URIOConsole implements Console, R>> { @SuppressWarnings("rawtypes") static final URIOConsole INSTANCE = new URIOConsole(); @@ -242,7 +241,7 @@ public URIO println(String text) { } } -interface URIORuntime extends Runtime> { +interface URIORuntime extends Runtime, R>> { static URIORuntime instance(R env) { return () -> env; @@ -251,22 +250,22 @@ static URIORuntime instance(R env) { R env(); @Override - default T run(Kind, T> value) { + default T run(Kind, R>, T> value) { return value.fix(toURIO()).safeRunSync(env()).getOrElseThrow(); } @Override - default Sequence run(Sequence, T>> values) { + default Sequence run(Sequence, R>, T>> values) { return run(URIO.traverse(values.map(URIOOf::narrowK))); } @Override - default Future parRun(Kind, T> value, Executor executor) { + default Future parRun(Kind, R>, T> value, Executor executor) { return value.fix(toURIO()).runAsync(env()); } @Override - default Future> parRun(Sequence, T>> values, Executor executor) { + default Future> parRun(Sequence, R>, T>> values, Executor executor) { return parRun(URIO.traverse(values.map(URIOOf::narrowK)), executor); } } \ No newline at end of file diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/ValidationInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/ValidationInstances.java index 04d9f5f17..ad62975d2 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/ValidationInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/ValidationInstances.java @@ -13,7 +13,6 @@ import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Validation; import com.github.tonivade.purefun.type.ValidationOf; -import com.github.tonivade.purefun.type.Validation_; import com.github.tonivade.purefun.typeclasses.Applicative; import com.github.tonivade.purefun.typeclasses.Bifunctor; import com.github.tonivade.purefun.typeclasses.Functor; @@ -26,7 +25,7 @@ @SuppressWarnings("unchecked") public interface ValidationInstances { - static Eq, T>> eq(Eq errorEq, Eq validEq) { + static Eq, E>, T>> eq(Eq errorEq, Eq validEq) { return (a, b) -> { if (a instanceof Validation.Invalid(var invalidA) && b instanceof Validation.Invalid(var invalidB)) { return errorEq.eqv(invalidA, invalidB); @@ -38,59 +37,59 @@ static Eq, T>> eq(Eq errorEq, Eq validEq) }; } - static Functor> functor() { + static Functor, E>> functor() { return ValidationFunctor.INSTANCE; } - static Bifunctor bifunctor() { + static Bifunctor> bifunctor() { return ValidationBifunctor.INSTANCE; } - static Applicative> applicative(Semigroup semigroup) { + static Applicative, E>> applicative(Semigroup semigroup) { return ValidationApplicative.instance(semigroup); } - static Selective> selective(Semigroup semigroup) { + static Selective, E>> selective(Semigroup semigroup) { return ValidationSelective.instance(semigroup); } - static Monad> monad() { + static Monad, E>> monad() { return ValidationMonad.INSTANCE; } - static MonadError, E> monadError() { + static MonadError, E>, E> monadError() { return ValidationMonadError.INSTANCE; } - static MonadThrow> monadThrow() { + static MonadThrow, Throwable>> monadThrow() { return ValidationMonadThrow.INSTANCE; } } -interface ValidationFunctor extends Functor> { +interface ValidationFunctor extends Functor, E>> { @SuppressWarnings("rawtypes") ValidationFunctor INSTANCE = new ValidationFunctor() {}; @Override - default Validation map(Kind, ? extends T> value, + default Validation map(Kind, E>, ? extends T> value, Function1 map) { return ValidationOf.narrowK(value).map(map); } } -interface ValidationBifunctor extends Bifunctor { +interface ValidationBifunctor extends Bifunctor> { ValidationBifunctor INSTANCE = new ValidationBifunctor() {}; @Override - default Validation bimap(Kind, ? extends B> value, + default Validation bimap(Kind, A>, ? extends B> value, Function1 leftMap, Function1 rightMap) { return ValidationOf.narrowK(value).bimap(leftMap, rightMap); } } -interface ValidationPure extends Applicative> { +interface ValidationPure extends Applicative, E>> { @Override default Validation pure(T value) { @@ -98,7 +97,7 @@ default Validation pure(T value) { } } -interface ValidationApplicative extends ValidationPure, Applicative> { +interface ValidationApplicative extends ValidationPure, Applicative, E>> { static ValidationApplicative instance(Semigroup semigroup) { return () -> semigroup; @@ -107,8 +106,8 @@ static ValidationApplicative instance(Semigroup semigroup) { Semigroup semigroup(); @Override - default Validation ap(Kind, ? extends T> value, - Kind, ? extends Function1> apply) { + default Validation ap(Kind, E>, ? extends T> value, + Kind, E>, ? extends Function1> apply) { Validation validation = value.fix(ValidationOf::narrowK); Validation> validationF = apply.fix(ValidationOf::narrowK); @@ -124,37 +123,37 @@ default Validation ap(Kind, ? extends T> value } } -interface ValidationSelective extends ValidationApplicative, Selective> { +interface ValidationSelective extends ValidationApplicative, Selective, E>> { static ValidationSelective instance(Semigroup semigroup) { return () -> semigroup; } @Override - default Validation select(Kind, Either> value, - Kind, Function1> apply) { + default Validation select(Kind, E>, Either> value, + Kind, E>, Function1> apply) { return Validation.select(value.fix(toValidation()), apply.fix(toValidation())); } } -interface ValidationMonad extends ValidationPure, Monad> { +interface ValidationMonad extends ValidationPure, Monad, E>> { @SuppressWarnings("rawtypes") ValidationMonad INSTANCE = new ValidationMonad() {}; @Override - default Validation flatMap(Kind, ? extends T> value, - Function1, ? extends R>> map) { + default Validation flatMap(Kind, E>, ? extends T> value, + Function1, E>, ? extends R>> map) { return ValidationOf.narrowK(value).flatMap(map.andThen(ValidationOf::narrowK)); } @Override - default Kind, R> tailRecM(T value, - Function1, Either>> map) { + default Kind, E>, R> tailRecM(T value, + Function1, E>, Either>> map) { return loop(value, map).run(); } - private Trampoline, R>> loop(T value, Function1, Either>> map) { + private Trampoline, E>, R>> loop(T value, Function1, E>, Either>> map) { return switch (map.andThen(ValidationOf::narrowK).apply(value)) { case Validation.Invalid>(var error) -> Trampoline.done(Validation.invalid(error)); case Validation.Valid>(Either.Right(var right)) -> Trampoline.done(Validation.valid(right)); @@ -163,7 +162,7 @@ private Trampoline, R>> loop(T value, Function1 } } -interface ValidationMonadError extends ValidationMonad, MonadError, E> { +interface ValidationMonadError extends ValidationMonad, MonadError, E>, E> { @SuppressWarnings("rawtypes") ValidationMonadError INSTANCE = new ValidationMonadError() {}; @@ -174,15 +173,15 @@ default Validation raiseError(E error) { } @Override - default Validation handleErrorWith(Kind, A> value, - Function1, ? extends A>> handler) { + default Validation handleErrorWith(Kind, E>, A> value, + Function1, E>, ? extends A>> handler) { return ValidationOf.narrowK(value).fold(handler.andThen(ValidationOf::narrowK), Validation::valid); } } interface ValidationMonadThrow extends ValidationMonadError, - MonadThrow> { + MonadThrow, Throwable>> { ValidationMonadThrow INSTANCE = new ValidationMonadThrow() {}; } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/WriterInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/WriterInstances.java index 29abe5e35..364d34fc0 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/WriterInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/WriterInstances.java @@ -10,18 +10,17 @@ import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.monad.Writer; import com.github.tonivade.purefun.monad.WriterOf; -import com.github.tonivade.purefun.monad.Writer_; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.Monoid; public interface WriterInstances { - static Monad> monad(Monoid monoid) { + static Monad, L>> monad(Monoid monoid) { return WriterMonad.instance(checkNonNull(monoid)); } } -interface WriterMonad extends Monad> { +interface WriterMonad extends Monad, L>> { static WriterMonad instance(Monoid monoid) { return () -> monoid; @@ -35,8 +34,8 @@ default Writer pure(T value) { } @Override - default Writer flatMap(Kind, ? extends T> value, - Function1, ? extends R>> map) { + default Writer flatMap(Kind, L>, ? extends T> value, + Function1, L>, ? extends R>> map) { return WriterOf.narrowK(value).flatMap(map.andThen(WriterOf::narrowK)); } } diff --git a/instances/src/main/java/com/github/tonivade/purefun/instances/WriterTInstances.java b/instances/src/main/java/com/github/tonivade/purefun/instances/WriterTInstances.java index 87db7dc63..fb56f2f6d 100644 --- a/instances/src/main/java/com/github/tonivade/purefun/instances/WriterTInstances.java +++ b/instances/src/main/java/com/github/tonivade/purefun/instances/WriterTInstances.java @@ -15,7 +15,6 @@ import com.github.tonivade.purefun.core.Tuple2; import com.github.tonivade.purefun.transformer.WriterT; import com.github.tonivade.purefun.transformer.WriterTOf; -import com.github.tonivade.purefun.transformer.WriterT_; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.MonadError; import com.github.tonivade.purefun.typeclasses.MonadWriter; @@ -23,23 +22,23 @@ public interface WriterTInstances { - static Monad, L>> monad(Monoid monoid, Monad monadF) { + static Monad, F>, L>> monad(Monoid monoid, Monad monadF) { return WriterTMonad.instance(checkNonNull(monoid), checkNonNull(monadF)); } - static MonadWriter, L>, L> monadWriter(Monoid monoid, Monad monadF) { + static MonadWriter, F>, L>, L> monadWriter(Monoid monoid, Monad monadF) { return WriterTMonadWriter.instance(checkNonNull(monoid), checkNonNull(monadF)); } - static MonadError, L>, E> monadError( + static MonadError, F>, L>, E> monadError( Monoid monoid, MonadError monadErrorF) { return WriterTMonadError.instance(checkNonNull(monoid), checkNonNull(monadErrorF)); } } -interface WriterTMonad extends Monad, L>> { +interface WriterTMonad extends Monad, F>, L>> { - static Monad, L>> instance(Monoid monoid, Monad monadF) { + static Monad, F>, L>> instance(Monoid monoid, Monad monadF) { return new WriterTMonad<>() { @Override @@ -63,16 +62,16 @@ default WriterT pure(T value) { } @Override - default WriterT flatMap(Kind, L>, ? extends T> value, - Function1, L>, ? extends R>> map) { + default WriterT flatMap(Kind, F>, L>, ? extends T> value, + Function1, F>, L>, ? extends R>> map) { return WriterTOf.narrowK(value).flatMap(map.andThen(WriterTOf::narrowK)); } } interface WriterTMonadWriter - extends MonadWriter, L>, L>, WriterTMonad { + extends MonadWriter, F>, L>, L>, WriterTMonad { - static MonadWriter, L>, L> instance(Monoid monoid, Monad monadF) { + static MonadWriter, F>, L>, L> instance(Monoid monoid, Monad monadF) { return new WriterTMonadWriter<>() { @Override @@ -93,13 +92,13 @@ default WriterT writer(Tuple2 value) { } @Override - default WriterT> listen(Kind, L>, ? extends A> value) { + default WriterT> listen(Kind, F>, L>, ? extends A> value) { return value.fix(WriterTOf::narrowK).listen(); } @Override default WriterT pass( - Kind, L>, Tuple2, A>> value) { + Kind, F>, L>, Tuple2, A>> value) { WriterT, A>> writerT = value.fix(WriterTOf::narrowK); return writerT.listen().flatMap((Tuple2, A>> tuple) -> { Operator1 operator = tuple.get2().get1(); @@ -110,9 +109,9 @@ default WriterT pass( } interface WriterTMonadError - extends MonadError, L>, E>, WriterTMonad { + extends MonadError, F>, L>, E>, WriterTMonad { - static MonadError, L>, E> instance( + static MonadError, F>, L>, E> instance( Monoid monoid, MonadError monadErrorF) { return new WriterTMonadError<>() { @@ -139,8 +138,8 @@ default WriterT raiseError(E error) { @Override default WriterT handleErrorWith( - Kind, L>, A> value, - Function1, L>, ? extends A>> handler) { + Kind, F>, L>, A> value, + Function1, F>, L>, ? extends A>> handler) { return WriterT.writer(monoid(), monadF(), monadF().handleErrorWith(value.fix(toWriterT()).value(), error -> handler.apply(error).fix(WriterTOf::narrowK).value())); diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/AlternativeTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/AlternativeTest.java index 3b3387672..a444e6de4 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/AlternativeTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/AlternativeTest.java @@ -8,27 +8,28 @@ import static com.github.tonivade.purefun.laws.AlternativeLaws.verifyLaws; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.Function1; -import com.github.tonivade.purefun.data.Sequence_; +import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.instances.OptionInstances; import com.github.tonivade.purefun.instances.SequenceInstances; public class AlternativeTest { - private final Alternative instance = SequenceInstances.alternative(); + private final Alternative> instance = SequenceInstances.alternative(); private final Function1 twoTimes = a -> a * 2; private final Function1 plusFive = a -> a + 5; @Test public void combineAndAp() { - Kind seven = instance.pure(7); - Kind eight = instance.pure(8); + Kind, Integer> seven = instance.pure(7); + Kind, Integer> eight = instance.pure(8); - Kind result = + Kind, Integer> result = instance.ap(instance.combineK(seven, eight), instance.combineK(instance.pure(twoTimes), instance.pure(plusFive))); diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ApplicativeTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ApplicativeTest.java index 31c8bd381..83c856f9f 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ApplicativeTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ApplicativeTest.java @@ -18,7 +18,6 @@ import com.github.tonivade.purefun.instances.TryInstances; import com.github.tonivade.purefun.instances.ValidationInstances; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; public class ApplicativeTest { @@ -39,7 +38,7 @@ public void laws() { @Test public void map5Some() { - Kind map5 = + Kind, Integer> map5 = OptionInstances.applicative().mapN( Option.some(1), Option.some(2), @@ -52,7 +51,7 @@ public void map5Some() { @Test public void map5None() { - Kind map5 = + Kind, Integer> map5 = OptionInstances.applicative().mapN( Option.some(1), Option.some(2), diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ContravariantTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ContravariantTest.java index c09d42633..efbce721f 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ContravariantTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ContravariantTest.java @@ -12,35 +12,32 @@ import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.core.Function1Of; -import com.github.tonivade.purefun.core.Function1_; import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.instances.ConstInstances; import com.github.tonivade.purefun.instances.Function1Instances; import com.github.tonivade.purefun.instances.IdInstances; import com.github.tonivade.purefun.type.Const; -import com.github.tonivade.purefun.type.Const_; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; public class ContravariantTest { @Test public void constInstance() { - Contravariant> instance = ConstInstances.contravariant(); + Contravariant, String>> instance = ConstInstances.contravariant(); verifyLaws(instance, Const.of("string")); } @Test public void function1Instance() { - Contravariant> instance = Function1Instances.contravariant(); + Contravariant, Double>> instance = Function1Instances.contravariant(); Function1 int2double = Integer::doubleValue; Function1 string2Int = String::length; - Kind, Integer> conest = conest(int2double); - Kind, String> contramap = instance.contramap(conest, string2Int); + Kind, Double>, Integer> conest = conest(int2double); + Kind, Double>, String> contramap = instance.contramap(conest, string2Int); Function1 result = counnest(contramap).fix(Function1Of::narrowK); assertEquals(4.0, result.apply("hola")); @@ -48,9 +45,9 @@ public void function1Instance() { @Test public void composedCovariantContravariant() { - Functor functor = IdInstances.functor(); - Contravariant> contravariant = ConstInstances.contravariant(); - Contravariant>> instance = Contravariant.compose(functor, contravariant); + Functor> functor = IdInstances.functor(); + Contravariant, String>> contravariant = ConstInstances.contravariant(); + Contravariant, Kind, String>>> instance = Contravariant.compose(functor, contravariant); verifyLaws(instance, nest(Id.of(Const.of("string")))); } diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/FoldableTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/FoldableTest.java index 15e5a5aa5..be95e1e30 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/FoldableTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/FoldableTest.java @@ -12,11 +12,12 @@ import static com.github.tonivade.purefun.typeclasses.Nested.nest; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; + import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.data.ImmutableList; import com.github.tonivade.purefun.data.Sequence; -import com.github.tonivade.purefun.data.Sequence_; import com.github.tonivade.purefun.instances.ConstInstances; import com.github.tonivade.purefun.instances.EitherInstances; import com.github.tonivade.purefun.instances.IdInstances; @@ -25,12 +26,9 @@ import com.github.tonivade.purefun.instances.TryInstances; import com.github.tonivade.purefun.type.Const; import com.github.tonivade.purefun.type.Either; -import com.github.tonivade.purefun.type.Either_; import com.github.tonivade.purefun.type.Id; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; import com.github.tonivade.purefun.type.Try; -import com.github.tonivade.purefun.type.Try_; public class FoldableTest { @@ -52,7 +50,7 @@ public void laws() { @Test public void composed() { - Foldable> instance = compose(SequenceInstances.foldable(), OptionInstances.foldable()); + Foldable, Option>> instance = compose(SequenceInstances.foldable(), OptionInstances.foldable()); assertEquals(Integer.valueOf(3), instance.fold(Monoid.integer(), nest(listOf(Option.some(1), Option.none(), Option.some(2))))); @@ -60,7 +58,7 @@ public void composed() { @Test public void sequence() { - Foldable instance = SequenceInstances.foldable(); + Foldable> instance = SequenceInstances.foldable(); assertAll( () -> assertEquals("abc", instance.foldLeft(listOf("a", "b", "c"), "", String::concat)), @@ -73,7 +71,7 @@ public void sequence() { @Test public void either() { - Foldable> instance = EitherInstances.foldable(); + Foldable, Throwable>> instance = EitherInstances.foldable(); assertAll( () -> assertEquals(empty(), instance.foldLeft(Either.left(new Error()), empty(), ImmutableList::append)), @@ -92,7 +90,7 @@ public void either() { @Test public void option() { - Foldable instance = OptionInstances.foldable(); + Foldable> instance = OptionInstances.foldable(); assertAll( () -> assertEquals(empty(), instance.foldLeft(Option.none(), empty(), ImmutableList::append)), @@ -111,7 +109,7 @@ public void option() { @Test public void try_() { - Foldable instance = TryInstances.foldable(); + Foldable> instance = TryInstances.foldable(); assertAll( () -> assertEquals(empty(), instance.foldLeft(Try.failure(), empty(), ImmutableList::append)), diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/FunctionKTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/FunctionKTest.java index cc7ab4bc2..aea790042 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/FunctionKTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/FunctionKTest.java @@ -9,45 +9,43 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.type.Option; import com.github.tonivade.purefun.type.OptionOf; -import com.github.tonivade.purefun.type.Option_; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.type.TryOf; -import com.github.tonivade.purefun.type.Try_; public class FunctionKTest { @Test public void apply() { - Kind success = new OptionToTry().apply(Option.some("hello world!")); + Kind, String> success = new OptionToTry().apply(Option.some("hello world!")); assertEquals(Try.success("hello world!"), success); } @Test public void andThen() { - Kind some = new OptionToTry().andThen(new TryToOption()).apply(Option.some("hello world!")); + Kind, String> some = new OptionToTry().andThen(new TryToOption()).apply(Option.some("hello world!")); assertEquals(Option.some("hello world!"), some); } @Test public void compose() { - Kind some = new OptionToTry().compose(new TryToOption()).apply(Try.success("hello world!")); + Kind, String> some = new OptionToTry().compose(new TryToOption()).apply(Try.success("hello world!")); assertEquals(Try.success("hello world!"), some); } } -class OptionToTry implements FunctionK { +class OptionToTry implements FunctionK, Try> { @Override - public Kind apply(Kind from) { + public Kind, X> apply(Kind, ? extends X> from) { return OptionOf.narrowK(from).map(Try::success).getOrElse(Try::failure); } } -class TryToOption implements FunctionK { +class TryToOption implements FunctionK, Option> { @Override - public Kind apply(Kind from) { + public Kind, X> apply(Kind, ? extends X> from) { return TryOf.narrowK(from).toOption(); } } \ No newline at end of file diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadDeferTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadDeferTest.java index 289b66217..e94f033c8 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadDeferTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadDeferTest.java @@ -26,27 +26,23 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.Producer; import com.github.tonivade.purefun.effect.PureIO; -import com.github.tonivade.purefun.effect.PureIO_; import com.github.tonivade.purefun.monad.IO; -import com.github.tonivade.purefun.monad.IO_; import com.github.tonivade.purefun.transformer.EitherT; import com.github.tonivade.purefun.transformer.EitherTOf; -import com.github.tonivade.purefun.transformer.EitherT_; import com.github.tonivade.purefun.transformer.OptionT; import com.github.tonivade.purefun.transformer.OptionTOf; -import com.github.tonivade.purefun.transformer.OptionT_; import com.github.tonivade.purefun.type.Either; @ExtendWith(MockitoExtension.class) public class MonadDeferTest { - private MonadDefer ioMonadDefer = Instances.monadDefer(); - private MonadDefer, Throwable>> PureIOMonadDefer = - new Instance, Throwable>>(){}.monadDefer(); - private MonadDefer, Throwable>> eitherTMonadDefer = - new Instance, Throwable>>(){}.monadDefer(ioMonadDefer); - private MonadDefer> optionTMonadDefer = - new Instance>(){}.monadDefer(ioMonadDefer); + private MonadDefer> ioMonadDefer = Instances.>monadDefer(); + private MonadDefer, Void>, Throwable>> PureIOMonadDefer = + new Instance, Void>, Throwable>>(){}.monadDefer(); + private MonadDefer, IO>, Throwable>> eitherTMonadDefer = + new Instance, IO>, Throwable>>(){}.monadDefer(ioMonadDefer); + private MonadDefer, IO>> optionTMonadDefer = + new Instance, IO>>(){}.monadDefer(ioMonadDefer); private AutoCloseable resource = Mockito.mock(AutoCloseable.class); @@ -54,7 +50,7 @@ public class MonadDeferTest { public void ioLater(@Mock Producer task) throws Exception { when(task.get()).thenReturn("hola toni"); - Kind later = ioMonadDefer.later(task); + Kind, String> later = ioMonadDefer.later(task); String result = later.fix(toIO()).unsafeRunSync(); @@ -64,7 +60,7 @@ public void ioLater(@Mock Producer task) throws Exception { @Test public void ioBracket() throws Exception { - Kind bracket = + Kind, String> bracket = ioMonadDefer.bracket(IO.pure(resource), r -> IO.pure("done")); String result = bracket.fix(toIO()).unsafeRunSync(); @@ -75,7 +71,7 @@ public void ioBracket() throws Exception { @Test public void ioBracketAcquireError() throws Exception { - Kind bracket = + Kind, String> bracket = ioMonadDefer.bracket(IO.raiseError(new IllegalStateException()), r -> IO.pure("done")); assertThrows(IllegalStateException.class, () -> bracket.fix(toIO()).unsafeRunSync()); @@ -85,7 +81,7 @@ public void ioBracketAcquireError() throws Exception { @Test public void ioBracketUseError() throws Exception { - Kind bracket = + Kind, String> bracket = ioMonadDefer.bracket(IO.pure(resource), r -> IO.raiseError(new UnsupportedOperationException())); assertThrows(UnsupportedOperationException.class, () -> bracket.fix(toIO()).unsafeRunSync()); @@ -95,9 +91,9 @@ public void ioBracketUseError() throws Exception { @Test public void eitherTBracket() throws Exception { - Kind, Throwable>, String> bracket = - eitherTMonadDefer.bracket(EitherT.right(Instances.monad(), resource), - r -> EitherT.right(Instances.monad(), "done")); + Kind, IO>, Throwable>, String> bracket = + eitherTMonadDefer.bracket(EitherT., Throwable, AutoCloseable>right(Instances.>monad(), resource), + r -> EitherT., Throwable, String>right(Instances.>monad(), "done")); String result = bracket.fix(EitherTOf::narrowK).get().fix(toIO()).unsafeRunSync(); @@ -107,9 +103,9 @@ public void eitherTBracket() throws Exception { @Test public void eitherTBracketAcquireError() throws Exception { - Kind, Throwable>, String> bracket = - eitherTMonadDefer.bracket(EitherT.left(Instances.monad(), new IllegalStateException()), - r -> EitherT.right(Instances.monad(), "done")); + Kind, IO>, Throwable>, String> bracket = + eitherTMonadDefer.bracket(EitherT., Throwable, AutoCloseable>left(Instances.>monad(), new IllegalStateException()), + r -> EitherT., Throwable, String>right(Instances.>monad(), "done")); assertThrows(IllegalStateException.class, () -> bracket.fix(EitherTOf::narrowK).value().fix(toIO()).unsafeRunSync()); @@ -119,9 +115,9 @@ public void eitherTBracketAcquireError() throws Exception { @Test public void eitherTBracketUseError() throws Exception { - Kind, Throwable>, String> bracket = - eitherTMonadDefer.bracket(EitherT.right(Instances.monad(), resource), - r -> EitherT.left(Instances.monad(), new UnsupportedOperationException())); + Kind, IO>, Throwable>, String> bracket = + eitherTMonadDefer.bracket(EitherT., Throwable, AutoCloseable>right(Instances.>monad(), resource), + r -> EitherT., Throwable, String>left(Instances.>monad(), new UnsupportedOperationException())); Either unsafeRunSync = bracket.fix(toEitherT()).value().fix(toIO()).unsafeRunSync(); @@ -131,9 +127,9 @@ public void eitherTBracketUseError() throws Exception { @Test public void optionTBracket() throws Exception { - Kind, String> bracket = - optionTMonadDefer.bracket(OptionT.some(Instances.monad(), resource), - r -> OptionT.some(Instances.monad(), "done")); + Kind, IO>, String> bracket = + optionTMonadDefer.bracket(OptionT.some(Instances.>monad(), resource), + r -> OptionT.some(Instances.>monad(), "done")); String result = bracket.fix(OptionTOf::narrowK).getOrElseThrow().fix(toIO()).unsafeRunSync(); @@ -144,9 +140,9 @@ public void optionTBracket() throws Exception { @Test @Disabled public void optionTBracketAcquireError() throws Exception { - Kind, String> bracket = - optionTMonadDefer.bracket(OptionT.none(Instances.monad()), - r -> OptionT.some(Instances.monad(), "done")); + Kind, IO>, String> bracket = + optionTMonadDefer.bracket(OptionT., AutoCloseable>none(Instances.>monad()), + r -> OptionT.some(Instances.>monad(), "done")); NoSuchElementException error = assertThrows(NoSuchElementException.class, () -> bracket.fix(OptionTOf::narrowK).getOrElseThrow().fix(toIO()).unsafeRunSync()); @@ -158,9 +154,9 @@ public void optionTBracketAcquireError() throws Exception { @Test @Disabled public void optionTBracketUseError() throws Exception { - Kind, String> bracket = - optionTMonadDefer.bracket(OptionT.some(Instances.monad(), resource), - r -> OptionT.none(Instances.monad())); + Kind, IO>, String> bracket = + optionTMonadDefer.bracket(OptionT.some(Instances.>monad(), resource), + r -> OptionT., String>none(Instances.>monad())); NoSuchElementException error = assertThrows(NoSuchElementException.class, () -> bracket.fix(OptionTOf::narrowK).getOrElseThrow().fix(toIO()).unsafeRunSync()); @@ -171,7 +167,7 @@ public void optionTBracketUseError() throws Exception { @Test public void pureIOBracket() throws Exception { - Kind, Throwable>, String> bracket = + Kind, Void>, Throwable>, String> bracket = PureIOMonadDefer.bracket(PureIO.pure(resource), r -> PureIO.pure("done")); @@ -183,7 +179,7 @@ public void pureIOBracket() throws Exception { @Test public void pureIOBracketAcquireError() throws Exception { - Kind, Throwable>, String> bracket = + Kind, Void>, Throwable>, String> bracket = PureIOMonadDefer.bracket(PureIO.raiseError(new IllegalStateException()), r -> PureIO.pure("done")); @@ -195,7 +191,7 @@ public void pureIOBracketAcquireError() throws Exception { @Test public void pureIOBracketUseError() throws Exception { - Kind, Throwable>, String> bracket = + Kind, Void>, Throwable>, String> bracket = PureIOMonadDefer.bracket(PureIO.pure(resource), r -> PureIO.raiseError(new UnsupportedOperationException())); diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadErrorTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadErrorTest.java index 0f9e9c896..120b1de54 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadErrorTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadErrorTest.java @@ -4,56 +4,54 @@ */ package com.github.tonivade.purefun.typeclasses; +import static com.github.tonivade.purefun.core.Matcher1.always; +import static com.github.tonivade.purefun.core.Matcher1.is; +import static com.github.tonivade.purefun.core.Producer.cons; +import static com.github.tonivade.purefun.core.Unit.unit; +import static com.github.tonivade.purefun.monad.IOOf.toIO; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; + import org.junit.jupiter.api.Test; + import com.github.tonivade.purefun.Kind; +import com.github.tonivade.purefun.concurrent.Future; import com.github.tonivade.purefun.concurrent.FutureOf; -import com.github.tonivade.purefun.concurrent.Future_; import com.github.tonivade.purefun.core.PartialFunction1; import com.github.tonivade.purefun.core.Unit; +import com.github.tonivade.purefun.effect.EIO; import com.github.tonivade.purefun.effect.EIOOf; -import com.github.tonivade.purefun.effect.EIO_; +import com.github.tonivade.purefun.effect.PureIO; +import com.github.tonivade.purefun.effect.PureIOOf; +import com.github.tonivade.purefun.effect.Task; import com.github.tonivade.purefun.effect.TaskOf; -import com.github.tonivade.purefun.effect.Task_; +import com.github.tonivade.purefun.effect.UIO; import com.github.tonivade.purefun.effect.UIOOf; -import com.github.tonivade.purefun.effect.UIO_; -import com.github.tonivade.purefun.effect.PureIOOf; -import com.github.tonivade.purefun.effect.PureIO_; import com.github.tonivade.purefun.instances.EIOInstances; import com.github.tonivade.purefun.instances.EitherInstances; import com.github.tonivade.purefun.instances.EvalInstances; import com.github.tonivade.purefun.instances.FutureInstances; import com.github.tonivade.purefun.instances.IOInstances; import com.github.tonivade.purefun.instances.OptionInstances; +import com.github.tonivade.purefun.instances.PureIOInstances; import com.github.tonivade.purefun.instances.TaskInstances; import com.github.tonivade.purefun.instances.TryInstances; import com.github.tonivade.purefun.instances.UIOInstances; -import com.github.tonivade.purefun.instances.PureIOInstances; - -import static com.github.tonivade.purefun.core.Matcher1.always; -import static com.github.tonivade.purefun.core.Matcher1.is; -import static com.github.tonivade.purefun.core.Producer.cons; -import static com.github.tonivade.purefun.core.Unit.unit; -import static com.github.tonivade.purefun.monad.IOOf.toIO; -import com.github.tonivade.purefun.monad.IO_; +import com.github.tonivade.purefun.monad.IO; import com.github.tonivade.purefun.type.Either; -import com.github.tonivade.purefun.type.Either_; +import com.github.tonivade.purefun.type.Eval; import com.github.tonivade.purefun.type.EvalOf; -import com.github.tonivade.purefun.type.Eval_; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; import com.github.tonivade.purefun.type.Try; -import com.github.tonivade.purefun.type.Try_; public class MonadErrorTest { - private final MonadError monadError = TryInstances.monadError(); + private final MonadError, Throwable> monadError = TryInstances.monadError(); @Test public void recover() { - Kind recover = + Kind, String> recover = monadError.recover(Try.failure("error"), PartialFunction1.of(always(), Throwable::toString)); assertEquals(Try.success("java.lang.RuntimeException: error"), recover); @@ -61,7 +59,7 @@ public void recover() { @Test public void attempRight() { - Kind> attempt = monadError.attempt(Try.success("hola mundo!")); + Kind, Either> attempt = monadError.attempt(Try.success("hola mundo!")); assertEquals(Try.success(Either.right("hola mundo!")), attempt); } @@ -70,7 +68,7 @@ public void attempRight() { public void attempLeft() { Exception error = new Exception("error"); - Kind> attempt = monadError.attempt(Try.failure(error)); + Kind, Either> attempt = monadError.attempt(Try.failure(error)); assertEquals(Try.success(Either.left(error)), attempt); } @@ -79,7 +77,7 @@ public void attempLeft() { public void ensureError() { Exception error = new Exception("error"); - Kind ensure = + Kind, String> ensure = monadError.ensure(Try.success("not ok"), cons(error), is("ok")); assertEquals(Try.failure(error), ensure); @@ -89,7 +87,7 @@ public void ensureError() { public void ensureOk() { Exception error = new Exception("error"); - Kind ensure = + Kind, String> ensure = monadError.ensure(Try.success("ok"), cons(error), is("ok")); assertEquals(Try.success("ok"), ensure); @@ -98,15 +96,15 @@ public void ensureOk() { @Test public void either() { RuntimeException error = new RuntimeException("error"); - MonadError, Throwable> monadError = EitherInstances.monadError(); + MonadError, Throwable>, Throwable> monadError = EitherInstances.monadError(); - Kind, String> pure = monadError.pure("is not ok"); - Kind, String> raiseError = monadError.raiseError(error); - Kind, String> handleError = + Kind, Throwable>, String> pure = monadError.pure("is not ok"); + Kind, Throwable>, String> raiseError = monadError.raiseError(error); + Kind, Throwable>, String> handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind, String> ensureOk = + Kind, Throwable>, String> ensureOk = monadError.ensure(pure, () -> error, value -> "is not ok".equals(value)); - Kind, String> ensureError = + Kind, Throwable>, String> ensureError = monadError.ensure(pure, () -> error, value -> "is ok?".equals(value)); assertAll( @@ -118,13 +116,13 @@ public void either() { @Test public void option() { - MonadError monadError = OptionInstances.monadError(); + MonadError, Unit> monadError = OptionInstances.monadError(); - Kind pure = monadError.pure("is not ok"); - Kind raiseError = monadError.raiseError(unit()); - Kind handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind ensureOk = monadError.ensure(pure, Unit::unit, "is not ok"::equals); - Kind ensureError = monadError.ensure(pure, Unit::unit, "is ok?"::equals); + Kind, String> pure = monadError.pure("is not ok"); + Kind, String> raiseError = monadError.raiseError(unit()); + Kind, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, String> ensureOk = monadError.ensure(pure, Unit::unit, "is not ok"::equals); + Kind, String> ensureError = monadError.ensure(pure, Unit::unit, "is ok?"::equals); assertAll( () -> assertEquals(Option.none(), raiseError), @@ -136,13 +134,13 @@ public void option() { @Test public void try_() { RuntimeException error = new RuntimeException("error"); - MonadError monadError = TryInstances.monadError(); + MonadError, Throwable> monadError = TryInstances.monadError(); - Kind pure = monadError.pure("is not ok"); - Kind raiseError = monadError.raiseError(error); - Kind handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); + Kind, String> pure = monadError.pure("is not ok"); + Kind, String> raiseError = monadError.raiseError(error); + Kind, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); + Kind, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( () -> assertEquals(Try.failure(error), raiseError), @@ -154,13 +152,13 @@ public void try_() { @Test public void future() { RuntimeException error = new RuntimeException("error"); - MonadError monadError = FutureInstances.monadError(); + MonadError, Throwable> monadError = FutureInstances.monadError(); - Kind pure = monadError.pure("is not ok"); - Kind raiseError = monadError.raiseError(error); - Kind handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); + Kind, String> pure = monadError.pure("is not ok"); + Kind, String> raiseError = monadError.raiseError(error); + Kind, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); + Kind, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( () -> assertEquals(Try.failure(error), FutureOf.narrowK(raiseError).await()), @@ -172,13 +170,13 @@ public void future() { @Test public void eval() { RuntimeException error = new RuntimeException("error"); - MonadError monadError = EvalInstances.monadError(); + MonadError, Throwable> monadError = EvalInstances.monadError(); - Kind pure = monadError.pure("is not ok"); - Kind raiseError = monadError.raiseError(error); - Kind handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); + Kind, String> pure = monadError.pure("is not ok"); + Kind, String> raiseError = monadError.raiseError(error); + Kind, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); + Kind, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( () -> assertThrows(RuntimeException.class, () -> EvalOf.narrowK(raiseError).value()), @@ -190,13 +188,13 @@ public void eval() { @Test public void io() { RuntimeException error = new RuntimeException("error"); - MonadError monadError = IOInstances.monadError(); + MonadError, Throwable> monadError = IOInstances.monadError(); - Kind pure = monadError.pure("is not ok"); - Kind raiseError = monadError.raiseError(error); - Kind handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); + Kind, String> pure = monadError.pure("is not ok"); + Kind, String> raiseError = monadError.raiseError(error); + Kind, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); + Kind, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( () -> assertThrows(RuntimeException.class, () -> raiseError.fix(toIO()).unsafeRunSync()), @@ -208,13 +206,13 @@ public void io() { @Test public void uio() { RuntimeException error = new RuntimeException("error"); - MonadError monadError = UIOInstances.monadError(); + MonadError, Throwable> monadError = UIOInstances.monadError(); - Kind pure = monadError.pure("is not ok"); - Kind raiseError = monadError.raiseError(error); - Kind handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); + Kind, String> pure = monadError.pure("is not ok"); + Kind, String> raiseError = monadError.raiseError(error); + Kind, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); + Kind, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( () -> assertThrows(RuntimeException.class, () -> UIOOf.narrowK(raiseError).unsafeRunSync()), @@ -226,13 +224,13 @@ public void uio() { @Test public void eio() { RuntimeException error = new RuntimeException("error"); - MonadError, Throwable> monadError = EIOInstances.monadThrow(); + MonadError, Throwable>, Throwable> monadError = EIOInstances.monadThrow(); - Kind, String> pure = monadError.pure("is not ok"); - Kind, String> raiseError = monadError.raiseError(error); - Kind, String> handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); + Kind, Throwable>, String> pure = monadError.pure("is not ok"); + Kind, Throwable>, String> raiseError = monadError.raiseError(error); + Kind, Throwable>, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, Throwable>, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); + Kind, Throwable>, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( () -> assertEquals(Either.left(error), EIOOf.narrowK(raiseError).safeRunSync()), @@ -244,13 +242,13 @@ public void eio() { @Test public void task() { RuntimeException error = new RuntimeException("error"); - MonadError monadError = TaskInstances.monadThrow(); + MonadError, Throwable> monadError = TaskInstances.monadThrow(); - Kind pure = monadError.pure("is not ok"); - Kind raiseError = monadError.raiseError(error); - Kind handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); + Kind, String> pure = monadError.pure("is not ok"); + Kind, String> raiseError = monadError.raiseError(error); + Kind, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); + Kind, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( () -> assertEquals(Try.failure(error), TaskOf.narrowK(raiseError).safeRunSync()), @@ -262,13 +260,13 @@ public void task() { @Test public void PureIO() { RuntimeException error = new RuntimeException("error"); - MonadError, Throwable>, Throwable> monadError = PureIOInstances.monadThrow(); + MonadError, Void>, Throwable>, Throwable> monadError = PureIOInstances.monadThrow(); - Kind, Throwable>, String> pure = monadError.pure("is not ok"); - Kind, Throwable>, String> raiseError = monadError.raiseError(error); - Kind, Throwable>, String> handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind, Throwable>, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind, Throwable>, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); + Kind, Void>, Throwable>, String> pure = monadError.pure("is not ok"); + Kind, Void>, Throwable>, String> raiseError = monadError.raiseError(error); + Kind, Void>, Throwable>, String> handleError = monadError.handleError(raiseError, e -> "not an error"); + Kind, Void>, Throwable>, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); + Kind, Void>, Throwable>, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( () -> assertEquals(Either.left(error), PureIOOf.narrowK(raiseError).provide(null)), diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadTest.java index d4447566c..04a350149 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/MonadTest.java @@ -7,16 +7,16 @@ import static com.github.tonivade.purefun.laws.MonadLaws.verifyLaws; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.instances.EitherInstances; import com.github.tonivade.purefun.instances.OptionInstances; import com.github.tonivade.purefun.instances.ValidationInstances; -import com.github.tonivade.purefun.type.Id_; +import com.github.tonivade.purefun.type.Id; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; -import com.github.tonivade.purefun.type.Try_; +import com.github.tonivade.purefun.type.Try; public class MonadTest { @@ -24,17 +24,17 @@ public class MonadTest { @Test public void idMonad() { - verifyLaws(Instances.monad()); + verifyLaws(Instances.>monad()); } @Test public void optionMonad() { - verifyLaws(Instances.monad()); + verifyLaws(Instances.>monad()); } @Test public void tryMonad() { - verifyLaws(Instances.monad()); + verifyLaws(Instances.>monad()); } @Test @@ -49,7 +49,7 @@ public void validationMonad() { @Test public void option() { - Monad monad = OptionInstances.monad(); + Monad> monad = OptionInstances.monad(); Option some = Option.some("asdf"); Option none = Option.none(); diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ProfunctorTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ProfunctorTest.java index c6c49f176..8f30a58f7 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ProfunctorTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/ProfunctorTest.java @@ -5,12 +5,12 @@ package com.github.tonivade.purefun.typeclasses; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; -import com.github.tonivade.purefun.core.Function1Of; -import com.github.tonivade.purefun.core.Function1_; import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.Function1; +import com.github.tonivade.purefun.core.Function1Of; import com.github.tonivade.purefun.instances.Function1Instances; public class ProfunctorTest { @@ -21,9 +21,9 @@ public class ProfunctorTest { @Test public void dimap() { - Profunctor profunctor = Function1Instances.profunctor(); + Profunctor> profunctor = Function1Instances.profunctor(); - Kind, Double> result = profunctor.dimap(parseInt, toString, toDouble); + Kind, Integer>, Double> result = profunctor.dimap(parseInt, toString, toDouble); assertEquals(2.0, result.fix(Function1Of::narrowK).apply(2)); } diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/SelectiveTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/SelectiveTest.java index fdc3cce68..5ada6bc0e 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/SelectiveTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/SelectiveTest.java @@ -5,18 +5,19 @@ package com.github.tonivade.purefun.typeclasses; import org.junit.jupiter.api.Test; + import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.instances.SequenceInstances; import com.github.tonivade.purefun.instances.ValidationInstances; import com.github.tonivade.purefun.laws.SelectiveLaws; -import com.github.tonivade.purefun.type.Validation_; +import com.github.tonivade.purefun.type.Validation; public class SelectiveTest { @Test public void laws() { - Selective>> selective = + Selective, Sequence>> selective = ValidationInstances.selective(SequenceInstances.semigroup()); SelectiveLaws.verifyLaws(selective); diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/SemigroupalTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/SemigroupalTest.java index 9287e2ac4..60a5b7677 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/SemigroupalTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/SemigroupalTest.java @@ -7,13 +7,12 @@ import com.github.tonivade.purefun.core.Tuple; import com.github.tonivade.purefun.instances.OptionInstances; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; public class SemigroupalTest { @Test public void semigroupal() { - Semigroupal instance = OptionInstances.semigroupal(); + Semigroupal> instance = OptionInstances.semigroupal(); assertAll( () -> assertEquals(Option.none(), instance.product(Option.none(), Option.none())), diff --git a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/TraverseTest.java b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/TraverseTest.java index 5f7d9986f..197631cf8 100644 --- a/instances/src/test/java/com/github/tonivade/purefun/typeclasses/TraverseTest.java +++ b/instances/src/test/java/com/github/tonivade/purefun/typeclasses/TraverseTest.java @@ -11,7 +11,6 @@ import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.data.Sequence; -import com.github.tonivade.purefun.data.Sequence_; import com.github.tonivade.purefun.instances.ConstInstances; import com.github.tonivade.purefun.instances.EitherInstances; import com.github.tonivade.purefun.instances.IdInstances; @@ -19,15 +18,10 @@ import com.github.tonivade.purefun.instances.SequenceInstances; import com.github.tonivade.purefun.instances.TryInstances; import com.github.tonivade.purefun.type.Const; -import com.github.tonivade.purefun.type.Const_; import com.github.tonivade.purefun.type.Either; -import com.github.tonivade.purefun.type.Either_; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; import com.github.tonivade.purefun.type.Option; -import com.github.tonivade.purefun.type.Option_; import com.github.tonivade.purefun.type.Try; -import com.github.tonivade.purefun.type.Try_; public class TraverseTest { @@ -35,9 +29,9 @@ public class TraverseTest { public void sequence() { Sequence> seq = listOf(Option.some("a"), Option.some("b"), Option.some("c")); - Traverse instance = SequenceInstances.traverse(); + Traverse> instance = SequenceInstances.traverse(); - Kind> result = + Kind, Kind, String>> result = instance.traverse(OptionInstances.applicative(), seq, x -> x.map(String::toUpperCase)); assertEquals(Option.some(listOf("A", "B", "C")), result); @@ -45,7 +39,7 @@ public void sequence() { @Test public void either() { - Traverse> instance = EitherInstances.traverse(); + Traverse, Throwable>> instance = EitherInstances.traverse(); Exception error = new Exception("error"); @@ -60,7 +54,7 @@ public void either() { @Test public void composed() { - Traverse> composed = Traverse.compose(OptionInstances.traverse(), IdInstances.traverse()); + Traverse, Id>> composed = Traverse.compose(OptionInstances.traverse(), IdInstances.traverse()); assertEquals(Try.success(Option.some(Id.of("HOLA!"))), composed.traverse(TryInstances.applicative(), nest(Option.some(Id.of(Try.success("hola!")))), @@ -69,7 +63,7 @@ public void composed() { @Test public void id() { - Traverse instance = IdInstances.traverse(); + Traverse> instance = IdInstances.traverse(); assertAll( () -> assertEquals(Option.some(Id.of("HELLO!")), @@ -79,7 +73,7 @@ public void id() { @Test public void testConst() { - Traverse> instance = ConstInstances.traverse(); + Traverse, String>> instance = ConstInstances.traverse(); assertAll( () -> assertEquals(Option.some(Const.of("hello!")), @@ -89,7 +83,7 @@ public void testConst() { @Test public void option() { - Traverse instance = OptionInstances.traverse(); + Traverse> instance = OptionInstances.traverse(); assertAll( () -> assertEquals(Try.success(Option.some("HELLO!")), @@ -102,7 +96,7 @@ public void option() { @Test public void testTry() { - Traverse instance = TryInstances.traverse(); + Traverse> instance = TryInstances.traverse(); Exception error = new Exception("error"); diff --git a/monad/src/main/java/com/github/tonivade/purefun/monad/IO.java b/monad/src/main/java/com/github/tonivade/purefun/monad/IO.java index 8054c4190..2e6330e9c 100644 --- a/monad/src/main/java/com/github/tonivade/purefun/monad/IO.java +++ b/monad/src/main/java/com/github/tonivade/purefun/monad/IO.java @@ -42,7 +42,7 @@ import com.github.tonivade.purefun.typeclasses.Fiber; @HigherKind -public sealed interface IO extends IOOf, Effect, Recoverable { +public sealed interface IO extends IOOf, Effect, T>, Recoverable { IO UNIT = pure(Unit.unit()); @@ -76,17 +76,17 @@ default IO map(Function1 map) { } @Override - default IO flatMap(Function1> map) { + default IO flatMap(Function1, ? extends R>> map) { return new FlatMapped<>(this, map); } @Override - default IO andThen(Kind after) { + default IO andThen(Kind, ? extends R> after) { return flatMap(ignore -> after); } @Override - default IO ap(Kind> apply) { + default IO ap(Kind, ? extends Function1> apply) { return parMap2(Future.DEFAULT_EXECUTOR, this, apply, (v, a) -> a.apply(v)); } @@ -108,8 +108,8 @@ default IO redeem(Function1 mapError, return attempt().map(result -> result.fold(mapError, mapper)); } - default IO redeemWith(Function1> mapError, - Function1> mapper) { + default IO redeemWith(Function1, ? extends R>> mapError, + Function1, ? extends R>> mapper) { return attempt().flatMap(result -> result.fold(mapError, mapper)); } @@ -122,7 +122,7 @@ default IO recover(Class type, Function1 error.getClass().equals(type), t -> function.andThen(IO::pure).apply((X) t))); } - default IO recoverWith(PartialFunction1> mapper) { + default IO recoverWith(PartialFunction1, ? extends T>> mapper) { return new Recover<>(this, mapper.andThen(IOOf::narrowK)); } @@ -132,7 +132,7 @@ default IO> timed() { start -> map(result -> Tuple.of(Duration.ofNanos(System.nanoTime() - start), result))); } - default IO> fork() { + default IO, T>> fork() { return async(callback -> { IOConnection connection = IOConnection.cancellable(); Promise promise = runAsync(this, connection); @@ -199,17 +199,17 @@ static IO pure(T value) { return new Pure<>(value); } - static IO> race(Kind fa, Kind fb) { + static IO> race(Kind, ? extends A> fa, Kind, ? extends B> fb) { return race(Future.DEFAULT_EXECUTOR, fa, fb); } - static IO> race(Executor executor, Kind fa, Kind fb) { + static IO> race(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { return racePair(executor, fa, fb).flatMap(either -> either.fold( ta -> ta.get2().cancel().fix(IOOf.toIO()).map(x -> Either.left(ta.get1())), tb -> tb.get1().cancel().fix(IOOf.toIO()).map(x -> Either.right(tb.get2())))); } - static IO>, Tuple2, B>>> racePair(Executor executor, Kind fa, Kind fb) { + static IO, B>>, Tuple2, A>, B>>> racePair(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { return cancellable(callback -> { IOConnection connection1 = IOConnection.cancellable(); @@ -241,7 +241,7 @@ static IO delay(Duration delay, Producer lazy) { return sleep(delay).andThen(task(lazy)); } - static IO suspend(Producer> lazy) { + static IO suspend(Producer, ? extends T>> lazy) { return new Suspend<>(() -> lazy.get().fix(IOOf::narrowK)); } @@ -318,7 +318,7 @@ static IO async(Consumer1>> callback) return cancellable(callback.asFunction().andThen(IO::pure)); } - static IO cancellable(Function1>, Kind> callback) { + static IO cancellable(Function1>, Kind, Unit>> callback) { return new Async<>(callback); } @@ -342,8 +342,8 @@ static IO unit() { return UNIT; } - static IO bracket(Kind acquire, - Function1> use, Function1> release) { + static IO bracket(Kind, ? extends T> acquire, + Function1, ? extends R>> use, Function1, Unit>> release) { return cancellable(callback -> { IOConnection cancellable = IOConnection.cancellable(); @@ -361,37 +361,37 @@ static IO bracket(Kind acquire, }); } - static IO bracket(Kind acquire, - Function1> use, Consumer1 release) { + static IO bracket(Kind, ? extends T> acquire, + Function1, ? extends R>> use, Consumer1 release) { return bracket(acquire, use, release.asFunction().andThen(IO::pure)); } - static IO bracket(Kind acquire, - Function1> use) { + static IO bracket(Kind, ? extends T> acquire, + Function1, ? extends R>> use) { return bracket(acquire, use, AutoCloseable::close); } - static IO sequence(Sequence> sequence) { - Kind initial = IO.unit().kind(); + static IO sequence(Sequence, ?>> sequence) { + Kind, ?> initial = IO.unit().kind(); return sequence.foldLeft(initial, - (Kind a, Kind b) -> a.fix(IOOf::narrowK).andThen(b.fix(IOOf::narrowK))).fix(IOOf::narrowK).andThen(IO.unit()); + (Kind, ?> a, Kind, ?> b) -> a.fix(IOOf::narrowK).andThen(b.fix(IOOf::narrowK))).fix(IOOf::narrowK).andThen(IO.unit()); } - static IO> traverse(Sequence> sequence) { + static IO> traverse(Sequence, A>> sequence) { return traverse(Future.DEFAULT_EXECUTOR, sequence); } - static IO> traverse(Executor executor, Sequence> sequence) { + static IO> traverse(Executor executor, Sequence, A>> sequence) { return sequence.foldLeft(pure(ImmutableList.empty()), - (Kind> xs, Kind a) -> parMap2(executor, xs, a, Sequence::append)); + (Kind, Sequence> xs, Kind, A> a) -> parMap2(executor, xs, a, Sequence::append)); } - static IO parMap2(Kind fa, Kind fb, + static IO parMap2(Kind, ? extends A> fa, Kind, ? extends B> fb, Function2 mapper) { return parMap2(Future.DEFAULT_EXECUTOR, fa, fb, mapper); } - static IO parMap2(Executor executor, Kind fa, Kind fb, + static IO parMap2(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb, Function2 mapper) { return cancellable(callback -> { @@ -413,11 +413,11 @@ static IO parMap2(Executor executor, Kind fa, Kin }); } - static IO> tuple(Kind fa, Kind fb) { + static IO> tuple(Kind, ? extends A> fa, Kind, ? extends B> fb) { return tuple(Future.DEFAULT_EXECUTOR, fa, fb); } - static IO> tuple(Executor executor, Kind fa, Kind fb) { + static IO> tuple(Executor executor, Kind, ? extends A> fa, Kind, ? extends B> fb) { return parMap2(executor, fa, fb, Tuple::of); } @@ -426,7 +426,7 @@ private static Promise runAsync(IO current, IOConnection connection) { } @SuppressWarnings("unchecked") - private static Promise runAsync(Kind current, IOConnection connection, CallStack stack, Promise promise) { + private static Promise runAsync(Kind, T> current, IOConnection connection, CallStack stack, Promise promise) { while (true) { try { current = unwrap(current, stack, identity()); @@ -478,7 +478,7 @@ private static Promise runAsync(Kind current, IOConnection } } - private static Kind unwrap(Kind current, CallStack stack, Function1, Kind> next) { + private static Kind,T> unwrap(Kind, T> current, CallStack stack, Function1, ? extends T>, Kind, ? extends U>> next) { while (true) { if (current instanceof Failure failure) { return stack.sneakyThrow(failure.error); @@ -565,11 +565,11 @@ public String toString() { final class FlatMapped implements IO { - private final Kind current; - private final Function1> next; + private final Kind, ? extends T> current; + private final Function1, ? extends R>> next; private FlatMapped(IO current, - Function1> next) { + Function1, ? extends R>> next) { this.current = checkNonNull(current); this.next = checkNonNull(next); } @@ -596,9 +596,9 @@ public String toString() { final class Async implements IO { - private final Function1>, Kind> callback; + private final Function1>, Kind, Unit>> callback; - private Async(Function1>, Kind> callback) { + private Async(Function1>, Kind, Unit>> callback) { this.callback = checkNonNull(callback); } @@ -610,9 +610,9 @@ public String toString() { final class Suspend implements IO { - private final Producer> lazy; + private final Producer, ? extends T>> lazy; - private Suspend(Producer> lazy) { + private Suspend(Producer, ? extends T>> lazy) { this.lazy = checkNonNull(lazy); } @@ -624,10 +624,10 @@ public String toString() { final class Recover implements IO { - private final Kind current; - private final PartialFunction1> mapper; + private final Kind, T> current; + private final PartialFunction1, ? extends T>> mapper; - private Recover(IO current, PartialFunction1> mapper) { + private Recover(IO current, PartialFunction1, ? extends T>> mapper) { this.current = checkNonNull(current); this.mapper = checkNonNull(mapper); } @@ -645,7 +645,7 @@ sealed interface IOConnection { boolean isCancellable(); - void setCancelToken(Kind cancel); + void setCancelToken(Kind, Unit> cancel); void cancelNow(); @@ -667,7 +667,7 @@ public boolean isCancellable() { } @Override - public void setCancelToken(Kind cancel) { + public void setCancelToken(Kind, Unit> cancel) { // uncancellable } @@ -689,7 +689,7 @@ public StateIO updateState(Operator1 update) { final class Cancellable implements IOConnection { - private Kind cancelToken = IO.UNIT; + private Kind, Unit> cancelToken = IO.UNIT; private final AtomicReference state = new AtomicReference<>(StateIO.INITIAL); private Cancellable() { } @@ -700,7 +700,7 @@ public boolean isCancellable() { } @Override - public void setCancelToken(Kind cancel) { + public void setCancelToken(Kind, Unit> cancel) { this.cancelToken = checkNonNull(cancel); } @@ -795,7 +795,7 @@ public void pop() { } } - public void add(PartialFunction1> mapError) { + public void add(PartialFunction1, ? extends T>> mapError) { if (top == null) { return; } @@ -824,7 +824,7 @@ public Option> tryHandle(Throwable error) { final class StackItem { private int count = 0; - private final Deque>> recover = new ArrayDeque<>(); + private final Deque, ? extends T>>> recover = new ArrayDeque<>(); @Nullable private final StackItem prev; @@ -858,7 +858,7 @@ public void reset() { count = 0; } - public void add(PartialFunction1> mapError) { + public void add(PartialFunction1, ? extends T>> mapError) { recover.addFirst(mapError); } diff --git a/monad/src/main/java/com/github/tonivade/purefun/monad/Reader.java b/monad/src/main/java/com/github/tonivade/purefun/monad/Reader.java index 60c247873..bee08f8ee 100644 --- a/monad/src/main/java/com/github/tonivade/purefun/monad/Reader.java +++ b/monad/src/main/java/com/github/tonivade/purefun/monad/Reader.java @@ -11,7 +11,7 @@ @HigherKind @FunctionalInterface -public non-sealed interface Reader extends ReaderOf, Bindable, A> { +public non-sealed interface Reader extends ReaderOf, Bindable, R>, A> { A eval(R reader); @@ -21,7 +21,7 @@ default Reader map(Function1 mapper) { } @Override - default Reader flatMap(Function1, ? extends B>> mapper) { + default Reader flatMap(Function1, R>, ? extends B>> mapper) { return reader -> mapper.andThen(ReaderOf::narrowK).apply(eval(reader)).eval(reader); } diff --git a/monad/src/main/java/com/github/tonivade/purefun/monad/State.java b/monad/src/main/java/com/github/tonivade/purefun/monad/State.java index 5724872d3..ccc0ee6d2 100644 --- a/monad/src/main/java/com/github/tonivade/purefun/monad/State.java +++ b/monad/src/main/java/com/github/tonivade/purefun/monad/State.java @@ -19,7 +19,7 @@ @HigherKind @FunctionalInterface -public non-sealed interface State extends StateOf, Bindable, A> { +public non-sealed interface State extends StateOf, Bindable, S>, A> { Tuple2 run(S state); @@ -29,7 +29,7 @@ default State map(Function1 mapper) { } @Override - default State flatMap(Function1, ? extends R>> mapper) { + default State flatMap(Function1, S>, ? extends R>> mapper) { return state -> { Tuple2 run = run(state); State narrowK = mapper.andThen(StateOf::narrowK).apply(run.get2()); diff --git a/monad/src/main/java/com/github/tonivade/purefun/monad/Writer.java b/monad/src/main/java/com/github/tonivade/purefun/monad/Writer.java index 705fb7934..96215daf0 100644 --- a/monad/src/main/java/com/github/tonivade/purefun/monad/Writer.java +++ b/monad/src/main/java/com/github/tonivade/purefun/monad/Writer.java @@ -16,7 +16,7 @@ import com.github.tonivade.purefun.typeclasses.Monoid; @HigherKind -public non-sealed interface Writer extends WriterOf, Bindable, A> { +public non-sealed interface Writer extends WriterOf, Bindable, L>, A> { Monoid monoid(); Tuple2 value(); @@ -53,7 +53,7 @@ default Writer bimap(Monoid monoidV, } @Override - default Writer flatMap(Function1, ? extends B>> mapper) { + default Writer flatMap(Function1, L>, ? extends B>> mapper) { Writer apply = mapper.andThen(WriterOf::narrowK).apply(value().get2()); Tuple2 combine = value().map1(log -> monoid().combine(log, apply.getLog())); return writer(monoid(), Tuple.of(combine.get1(), apply.getValue())); diff --git a/monad/src/test/java/com/github/tonivade/purefun/monad/IOTest.java b/monad/src/test/java/com/github/tonivade/purefun/monad/IOTest.java index 4ca60636f..4239ee57a 100644 --- a/monad/src/test/java/com/github/tonivade/purefun/monad/IOTest.java +++ b/monad/src/test/java/com/github/tonivade/purefun/monad/IOTest.java @@ -51,7 +51,7 @@ @ExtendWith(MockitoExtension.class) public class IOTest { - private final Console console = IOInstances.console(); + private final Console> console = IOInstances.console(); @Test public void pure() { @@ -312,7 +312,7 @@ public void raceB() { @Test public void fork() { - IO result = Instances.monad().use() + IO result = Instances.>monad().use() .then(IO.pure("hola")) .flatMap(hello -> IO.delay(Duration.ofSeconds(1), () -> hello + " toni").fork()) .flatMap(Fiber::join).fix(toIO()); @@ -406,7 +406,7 @@ private IO sum(Integer n, Integer sum) { } private IO> currentThreadIO() { - Reference> ref = IOInstances.monadDefer().ref(ImmutableList.empty()); + Reference, ImmutableList> ref = IOInstances.monadDefer().ref(ImmutableList.empty()); IO> currentThread = ref.updateAndGet(list -> list.append("thread-" + Thread.currentThread().threadId())).fix(toIO()); diff --git a/monad/src/test/java/com/github/tonivade/purefun/monad/TaglessTest.java b/monad/src/test/java/com/github/tonivade/purefun/monad/TaglessTest.java index 681f5f5f9..83a37a269 100644 --- a/monad/src/test/java/com/github/tonivade/purefun/monad/TaglessTest.java +++ b/monad/src/test/java/com/github/tonivade/purefun/monad/TaglessTest.java @@ -24,9 +24,9 @@ public class TaglessTest { - private final Program>> stateProgram = + private final Program, ImmutableList>> stateProgram = new Program<>(StateInstances.monad(), StateInstances.console()); - private final Program ioProgram = + private final Program> ioProgram = new Program<>(IOInstances.monad(), IOInstances.console()); @Test diff --git a/optics/src/test/java/com/github/tonivade/purefun/optics/IsoTest.java b/optics/src/test/java/com/github/tonivade/purefun/optics/IsoTest.java index b5c74acd0..9ed85882e 100644 --- a/optics/src/test/java/com/github/tonivade/purefun/optics/IsoTest.java +++ b/optics/src/test/java/com/github/tonivade/purefun/optics/IsoTest.java @@ -13,7 +13,6 @@ import com.github.tonivade.purefun.core.Tuple2; import com.github.tonivade.purefun.type.Option; import com.github.tonivade.purefun.type.OptionOf; -import com.github.tonivade.purefun.type.Option_; public class IsoTest { @@ -22,7 +21,7 @@ public class IsoTest { private final Iso, Point> tupleToPoint = pointToTuple.reverse(); private final Iso pointToPoint = pointToTuple.compose(tupleToPoint); private final Iso identity = Iso.identity(); - private final Iso, Kind> optionToKind = + private final Iso, Kind, String>> optionToKind = Iso.of(Option::kind, OptionOf::narrowK); private final Point point = new Point(1, 2); diff --git a/processor/src/main/java/com/github/tonivade/purefun/processor/HigherKindProcessor.java b/processor/src/main/java/com/github/tonivade/purefun/processor/HigherKindProcessor.java index 1adae9089..0f897c7e5 100644 --- a/processor/src/main/java/com/github/tonivade/purefun/processor/HigherKindProcessor.java +++ b/processor/src/main/java/com/github/tonivade/purefun/processor/HigherKindProcessor.java @@ -72,17 +72,18 @@ private void generate(TypeElement element) throws IOException { packageName = null; className = qualifiedName; } - String witnessName = className + "_"; + String witnessName = witnessOf(packageName, className, element.getTypeParameters()); String typeOfName = annotation.value().isEmpty() ? className + "Of" : annotation.value(); - writeWitness(packageName, witnessName); writeTypeOf(packageName, className, typeOfName, witnessName, element.getTypeParameters()); } - private void writeWitness(@Nullable String packageName, String witnessName) throws IOException { - JavaFileObject witnessFile = createFile(packageName, witnessName); - try (PrintWriter writer = new PrintWriter(witnessFile.openWriter())) { - witness(writer, packageName, witnessName); - } + private String witnessOf(@Nullable String packageName, String className, List types) { + return switch (types.size()) { + case 1 -> className + ""; + case 2 -> className + ""; + case 3 -> className + ""; + default -> throw new UnsupportedOperationException("too many params: " + packageName + "." + className); + }; } private void writeTypeOf(@Nullable String packageName, String className, String typeOfName, String witnessName, @@ -98,21 +99,6 @@ private void writeTypeOf(@Nullable String packageName, String className, String } } - private void witness(PrintWriter writer, @Nullable String packageName, String witnessName) { - if (packageName != null) { - writer.println(packageName(packageName)); - writer.println(); - } - writer.println(import_(generated())); - writer.println(); - writer.println(GENERATED); - writer.println(witnessClass(witnessName)); - writer.println(); - writer.println(privateConstructor(witnessName)); - writer.println(); - writer.println(END); - } - private void generate1(PrintWriter writer, @Nullable String packageName, String className, String typeOfName, String kindName, List list) { String higher1 = "Kind<" + kindName + ", A>"; @@ -124,9 +110,9 @@ private void generate1(PrintWriter writer, @Nullable String packageName, String writer.println(packageName(packageName)); writer.println(); } - writer.println(import_(KIND)); - writer.println(import_(FUNCTION)); - writer.println(import_(generated())); + writer.println(importClass(KIND)); + writer.println(importClass(FUNCTION)); + writer.println(importClass(generated())); writer.println(); writer.println(GENERATED); writer.println(typeOfClass(className, typeOfNameWithParams, higher1)); @@ -149,9 +135,9 @@ private void generate2(PrintWriter writer, @Nullable String packageName, String writer.println(); } writer.println(); - writer.println(import_(KIND)); - writer.println(import_(FUNCTION)); - writer.println(import_(generated())); + writer.println(importClass(KIND)); + writer.println(importClass(FUNCTION)); + writer.println(importClass(generated())); writer.println(); writer.println(GENERATED); writer.println(typeOfClass(className, typeOfNameWithParams, higher2)); @@ -175,9 +161,9 @@ private void generate3(PrintWriter writer, @Nullable String packageName, String writer.println(); } writer.println(); - writer.println(import_(KIND)); - writer.println(import_(FUNCTION)); - writer.println(import_(generated())); + writer.println(importClass(KIND)); + writer.println(importClass(FUNCTION)); + writer.println(importClass(generated())); writer.println(); writer.println(GENERATED); writer.println(typeOfClass(className, typeOfNameWithParams, higher3)); @@ -199,10 +185,6 @@ private String generated() { return JAVAX_ANNOTATION_PROCESSING_GENERATED; } - private static String privateConstructor(String witnessName) { - return " private " + witnessName + "() {}"; - } - private static void narrowK1(PrintWriter writer, String className, String aType, String hkt) { narrowK(writer, "<" + aType + ">", className + "", hkt); } @@ -242,10 +224,6 @@ private static void toTypeOf(PrintWriter writer, String types, String returnType writer.println(); } - private static String witnessClass(String kindName) { - return "public final class " + kindName + " {"; - } - private static String typeOfClass(String typeName, String typeOfName, String type) { return "public sealed interface " + typeOfName + " extends " + type + " permits " + typeName + " {"; } @@ -266,7 +244,7 @@ private static String packageName(String packageName) { return "package " + packageName + ";"; } - private static String import_(String className) { + private static String importClass(String className) { return "import " + className + ";"; } } diff --git a/processor/src/test/java/com/github/tonivade/purefun/processor/HigherKindProcessorTest.java b/processor/src/test/java/com/github/tonivade/purefun/processor/HigherKindProcessorTest.java index e04438c43..82671b37c 100644 --- a/processor/src/test/java/com/github/tonivade/purefun/processor/HigherKindProcessorTest.java +++ b/processor/src/test/java/com/github/tonivade/purefun/processor/HigherKindProcessorTest.java @@ -12,16 +12,6 @@ public class HigherKindProcessorTest { - private final JavaFileObject witness = forSourceLines("test.Foo_", - "package test;", - - "import javax.annotation.processing.Generated;", - - "@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")", - "public final class Foo_ {", - "private Foo_() {}", - "}"); - @Test public void compilesKind1() { JavaFileObject file = forSourceLines("test.Foo", @@ -41,14 +31,14 @@ public void compilesKind1() { "import javax.annotation.processing.Generated;", "@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")", - "public sealed interface FooOf extends Kind permits Foo {", + "public sealed interface FooOf extends Kind, A> permits Foo {", "@SuppressWarnings(\"unchecked\")", - "static Foo narrowK(Kind hkt) {", + "static Foo narrowK(Kind, ? extends A> hkt) {", "return (Foo) hkt;", "}", - "static Function, Foo> toFoo() {", + "static Function, ? extends A>, Foo> toFoo() {", "return FooOf::narrowK;", "}", @@ -56,7 +46,7 @@ public void compilesKind1() { assert_().about(javaSource()).that(file) .processedWith(new HigherKindProcessor()) - .compilesWithoutError().and().generatesSources(generated, witness); + .compilesWithoutError().and().generatesSources(generated); } @Test @@ -74,29 +64,22 @@ public void compilesKind1NoPackage() { "import javax.annotation.processing.Generated;", "@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")", - "public sealed interface FooOf extends Kind permits Foo {", + "public sealed interface FooOf extends Kind, A> permits Foo {", "@SuppressWarnings(\"unchecked\")", - "static Foo narrowK(Kind hkt) {", + "static Foo narrowK(Kind, ? extends A> hkt) {", "return (Foo) hkt;", "}", - "static Function, Foo> toFoo() {", + "static Function, ? extends A>, Foo> toFoo() {", "return FooOf::narrowK;", "}", "}"); - JavaFileObject generatedWitness = forSourceLines("Foo_", - "import javax.annotation.processing.Generated;", - - "@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")", - "public final class Foo_ {", - "private Foo_() {}", - "}"); assert_().about(javaSource()).that(file) .processedWith(new HigherKindProcessor()) - .compilesWithoutError().and().generatesSources(generated, generatedWitness); + .compilesWithoutError().and().generatesSources(generated); } @Test @@ -118,14 +101,14 @@ public void compilesKind1Bounds() { "import javax.annotation.processing.Generated;", "@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")", - "public sealed interface FooOf extends Kind permits Foo {", + "public sealed interface FooOf extends Kind, A> permits Foo {", "@SuppressWarnings(\"unchecked\")", - "static Foo narrowK(Kind hkt) {", + "static Foo narrowK(Kind, ? extends A> hkt) {", "return (Foo) hkt;", "}", - "static Function, Foo> toFoo() {", + "static Function, ? extends A>, Foo> toFoo() {", "return FooOf::narrowK;", "}", @@ -133,7 +116,7 @@ public void compilesKind1Bounds() { assert_().about(javaSource()).that(file) .processedWith(new HigherKindProcessor()) - .compilesWithoutError().and().generatesSources(generated, witness); + .compilesWithoutError().and().generatesSources(generated); } @Test @@ -187,14 +170,14 @@ public void compilesKind2() { "import javax.annotation.processing.Generated;", "@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")", - "public sealed interface FooOf extends Kind, B> permits Foo {", + "public sealed interface FooOf extends Kind, A>, B> permits Foo {", "@SuppressWarnings(\"unchecked\")", - "static Foo narrowK(Kind, ? extends B> hkt) {", + "static Foo narrowK(Kind, A>, ? extends B> hkt) {", "return (Foo) hkt;", "}", - "static Function, ? extends B>, Foo> toFoo() {", + "static Function, A>, ? extends B>, Foo> toFoo() {", "return FooOf::narrowK;", "}", @@ -202,7 +185,7 @@ public void compilesKind2() { assert_().about(javaSource()).that(file) .processedWith(new HigherKindProcessor()) - .compilesWithoutError().and().generatesSources(generated, witness); + .compilesWithoutError().and().generatesSources(generated); } @Test @@ -224,14 +207,14 @@ public void compilesKind3() { "import javax.annotation.processing.Generated;", "@Generated(\"com.github.tonivade.purefun.processor.HigherKindProcessor\")", - "public sealed interface FooOf extends Kind, B>, C> permits Foo {", + "public sealed interface FooOf extends Kind, A>, B>, C> permits Foo {", "@SuppressWarnings(\"unchecked\")", - "static Foo narrowK(Kind, B>, ? extends C> hkt) {", + "static Foo narrowK(Kind, A>, B>, ? extends C> hkt) {", "return (Foo) hkt;", "}", - "static Function, B>, ? extends C>, Foo> toFoo() {", + "static Function, A>, B>, ? extends C>, Foo> toFoo() {", "return FooOf::narrowK;", "}", @@ -239,6 +222,6 @@ public void compilesKind3() { assert_().about(javaSource()).that(file) .processedWith(new HigherKindProcessor()) - .compilesWithoutError().and().generatesSources(generated, witness); + .compilesWithoutError().and().generatesSources(generated); } } \ No newline at end of file diff --git a/stream/src/main/java/com/github/tonivade/purefun/stream/Cons.java b/stream/src/main/java/com/github/tonivade/purefun/stream/Cons.java index 4e6b1ecfb..f8f4e5b05 100644 --- a/stream/src/main/java/com/github/tonivade/purefun/stream/Cons.java +++ b/stream/src/main/java/com/github/tonivade/purefun/stream/Cons.java @@ -124,7 +124,7 @@ public PureStream mapEval(Function1 PureStream flatMap(Function1, ? extends R>> map) { + public PureStream flatMap(Function1, F>, ? extends R>> map) { return suspendF(() -> monad.map( monad.map(head, map.andThen(PureStreamOf::narrowK)), diff --git a/stream/src/main/java/com/github/tonivade/purefun/stream/Nil.java b/stream/src/main/java/com/github/tonivade/purefun/stream/Nil.java index c83b28c03..2d04b6a3d 100644 --- a/stream/src/main/java/com/github/tonivade/purefun/stream/Nil.java +++ b/stream/src/main/java/com/github/tonivade/purefun/stream/Nil.java @@ -112,7 +112,7 @@ public PureStream mapEval(Function1 PureStream flatMap(Function1, ? extends R>> map) { + public PureStream flatMap(Function1, F>, ? extends R>> map) { return new Nil<>(monad); } diff --git a/stream/src/main/java/com/github/tonivade/purefun/stream/PureStream.java b/stream/src/main/java/com/github/tonivade/purefun/stream/PureStream.java index 312d55d6b..7a729f1a3 100644 --- a/stream/src/main/java/com/github/tonivade/purefun/stream/PureStream.java +++ b/stream/src/main/java/com/github/tonivade/purefun/stream/PureStream.java @@ -31,7 +31,7 @@ @HigherKind public sealed interface PureStream - extends PureStreamOf, Bindable, T> + extends PureStreamOf, Bindable, F>, T> permits Cons, Suspend, Nil { default PureStream head() { @@ -68,9 +68,9 @@ Kind foldRight(Kind begin, @Override PureStream map(Function1 map); @Override - PureStream flatMap(Function1, ? extends R>> map); + PureStream flatMap(Function1, F>, ? extends R>> map); @Override - default PureStream andThen(Kind, ? extends R> next) { + default PureStream andThen(Kind, F>, ? extends R> next) { return flatMap(ignore -> next); } diff --git a/stream/src/main/java/com/github/tonivade/purefun/stream/Suspend.java b/stream/src/main/java/com/github/tonivade/purefun/stream/Suspend.java index 4eb314e4c..ed254c142 100644 --- a/stream/src/main/java/com/github/tonivade/purefun/stream/Suspend.java +++ b/stream/src/main/java/com/github/tonivade/purefun/stream/Suspend.java @@ -114,7 +114,7 @@ public PureStream mapEval(Function1 PureStream flatMap(Function1, ? extends R>> map) { + public PureStream flatMap(Function1, F>, ? extends R>> map) { return lazyMap(s -> s.flatMap(map)); } diff --git a/stream/src/test/java/com/github/tonivade/purefun/stream/PureStreamTest.java b/stream/src/test/java/com/github/tonivade/purefun/stream/PureStreamTest.java index e00275742..a18f0c606 100644 --- a/stream/src/test/java/com/github/tonivade/purefun/stream/PureStreamTest.java +++ b/stream/src/test/java/com/github/tonivade/purefun/stream/PureStreamTest.java @@ -26,34 +26,29 @@ import com.github.tonivade.purefun.core.Tuple2; import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.effect.EIO; -import com.github.tonivade.purefun.effect.EIO_; import com.github.tonivade.purefun.effect.Task; -import com.github.tonivade.purefun.effect.Task_; import com.github.tonivade.purefun.effect.UIO; -import com.github.tonivade.purefun.effect.UIO_; import com.github.tonivade.purefun.effect.URIO; import com.github.tonivade.purefun.effect.PureIO; -import com.github.tonivade.purefun.effect.PureIO_; import com.github.tonivade.purefun.instances.PureStreamInstances; import com.github.tonivade.purefun.monad.IO; -import com.github.tonivade.purefun.monad.IO_; import com.github.tonivade.purefun.stream.PureStream.Of; import com.github.tonivade.purefun.type.Option; public class PureStreamTest { - private final Of streamOfIO = PureStreamInstances.ofIO(); - private final Of streamOfUIO = PureStreamInstances.ofUIO(); - private final Of streamOfTask = PureStreamInstances.ofTask(); - private final Of> streamOfEIO = PureStreamInstances.ofEIO(); - private final Of, Throwable>> streamOfPureIO = PureStreamInstances.ofPureIO(); + private final Of> streamOfIO = PureStreamInstances.ofIO(); + private final Of> streamOfUIO = PureStreamInstances.ofUIO(); + private final Of> streamOfTask = PureStreamInstances.ofTask(); + private final Of, Throwable>> streamOfEIO = PureStreamInstances.ofEIO(); + private final Of, Void>, Throwable>> streamOfPureIO = PureStreamInstances.ofPureIO(); @Test public void map() { - PureStream pure1 = PureStream.pure("hola"); - PureStream pure2 = PureStream.pure(" mundo"); + PureStream, String> pure1 = PureStream.pure("hola"); + PureStream, String> pure2 = PureStream.pure(" mundo"); - PureStream result = pure1.concat(pure2).map(String::toUpperCase); + PureStream, String> result = pure1.concat(pure2).map(String::toUpperCase); IO foldRight = result.foldRight(IO.pure(""), (a, b) -> b.fix(toIO()).map(x -> x + a)) .fix(toIO()); @@ -63,10 +58,10 @@ public void map() { @Test public void flatMap() { - PureStream pure1 = PureStream.pure("hola"); - PureStream pure2 = PureStream.pure(" mundo"); + PureStream, String> pure1 = PureStream.pure("hola"); + PureStream, String> pure2 = PureStream.pure(" mundo"); - PureStream result = pure1.concat(pure2).flatMap(string -> PureStream.pure(string.toUpperCase())); + PureStream, String> result = pure1.concat(pure2).flatMap(string -> PureStream.pure(string.toUpperCase())); IO foldLeft = result.asString().fix(toIO()); @@ -75,115 +70,115 @@ public void flatMap() { @Test public void mapEval() { - PureStream stream = PureStream.from(listOf(1, 2, 3)); + PureStream, Integer> stream = PureStream.from(listOf(1, 2, 3)); - PureStream result = stream.mapEval(i -> IO.task(() -> i * 2)); + PureStream, Integer> result = stream.mapEval(i -> IO.task(() -> i * 2)); assertEquals(Integer.valueOf(12), run(result.foldLeft(0, Integer::sum))); } @Test public void append() { - PureStream stream = PureStream.from(listOf(1, 2, 3)); + PureStream, Integer> stream = PureStream.from(listOf(1, 2, 3)); - PureStream result = stream.append(IO.pure(4)); + PureStream, Integer> result = stream.append(IO.pure(4)); assertEquals(listOf(1, 2, 3, 4), run(result.asSequence())); } @Test public void prepend() { - PureStream stream = PureStream.from(listOf(1, 2, 3)); + PureStream, Integer> stream = PureStream.from(listOf(1, 2, 3)); - PureStream result = stream.prepend(IO.pure(0)); + PureStream, Integer> result = stream.prepend(IO.pure(0)); assertEquals(listOf(0, 1, 2, 3), run(result.asSequence())); } @Test public void take() { - PureStream stream = PureStream.from(listOf(1, 2, 3)); + PureStream, Integer> stream = PureStream.from(listOf(1, 2, 3)); - PureStream result = stream.take(2); + PureStream, Integer> result = stream.take(2); assertEquals(listOf(1, 2), run(result.asSequence())); } @Test public void drop() { - PureStream stream = PureStream.from(listOf(1, 2, 3)); + PureStream, Integer> stream = PureStream.from(listOf(1, 2, 3)); - PureStream result = stream.drop(2); + PureStream, Integer> result = stream.drop(2); assertEquals(listOf(3), run(result.asSequence())); } @Test public void takeWhile() { - PureStream stream = PureStream.from(listOf(1, 2, 3, 4, 5)); + PureStream, Integer> stream = PureStream.from(listOf(1, 2, 3, 4, 5)); - PureStream result = stream.takeWhile(t -> t < 4); + PureStream, Integer> result = stream.takeWhile(t -> t < 4); assertEquals(listOf(1, 2, 3), run(result.asSequence())); } @Test public void dropWhile() { - PureStream stream = PureStream.from(listOf(1, 2, 3, 4, 5)); + PureStream, Integer> stream = PureStream.from(listOf(1, 2, 3, 4, 5)); - PureStream result = stream.dropWhile(t -> t < 4); + PureStream, Integer> result = stream.dropWhile(t -> t < 4); assertEquals(listOf(4, 5), run(result.asSequence())); } @Test public void filter() { - PureStream stream = PureStream.from(java.util.stream.Stream.of(1, 2, 3, 4, 5)); + PureStream, Integer> stream = PureStream.from(java.util.stream.Stream.of(1, 2, 3, 4, 5)); - PureStream result = stream.filter(t -> (t % 2) == 0); + PureStream, Integer> result = stream.filter(t -> (t % 2) == 0); assertEquals(listOf(2, 4), run(result.asSequence())); } @Test public void collect() { - PureStream stream = PureStream.of(IO_.class).of(1, 2, 3, 4, 5); + PureStream, Integer> stream = PureStream.>of().of(1, 2, 3, 4, 5); - PureStream result = stream.collect(PartialFunction1.of(t -> (t % 2) == 0, x -> x * 2)); + PureStream, Integer> result = stream.collect(PartialFunction1.of(t -> (t % 2) == 0, x -> x * 2)); assertEquals(listOf(4, 8), run(result.asSequence())); } @Test public void iterate() { - PureStream stream = PureStream.iterate(0, i -> i + 1); + PureStream, Integer> stream = PureStream.iterate(0, i -> i + 1); - PureStream result = stream.takeWhile(t -> t < 4).dropWhile(t -> t < 2); + PureStream, Integer> result = stream.takeWhile(t -> t < 4).dropWhile(t -> t < 2); assertEquals(listOf(2, 3), run(result.asSequence())); } @Test public void repeat() { - PureStream stream = PureStream.of().of(1, 2, 3); + PureStream, Integer> stream = PureStream.>of().of(1, 2, 3); - PureStream result = stream.repeat().take(7); + PureStream, Integer> result = stream.repeat().take(7); assertEquals(listOf(1, 2, 3, 1, 2, 3, 1), run(result.asSequence())); } @Test public void intersperse() { - PureStream stream = PureStream.of().of(1, 2); + PureStream, Integer> stream = PureStream.>of().of(1, 2); - PureStream result = stream.intersperse(IO.pure(0)); + PureStream, Integer> result = stream.intersperse(IO.pure(0)); assertEquals(listOf(1, 0, 2, 0), run(result.asSequence())); } @Test public void zip() { - PureStream stream = PureStream.from(listOf("a", "b", "c")); + PureStream, String> stream = PureStream.from(listOf("a", "b", "c")); IO>> zip = PureStream.zipWithIndex(stream).asSequence().fix(toIO()); @@ -192,20 +187,20 @@ public void zip() { @Test public void merge() { - PureStream stream1 = PureStream.of().of(1, 2, 3); - PureStream stream2 = PureStream.of().of(4, 5, 6, 7); + PureStream, Integer> stream1 = PureStream.>of().of(1, 2, 3); + PureStream, Integer> stream2 = PureStream.>of().of(4, 5, 6, 7); - PureStream merge = PureStream.merge(stream1, stream2); + PureStream, Integer> merge = PureStream.merge(stream1, stream2); assertEquals(listOf(1, 4, 2, 5, 3, 6), run(merge.asSequence())); } @Test public void forAll() { - PureStream stream = PureStream.from(listOf("a", "b", "c")); + PureStream, String> stream = PureStream.from(listOf("a", "b", "c")); - Kind all = stream.exists(x -> x.toLowerCase().equals(x)); - Kind notAll = stream.exists(x -> x.toUpperCase().equals(x)); + Kind, Boolean> all = stream.exists(x -> x.toLowerCase().equals(x)); + Kind, Boolean> notAll = stream.exists(x -> x.toUpperCase().equals(x)); assertTrue(run(all)); assertFalse(run(notAll)); @@ -213,10 +208,10 @@ public void forAll() { @Test public void exists() { - PureStream stream = PureStream.from(listOf("a", "b", "c")); + PureStream, String> stream = PureStream.from(listOf("a", "b", "c")); - Kind exists = stream.exists(x -> "c".equals(x)); - Kind notExists = stream.exists(x -> "z".equals(x)); + Kind, Boolean> exists = stream.exists(x -> "c".equals(x)); + Kind, Boolean> notExists = stream.exists(x -> "z".equals(x)); assertTrue(run(exists)); assertFalse(run(notExists)); @@ -298,7 +293,7 @@ public void readFileAsync() { @Test public void test() { - PureStream stream = streamOfIO.from(listOf("a", "b", "c")).mapReplace(IO.pure(1)); + PureStream, Integer> stream = streamOfIO.from(listOf("a", "b", "c")).mapReplace(IO.pure(1)); assertEquals("111", stream.asString().fix(toIO()).unsafeRunSync()); } @@ -386,7 +381,7 @@ private BufferedReader reader(String file) { } } - private static T run(Kind effect) { + private static T run(Kind, T> effect) { return effect.fix(toIO()).unsafeRunSync(); } } diff --git a/transformer/src/main/java/com/github/tonivade/purefun/transformer/EitherT.java b/transformer/src/main/java/com/github/tonivade/purefun/transformer/EitherT.java index 42d8a3db1..dd895cbd1 100644 --- a/transformer/src/main/java/com/github/tonivade/purefun/transformer/EitherT.java +++ b/transformer/src/main/java/com/github/tonivade/purefun/transformer/EitherT.java @@ -22,7 +22,7 @@ import com.github.tonivade.purefun.typeclasses.Monad; @HigherKind -public non-sealed interface EitherT extends EitherTOf, Bindable, L>, R> { +public non-sealed interface EitherT extends EitherTOf, Bindable, F>, L>, R> { Monad monad(); Kind> value(); @@ -33,7 +33,7 @@ default EitherT map(Function1 map) { } @Override - default EitherT flatMap(Function1, L>, ? extends V>> map) { + default EitherT flatMap(Function1, F>, L>, ? extends V>> map) { return EitherT.of(monad(), flatMapF(v -> map.andThen(EitherTOf::narrowK).apply(v).value())); } diff --git a/transformer/src/main/java/com/github/tonivade/purefun/transformer/Kleisli.java b/transformer/src/main/java/com/github/tonivade/purefun/transformer/Kleisli.java index 3b5f84c03..fa3c0549f 100644 --- a/transformer/src/main/java/com/github/tonivade/purefun/transformer/Kleisli.java +++ b/transformer/src/main/java/com/github/tonivade/purefun/transformer/Kleisli.java @@ -14,7 +14,7 @@ import com.github.tonivade.purefun.typeclasses.Monad; @HigherKind -public non-sealed interface Kleisli extends KleisliOf, Bindable, Z>, A> { +public non-sealed interface Kleisli extends KleisliOf, Bindable, F>, Z>, A> { Monad monad(); Kind run(Z value); @@ -25,7 +25,7 @@ default Kleisli map(Function1 map) { } @Override - default Kleisli flatMap(Function1, Z>, ? extends R>> map) { + default Kleisli flatMap(Function1, F>, Z>, ? extends R>> map) { return Kleisli.of(monad(), value -> monad().flatMap(run(value), a -> map.andThen(KleisliOf::narrowK).apply(a).run(value))); } diff --git a/transformer/src/main/java/com/github/tonivade/purefun/transformer/OptionT.java b/transformer/src/main/java/com/github/tonivade/purefun/transformer/OptionT.java index 90964ae1f..edc5f6c3d 100644 --- a/transformer/src/main/java/com/github/tonivade/purefun/transformer/OptionT.java +++ b/transformer/src/main/java/com/github/tonivade/purefun/transformer/OptionT.java @@ -20,7 +20,7 @@ import com.github.tonivade.purefun.typeclasses.Monad; @HigherKind -public non-sealed interface OptionT extends OptionTOf, Bindable, T> { +public non-sealed interface OptionT extends OptionTOf, Bindable, F>, T> { Monad monad(); Kind> value(); @@ -31,7 +31,7 @@ default OptionT map(Function1 map) { } @Override - default OptionT flatMap(Function1, ? extends R>> map) { + default OptionT flatMap(Function1, F>, ? extends R>> map) { return OptionT.of(monad(), flatMapF(v -> map.andThen(OptionTOf::narrowK).apply(v).value())); } diff --git a/transformer/src/main/java/com/github/tonivade/purefun/transformer/StateT.java b/transformer/src/main/java/com/github/tonivade/purefun/transformer/StateT.java index a20713f9c..3da60ff32 100644 --- a/transformer/src/main/java/com/github/tonivade/purefun/transformer/StateT.java +++ b/transformer/src/main/java/com/github/tonivade/purefun/transformer/StateT.java @@ -21,7 +21,7 @@ import com.github.tonivade.purefun.typeclasses.Monad; @HigherKind -public non-sealed interface StateT extends StateTOf, Bindable, S>, A> { +public non-sealed interface StateT extends StateTOf, Bindable, F>, S>, A> { Monad monad(); Kind> run(S state); @@ -36,7 +36,7 @@ default StateT map(Function1 map) { } @Override - default StateT flatMap(Function1, S>, ? extends R>> map) { + default StateT flatMap(Function1, F>, S>, ? extends R>> map) { return state(monad(), state -> { Kind> newState = run(state); return monad().flatMap(newState, state2 -> map.andThen(StateTOf::narrowK).apply(state2.get2()).run(state2.get1())); diff --git a/transformer/src/main/java/com/github/tonivade/purefun/transformer/WriterT.java b/transformer/src/main/java/com/github/tonivade/purefun/transformer/WriterT.java index 1a0b66bdf..063094105 100644 --- a/transformer/src/main/java/com/github/tonivade/purefun/transformer/WriterT.java +++ b/transformer/src/main/java/com/github/tonivade/purefun/transformer/WriterT.java @@ -20,7 +20,7 @@ import com.github.tonivade.purefun.typeclasses.Monoid; @HigherKind -public non-sealed interface WriterT extends WriterTOf, Bindable, L>, A> { +public non-sealed interface WriterT extends WriterTOf, Bindable, F>, L>, A> { Monoid monoid(); Monad monad(); @@ -66,7 +66,7 @@ default WriterT mapK(Monad monadG, FunctionK functionK) { } @Override - default WriterT flatMap(Function1, L>, ? extends R>> mapper) { + default WriterT flatMap(Function1, F>, L>, ? extends R>> mapper) { return writer(monoid(), monad(), monad().flatMap(value(), current -> monad().map(mapper.andThen(WriterTOf::narrowK).apply(current.get2()).value(), diff --git a/transformer/src/test/java/com/github/tonivade/purefun/transformer/EitherTTest.java b/transformer/src/test/java/com/github/tonivade/purefun/transformer/EitherTTest.java index 5906128ac..de873c43d 100644 --- a/transformer/src/test/java/com/github/tonivade/purefun/transformer/EitherTTest.java +++ b/transformer/src/test/java/com/github/tonivade/purefun/transformer/EitherTTest.java @@ -10,60 +10,60 @@ import static com.github.tonivade.purefun.transformer.EitherTOf.toEitherT; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; + import com.github.tonivade.purefun.Kind; -import com.github.tonivade.purefun.concurrent.Future_; +import com.github.tonivade.purefun.concurrent.Future; import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.instances.EitherTInstances; import com.github.tonivade.purefun.instances.FutureInstances; import com.github.tonivade.purefun.instances.IOInstances; import com.github.tonivade.purefun.instances.IdInstances; import com.github.tonivade.purefun.instances.TryInstances; -import com.github.tonivade.purefun.monad.IO_; +import com.github.tonivade.purefun.monad.IO; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.type.TryOf; -import com.github.tonivade.purefun.type.Try_; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.MonadError; public class EitherTTest { - private final Monad monad = IdInstances.monad(); + private final Monad> monad = IdInstances.monad(); @Test public void map() { - EitherT right = EitherT.right(monad, "abc"); + EitherT, Void, String> right = EitherT.right(monad, "abc"); - EitherT map = right.map(String::toUpperCase); + EitherT, Void, String> map = right.map(String::toUpperCase); assertEquals(Id.of("ABC"), map.get()); } @Test public void flatMap() { - EitherT right = EitherT.right(monad, "abc"); + EitherT, Void, String> right = EitherT.right(monad, "abc"); - EitherT map = right.flatMap(value -> EitherT.right(monad, value.toUpperCase())); + EitherT, Void, String> map = right.flatMap(value -> EitherT.right(monad, value.toUpperCase())); assertEquals(Id.of("ABC"), map.get()); } @Test public void filterOrElse() { - EitherT right = EitherT.right(monad, "abc"); + EitherT, Void, String> right = EitherT.right(monad, "abc"); - EitherT filter = right.filterOrElse(String::isEmpty, cons(Either.right("not empty"))); - EitherT orElse = EitherT.right(monad, "not empty"); + EitherT, Void, String> filter = right.filterOrElse(String::isEmpty, cons(Either.right("not empty"))); + EitherT, Void, String> orElse = EitherT.right(monad, "not empty"); assertEquals(orElse.get(), filter.getOrElse("not empty")); } @Test public void left() { - EitherT left = EitherT.left(monad, unit()); + EitherT, Unit, String> left = EitherT.left(monad, unit()); assertAll( () -> assertEquals(Id.of(true), left.isLeft()), @@ -73,7 +73,7 @@ public void left() { @Test public void right() { - EitherT right = EitherT.right(monad, "abc"); + EitherT, Void, String> right = EitherT.right(monad, "abc"); assertAll( () -> assertEquals(Id.of(false), right.isLeft()), @@ -83,9 +83,9 @@ public void right() { @Test public void mapK() { - EitherT rightIo = EitherT.right(IOInstances.monad(), "abc"); + EitherT, Void, String> rightIo = EitherT.right(IOInstances.monad(), "abc"); - EitherT rightTry = rightIo.mapK(TryInstances.monad(), new IOToTryFunctionK()); + EitherT, Void, String> rightTry = rightIo.mapK(TryInstances.monad(), new IOToTryFunctionK()); assertEquals(Try.success("abc"), TryOf.narrowK(rightTry.get())); } @@ -93,16 +93,16 @@ public void mapK() { @Test public void monadErrorFuture() { RuntimeException error = new RuntimeException("error"); - MonadError, Throwable>, Throwable> monadError = + MonadError, Future>, Throwable>, Throwable> monadError = EitherTInstances.monadError(FutureInstances.monadError()); - Kind, Throwable>, String> pure = monadError.pure("is not ok"); - Kind, Throwable>, String> raiseError = monadError.raiseError(error); - Kind, Throwable>, String> handleError = + Kind, Future>, Throwable>, String> pure = monadError.pure("is not ok"); + Kind, Future>, Throwable>, String> raiseError = monadError.raiseError(error); + Kind, Future>, Throwable>, String> handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind, Throwable>, String> ensureOk = + Kind, Future>, Throwable>, String> ensureOk = monadError.ensure(pure, () -> error, value -> "is not ok".equals(value)); - Kind, Throwable>, String> ensureError = + Kind, Future>, Throwable>, String> ensureError = monadError.ensure(pure, () -> error, value -> "is ok?".equals(value)); assertAll( @@ -115,16 +115,16 @@ public void monadErrorFuture() { @Test public void monadErrorIO() { RuntimeException error = new RuntimeException("error"); - MonadError, Throwable>, Throwable> monadError = + MonadError, Id>, Throwable>, Throwable> monadError = EitherTInstances.monadError(monad); - Kind, Throwable>, String> pure = monadError.pure("is not ok"); - Kind, Throwable>, String> raiseError = monadError.raiseError(error); - Kind, Throwable>, String> handleError = + Kind, Id>, Throwable>, String> pure = monadError.pure("is not ok"); + Kind, Id>, Throwable>, String> raiseError = monadError.raiseError(error); + Kind, Id>, Throwable>, String> handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind, Throwable>, String> ensureOk = + Kind, Id>, Throwable>, String> ensureOk = monadError.ensure(pure, () -> error, "is not ok"::equals); - Kind, Throwable>, String> ensureError = + Kind, Id>, Throwable>, String> ensureError = monadError.ensure(pure, () -> error, "is ok?"::equals); assertAll( diff --git a/transformer/src/test/java/com/github/tonivade/purefun/transformer/KleisliTest.java b/transformer/src/test/java/com/github/tonivade/purefun/transformer/KleisliTest.java index 2ff97a567..0c394b52f 100644 --- a/transformer/src/test/java/com/github/tonivade/purefun/transformer/KleisliTest.java +++ b/transformer/src/test/java/com/github/tonivade/purefun/transformer/KleisliTest.java @@ -11,26 +11,25 @@ import com.github.tonivade.purefun.core.Tuple2; import com.github.tonivade.purefun.instances.TryInstances; import com.github.tonivade.purefun.type.Try; -import com.github.tonivade.purefun.type.Try_; public class KleisliTest { @Test public void compose() { - Kleisli toInt = Kleisli.lift(TryInstances.monad(), Integer::parseInt); - Kleisli half = Kleisli.lift(TryInstances.monad(), i -> i / 2.); + Kleisli, String, Integer> toInt = Kleisli.lift(TryInstances.monad(), Integer::parseInt); + Kleisli, Integer, Double> half = Kleisli.lift(TryInstances.monad(), i -> i / 2.); - Kind result = toInt.compose(half).run("123"); + Kind, Double> result = toInt.compose(half).run("123"); assertEquals(Try.success(61.5), result); } @Test public void flatMap() { - Kleisli toInt = Kleisli.lift(TryInstances.monad(), Integer::parseInt); - Kleisli toDouble = Kleisli.lift(TryInstances.monad(), Double::parseDouble); + Kleisli, String, Integer> toInt = Kleisli.lift(TryInstances.monad(), Integer::parseInt); + Kleisli, String, Double> toDouble = Kleisli.lift(TryInstances.monad(), Double::parseDouble); - Kleisli> flatMap = + Kleisli, String, Tuple2> flatMap = toInt.flatMap(integer -> toDouble.map(double_ -> Tuple.of(integer, double_))); assertEquals(Try.success(Tuple.of(123, 123.)), flatMap.run("123")); diff --git a/transformer/src/test/java/com/github/tonivade/purefun/transformer/OptionTTest.java b/transformer/src/test/java/com/github/tonivade/purefun/transformer/OptionTTest.java index 6e9285567..3eff089ff 100644 --- a/transformer/src/test/java/com/github/tonivade/purefun/transformer/OptionTTest.java +++ b/transformer/src/test/java/com/github/tonivade/purefun/transformer/OptionTTest.java @@ -14,7 +14,7 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.concurrent.FutureOf; -import com.github.tonivade.purefun.concurrent.Future_; +import com.github.tonivade.purefun.concurrent.Future; import com.github.tonivade.purefun.core.Eq; import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.instances.FutureInstances; @@ -22,52 +22,50 @@ import com.github.tonivade.purefun.instances.IdInstances; import com.github.tonivade.purefun.instances.OptionTInstances; import com.github.tonivade.purefun.instances.TryInstances; -import com.github.tonivade.purefun.monad.IO_; +import com.github.tonivade.purefun.monad.IO; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; import com.github.tonivade.purefun.type.Option; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.type.TryOf; -import com.github.tonivade.purefun.type.Try_; import com.github.tonivade.purefun.typeclasses.FunctionK; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.MonadError; public class OptionTTest { - private final Monad monad = IdInstances.monad(); + private final Monad> monad = IdInstances.monad(); @Test public void map() { - OptionT some = OptionT.some(monad, "abc"); + OptionT, String> some = OptionT.some(monad, "abc"); - OptionT map = some.map(String::toUpperCase); + OptionT, String> map = some.map(String::toUpperCase); assertEquals(Id.of("ABC"), map.getOrElseThrow()); } @Test public void flatMap() { - OptionT some = OptionT.some(monad, "abc"); + OptionT, String> some = OptionT.some(monad, "abc"); - OptionT map = some.flatMap(value -> OptionT.some(monad, value.toUpperCase())); + OptionT, String> map = some.flatMap(value -> OptionT.some(monad, value.toUpperCase())); assertEquals(Id.of("ABC"), map.getOrElseThrow()); } @Test public void filter() { - OptionT some = OptionT.some(monad, "abc"); + OptionT, String> some = OptionT.some(monad, "abc"); - OptionT filter = some.filter(String::isEmpty); - OptionT orElse = OptionT.some(monad, "not empty"); + OptionT, String> filter = some.filter(String::isEmpty); + OptionT, String> orElse = OptionT.some(monad, "not empty"); assertEquals(orElse.getOrElseThrow(), filter.getOrElse("not empty")); } @Test public void none() { - OptionT none = OptionT.none(monad); + OptionT, String> none = OptionT.none(monad); assertAll( () -> assertEquals(Id.of(true), none.isEmpty()), @@ -76,7 +74,7 @@ public void none() { @Test public void some() { - OptionT some = OptionT.some(monad, "abc"); + OptionT, String> some = OptionT.some(monad, "abc"); assertAll( () -> assertEquals(Id.of(false), some.isEmpty()), @@ -85,21 +83,21 @@ public void some() { @Test public void mapK() { - OptionT someIo = OptionT.some(IOInstances.monad(), "abc"); + OptionT, String> someIo = OptionT.some(IOInstances.monad(), "abc"); - OptionT someTry = someIo.mapK(TryInstances.monad(), new IOToTryFunctionK()); + OptionT, String> someTry = someIo.mapK(TryInstances.monad(), new IOToTryFunctionK()); assertEquals(Try.success("abc"), TryOf.narrowK(someTry.getOrElseThrow())); } @Test public void eq() { - OptionT some1 = OptionT.some(monad, "abc"); - OptionT some2 = OptionT.some(monad, "abc"); - OptionT none1 = OptionT.none(monad); - OptionT none2 = OptionT.none(monad); + OptionT, String> some1 = OptionT.some(monad, "abc"); + OptionT, String> some2 = OptionT.some(monad, "abc"); + OptionT, String> none1 = OptionT.none(monad); + OptionT, String> none2 = OptionT.none(monad); - Eq, String>> instance = OptionTInstances.eq(IdInstances.eq(Eq.any())); + Eq, Id>, String>> instance = OptionTInstances.eq(IdInstances.eq(Eq.any())); assertAll( () -> assertTrue(instance.eqv(some1, some2)), @@ -111,16 +109,16 @@ public void eq() { @Test public void monadErrorFuture() { RuntimeException error = new RuntimeException("error"); - MonadError, Throwable> monadError = + MonadError, Future>, Throwable> monadError = OptionTInstances.monadError(FutureInstances.monadError()); - Kind, String> pure = monadError.pure("is not ok"); - Kind, String> raiseError = monadError.raiseError(error); - Kind, String> handleError = + Kind, Future>, String> pure = monadError.pure("is not ok"); + Kind, Future>, String> raiseError = monadError.raiseError(error); + Kind, Future>, String> handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind, String> ensureOk = + Kind, Future>, String> ensureOk = monadError.ensure(pure, () -> error, value -> "is not ok".equals(value)); - Kind, String> ensureError = + Kind, Future>, String> ensureError = monadError.ensure(pure, () -> error, value -> "is ok?".equals(value)); assertAll( @@ -132,15 +130,15 @@ public void monadErrorFuture() { @Test public void monadErrorIO() { - MonadError, Unit> monadError = OptionTInstances.monadError(monad); + MonadError, Id>, Unit> monadError = OptionTInstances.monadError(monad); - Kind, String> pure = monadError.pure("is not ok"); - Kind, String> raiseError = monadError.raiseError(unit()); - Kind, String> handleError = + Kind, Id>, String> pure = monadError.pure("is not ok"); + Kind, Id>, String> raiseError = monadError.raiseError(unit()); + Kind, Id>, String> handleError = monadError.handleError(raiseError, e -> "not an error"); - Kind, String> ensureOk = + Kind, Id>, String> ensureOk = monadError.ensure(pure, Unit::unit, "is not ok"::equals); - Kind, String> ensureError = + Kind, Id>, String> ensureError = monadError.ensure(pure, Unit::unit, "is ok?"::equals); assertAll( @@ -151,10 +149,10 @@ public void monadErrorIO() { } } -class IOToTryFunctionK implements FunctionK { +class IOToTryFunctionK implements FunctionK, Try> { @Override - public Kind apply(Kind from) { + public Kind, T> apply(Kind, ? extends T> from) { return Try.of(from.fix(toIO())::unsafeRunSync); } } diff --git a/transformer/src/test/java/com/github/tonivade/purefun/transformer/StateTTest.java b/transformer/src/test/java/com/github/tonivade/purefun/transformer/StateTTest.java index 3cfa1763f..7d2c94537 100644 --- a/transformer/src/test/java/com/github/tonivade/purefun/transformer/StateTTest.java +++ b/transformer/src/test/java/com/github/tonivade/purefun/transformer/StateTTest.java @@ -6,7 +6,9 @@ import static com.github.tonivade.purefun.core.Unit.unit; import static com.github.tonivade.purefun.data.Sequence.listOf; +import static com.github.tonivade.purefun.monad.IOOf.toIO; import static org.junit.jupiter.api.Assertions.assertEquals; + import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.Kind; @@ -20,45 +22,42 @@ import com.github.tonivade.purefun.instances.TryInstances; import com.github.tonivade.purefun.monad.IO; import com.github.tonivade.purefun.monad.IOOf; -import static com.github.tonivade.purefun.monad.IOOf.toIO; -import com.github.tonivade.purefun.monad.IO_; import com.github.tonivade.purefun.type.Option; import com.github.tonivade.purefun.type.Try; import com.github.tonivade.purefun.type.TryOf; -import com.github.tonivade.purefun.type.Try_; import com.github.tonivade.purefun.typeclasses.Monad; public class StateTTest { - private Monad monad = IOInstances.monad(); + private Monad> monad = IOInstances.monad(); @Test public void get() { - Kind> run = StateT.get(monad).run("abc"); + Kind, Tuple2> run = StateT.get(monad).run("abc"); assertEquals(Tuple.of("abc", "abc"), run.fix(toIO()).unsafeRunSync()); } @Test public void set() { - Kind> run = StateT.set(monad, "abc").run("zzz"); + Kind, Tuple2> run = StateT.set(monad, "abc").run("zzz"); assertEquals(Tuple.of("abc", unit()), run.fix(toIO()).unsafeRunSync()); } @Test public void gets() { - Kind eval = StateT.inspect(monad, String::toUpperCase).eval("abc"); + Kind, String> eval = StateT., String, String>inspect(monad, String::toUpperCase).eval("abc"); assertEquals("ABC", eval.fix(toIO()).unsafeRunSync()); } @Test public void modify() { - Kind> run = StateT.modify(monad, String::toUpperCase).run("abc"); + Kind, Tuple2> run = StateT., String>modify(monad, String::toUpperCase).run("abc"); assertEquals(Tuple.of("ABC", unit()), run.fix(toIO()).unsafeRunSync()); } @Test public void flatMap() { - StateT, Unit> state = + StateT, ImmutableList, Unit> state = pure("a").flatMap(append("b")).flatMap(append("c")).flatMap(end()); IO, Unit>> result = IOOf.narrowK(state.run(ImmutableList.empty())); @@ -68,18 +67,18 @@ public void flatMap() { @Test public void traverse() { - StateT sa = StateT.pure(monad, "a"); - StateT sb = StateT.pure(monad, "b"); - StateT sc = StateT.pure(monad, "c"); + StateT, Unit, String> sa = StateT.pure(monad, "a"); + StateT, Unit, String> sb = StateT.pure(monad, "b"); + StateT, Unit, String> sc = StateT.pure(monad, "c"); - Kind>> result = StateT.traverse(monad, listOf(sa, sb, sc)).run(unit()); + Kind, Tuple2>> result = StateT.traverse(monad, listOf(sa, sb, sc)).run(unit()); assertEquals(Tuple.of(unit(), listOf("a", "b", "c")), IOOf.narrowK(result).unsafeRunSync()); } @Test public void run() { - StateT, Option> read = + StateT, ImmutableList, Option> read = StateT.lift(monad, state -> Tuple.of(state.tail(), state.head())); IO, Option>> result = IOOf.narrowK(read.run(listOf("a", "b", "c"))); @@ -89,22 +88,22 @@ public void run() { @Test public void mapK() { - StateT stateIo = StateT.pure(monad, "abc"); + StateT, Unit, String> stateIo = StateT.pure(monad, "abc"); - StateT stateTry = stateIo.mapK(TryInstances.monad(), new IOToTryFunctionK()); + StateT, Unit, String> stateTry = stateIo.mapK(TryInstances.monad(), new IOToTryFunctionK()); assertEquals(Try.success(Tuple2.of(unit(), "abc")), TryOf.narrowK(stateTry.run(unit()))); } - private StateT, String> pure(String value) { + private StateT, ImmutableList, String> pure(String value) { return StateT.pure(monad, value); } - private Function1, T>> append(T nextVal) { + private Function1, ImmutableList, T>> append(T nextVal) { return value -> StateT.lift(monad, state -> Tuple.of(state.append(value), nextVal)); } - private Function1, Unit>> end() { + private Function1, ImmutableList, Unit>> end() { return value -> StateT.lift(monad, state -> Tuple.of(state.append(value), unit())); } } diff --git a/transformer/src/test/java/com/github/tonivade/purefun/transformer/WriterTTest.java b/transformer/src/test/java/com/github/tonivade/purefun/transformer/WriterTTest.java index 2ee1bdf24..c9426518c 100644 --- a/transformer/src/test/java/com/github/tonivade/purefun/transformer/WriterTTest.java +++ b/transformer/src/test/java/com/github/tonivade/purefun/transformer/WriterTTest.java @@ -16,19 +16,18 @@ import com.github.tonivade.purefun.instances.IdInstances; import com.github.tonivade.purefun.instances.SequenceInstances; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; import com.github.tonivade.purefun.typeclasses.Monad; import com.github.tonivade.purefun.typeclasses.Monoid; public class WriterTTest { private final Monoid> monoid = SequenceInstances.monoid(); - private final Monad monad = IdInstances.monad(); + private final Monad> monad = IdInstances.monad(); @Test public void writerTest() { - WriterT, Integer> writer = - WriterT., Integer>pure(monoid, monad, 5) + WriterT, Sequence, Integer> writer = + WriterT., Sequence, Integer>pure(monoid, monad, 5) .flatMap(value -> lift(monoid, monad, Tuple.of(listOf("add 5"), value + 5))) .flatMap(value -> lift(monoid, monad, Tuple.of(listOf("plus 2"), value * 2))); diff --git a/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Instance.java b/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Instance.java index 808ade08e..f5c80c8da 100644 --- a/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Instance.java +++ b/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Instance.java @@ -12,16 +12,17 @@ import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; import java.util.Arrays; import com.github.tonivade.purefun.type.Try; public abstract class Instance { - + private final Class kindType; private final Type type; - + protected Instance(Class clazz) { this.kindType = clazz; this.type = clazz; @@ -118,9 +119,9 @@ public Traverse traverse(Object...params) { } protected String instanceName() { - return "com.github.tonivade.purefun.instances." + kindType.getSimpleName().replace("_", "Instances"); + return "com.github.tonivade.purefun.instances." + kindType.getSimpleName() + "Instances"; } - + private static Type genericType(Type type) { if (type instanceof ParameterizedType parameterizedType) { return parameterizedType.getActualTypeArguments()[0]; @@ -128,12 +129,14 @@ private static Type genericType(Type type) { throw new UnsupportedOperationException("not supported " + type.getTypeName()); } - @SuppressWarnings("unchecked") private static Class kindType(Type type) { if (type instanceof ParameterizedType parameterizedType) { if (parameterizedType.getActualTypeArguments()[0] instanceof ParameterizedType) { return kindType(parameterizedType.getActualTypeArguments()[0]); } + if (parameterizedType.getActualTypeArguments()[0] instanceof WildcardType) { + return (Class) parameterizedType.getRawType(); + } if (parameterizedType.getActualTypeArguments()[0] instanceof Class) { return (Class) parameterizedType.getActualTypeArguments()[0]; } @@ -154,7 +157,7 @@ private static Class findClass(Instance instance) return Class.forName(instance.instanceName()); } - private static Method findMethod(Class instanceClass, Class typeClass, Object... args) + private static Method findMethod(Class instanceClass, Class typeClass, Object... args) throws NoSuchMethodException { String simpleName = typeClass.getSimpleName(); String methodName = toLowerCase(simpleName.charAt(0)) + simpleName.substring(1); @@ -167,7 +170,7 @@ private static Method findMethod(Class instanceClass, Class typeClass, Obj } @SuppressWarnings("unchecked") - private static T getInstance(Method method, Object...args) + private static T getInstance(Method method, Object...args) throws IllegalAccessException, InvocationTargetException { return (T) method.invoke(null, args); } diff --git a/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Monoid.java b/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Monoid.java index f19c34565..6badaab29 100644 --- a/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Monoid.java +++ b/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Monoid.java @@ -44,12 +44,12 @@ public T combine(T t1, T t2) { } } -interface MonoidInvariant extends Invariant { +interface MonoidInvariant extends Invariant> { MonoidInvariant INSTANCE = new MonoidInvariant() { }; @Override - default Kind imap(Kind value, + default Kind, B> imap(Kind, ? extends A> value, Function1 map, Function1 comap) { return new Monoid<>() { diff --git a/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Resource.java b/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Resource.java index af3109e83..6f44e93f9 100644 --- a/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Resource.java +++ b/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Resource.java @@ -30,7 +30,7 @@ public Resource map(Function1 mapper) { return flatMap(t -> pure(monad, mapper.andThen(monad::pure).apply(t))); } - public Resource flatMap(Function1, ? extends R>> mapper) { + public Resource flatMap(Function1, F>, ? extends R>> mapper) { return new Resource<>(monad, monad.flatMap(resource, t -> monad.map(mapper.andThen(ResourceOf::narrowK).apply(t.get1()).resource, r -> Tuple.of(r.get1(), (Consumer1) ignore -> releaseAndThen(t, r))))); diff --git a/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Traverse.java b/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Traverse.java index faeff408e..2e755f2ef 100644 --- a/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Traverse.java +++ b/typeclasses/src/main/java/com/github/tonivade/purefun/typeclasses/Traverse.java @@ -11,7 +11,6 @@ import com.github.tonivade.purefun.core.Function1; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; public interface Traverse extends Functor, Foldable { @@ -25,7 +24,7 @@ default Kind> sequence(Applicative applicative, @Override default Kind map(Kind value, Function1 map) { - Kind> traverse = traverse(Instances.applicative(), value, t -> Id.of(map.apply(t))); + Kind, Kind> traverse = traverse(Instances.>applicative(), value, t -> Id.of(map.apply(t))); return traverse.fix(toId()).value(); } diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ConcurrentTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ConcurrentTest.java index 05956a264..e1a64ba6d 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ConcurrentTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ConcurrentTest.java @@ -14,9 +14,7 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.effect.PureIO; -import com.github.tonivade.purefun.effect.PureIO_; import com.github.tonivade.purefun.monad.IO; -import com.github.tonivade.purefun.monad.IO_; import com.github.tonivade.purefun.type.Either; public class ConcurrentTest { @@ -25,9 +23,9 @@ public class ConcurrentTest { @Test public void ioRaceA() { - Concurrent concurrent = Instances.concurrent(); + Concurrent> concurrent = Instances.>concurrent(); - Kind> race = concurrent.race( + Kind, Either> race = concurrent.race( IO.delay(Duration.ofMillis(10), () -> 10), IO.delay(Duration.ofMillis(100), () -> "b")); @@ -38,9 +36,9 @@ public void ioRaceA() { @Test public void ioRaceB() { - Concurrent concurrent = Instances.concurrent(); + Concurrent> concurrent = Instances.>concurrent(); - Kind> race = concurrent.race( + Kind, Either> race = concurrent.race( IO.delay(Duration.ofMillis(100), () -> 10), IO.delay(Duration.ofMillis(10), () -> "b")); @@ -51,10 +49,10 @@ public void ioRaceB() { @Test public void PureIORaceA() { - Concurrent, Throwable>> concurrent = - new Instance, Throwable>>() { }.concurrent(); + Concurrent, Void>, Throwable>> concurrent = + new Instance, Void>, Throwable>>() { }.concurrent(); - Kind, Throwable>, Either> race = concurrent.race( + Kind, Void>, Throwable>, Either> race = concurrent.race( PureIO.sleep(Duration.ofMillis(10)).andThen(PureIO.task(() -> 10)), PureIO.sleep(Duration.ofMillis(100)).andThen(PureIO.task(() -> "b"))); @@ -66,10 +64,10 @@ public void PureIORaceA() { @Test public void PureIORaceB() { - Concurrent, Throwable>> concurrent = - new Instance, Throwable>>() { }.concurrent(); + Concurrent, Void>, Throwable>> concurrent = + new Instance, Void>, Throwable>>() { }.concurrent(); - Kind, Throwable>, Either> race = concurrent.race( + Kind, Void>, Throwable>, Either> race = concurrent.race( PureIO.sleep(Duration.ofMillis(100)).andThen(PureIO.task(() -> 10)), PureIO.sleep(Duration.ofMillis(10)).andThen(PureIO.task(() -> "b"))); diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/EqTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/EqTest.java index 73e905ba1..c6c01fcc7 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/EqTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/EqTest.java @@ -8,21 +8,21 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; + import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.core.Eq; -import com.github.tonivade.purefun.data.Sequence_; +import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.instances.EitherInstances; import com.github.tonivade.purefun.instances.SequenceInstances; import com.github.tonivade.purefun.type.Either; -import com.github.tonivade.purefun.type.Either_; public class EqTest { @Test public void sequence() { - Eq> instance = SequenceInstances.eq(Eq.any()); + Eq, Integer>> instance = SequenceInstances.eq(Eq.any()); assertAll( () -> assertTrue(instance.eqv(listOf(1, 2, 3), listOf(1, 2, 3))), @@ -37,7 +37,7 @@ public void either() { Either right1 = Either.right("hola"); Either right2 = Either.right("hola"); - Eq, String>> instance = EitherInstances.eq(Eq.any(), Eq.any()); + Eq, Integer>, String>> instance = EitherInstances.eq(Eq.any(), Eq.any()); assertAll( () -> assertTrue(instance.eqv(left1, left2)), diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ForTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ForTest.java index ddb4d323e..cba094ffe 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ForTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ForTest.java @@ -22,14 +22,12 @@ import com.github.tonivade.purefun.core.Producer; import com.github.tonivade.purefun.core.Tuple; import com.github.tonivade.purefun.monad.IO; -import com.github.tonivade.purefun.monad.IO_; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; @ExtendWith(MockitoExtension.class) public class ForTest { - final Monad monad = Instances.monad(); + final Monad> monad = Instances.>monad(); @Test public void map() { @@ -57,9 +55,9 @@ public void returns(@Mock Function1 mapper) { @Test public void flatMap() { - Id result = For.with(Instances.monad()) - .andThen(() -> Instances.monad().pure("value")) - .flatMap(string -> Instances.monad().pure(string.toUpperCase())) + Id result = For.with(Instances.>monad()) + .andThen(() -> Instances.>monad().pure("value")) + .flatMap(string -> Instances.>monad().pure(string.toUpperCase())) .fix(toId()); assertEquals(Id.of("VALUE"), result); @@ -70,7 +68,7 @@ public void applyBug(@Mock Producer task1, @Mock Producer task2) when(task1.get()).thenReturn("hola toni"); when(task2.get()).thenReturn("adios toni"); - Applicative monad = Instances.applicative(IO_.class); + Applicative> monad = Instances.>applicative(); var result = For.with(monad) .then(IO.task(task1)) .then(IO.task(task2)) @@ -99,13 +97,13 @@ public void apply() { @Test public void applyVsYield() { - var program1 = For.with(Instances.monad()) + var program1 = For.with(Instances.>monad()) .and(1) .and(2) .and(3) .and(4) .and(5); - var program2 = For.with(Instances.applicative()) + var program2 = For.with(Instances.>applicative()) .and(1) .and(2) .and(3) diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/IOReferenceTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/IOReferenceTest.java index 32e7cc5d8..e2f8a8cb8 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/IOReferenceTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/IOReferenceTest.java @@ -4,11 +4,11 @@ */ package com.github.tonivade.purefun.typeclasses; -import com.github.tonivade.purefun.monad.IO_; +import com.github.tonivade.purefun.monad.IO; -public class IOReferenceTest extends ReferenceTest { +public class IOReferenceTest extends ReferenceTest> { public IOReferenceTest() { - super(IO_.class); + super(new Instance>() {}); } } diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/IOResourceTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/IOResourceTest.java index 378449bd4..f7181f814 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/IOResourceTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/IOResourceTest.java @@ -4,11 +4,11 @@ */ package com.github.tonivade.purefun.typeclasses; -import com.github.tonivade.purefun.monad.IO_; +import com.github.tonivade.purefun.monad.IO; -public class IOResourceTest extends ResourceTest { +public class IOResourceTest extends ResourceTest> { public IOResourceTest() { - super(IO_.class); + super(new Instance>() {}); } } \ No newline at end of file diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/InstanceTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/InstanceTest.java index 74365177e..beeb07bff 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/InstanceTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/InstanceTest.java @@ -15,24 +15,21 @@ import com.github.tonivade.purefun.Kind; import com.github.tonivade.purefun.effect.PureIO; -import com.github.tonivade.purefun.effect.PureIO_; import com.github.tonivade.purefun.type.Either; -import com.github.tonivade.purefun.type.Either_; import com.github.tonivade.purefun.type.Id; -import com.github.tonivade.purefun.type.Id_; public class InstanceTest { @Test public void testSimple() { - Id result = Instances.functor().map(Id.of(1), x -> x + 1).fix(toId()); + Id result = Instances.>functor().map(Id.of(1), x -> x + 1).fix(toId()); assertEquals(Id.of(2), result); } @Test public void testComplex() { - Instance> instance = new Instance>(){}; + Instance, String>> instance = new Instance, String>>(){}; Either result = instance.functor().map(Either.right(1), x -> x + 1).fix(toEither()); @@ -41,7 +38,7 @@ public void testComplex() { @Test public void testPureIO() { - Instance, String>> instance = new Instance, String>>(){}; + Instance, Void>, String>> instance = new Instance, Void>, String>>(){}; PureIO result = instance.functor().map(PureIO.pure(1), x -> x + 1).fix(toPureIO()); @@ -50,18 +47,18 @@ public void testPureIO() { @Test public void notFoundSimple() { - InstanceNotFoundException exception = assertThrows(InstanceNotFoundException.class, () -> Instances.monadError()); + InstanceNotFoundException exception = assertThrows(InstanceNotFoundException.class, () -> Instances., String>monadError()); - assertEquals("instance of type MonadError for type com.github.tonivade.purefun.type.Id_ not found", exception.getMessage()); + assertEquals("instance of type MonadError for type com.github.tonivade.purefun.type.Id not found", exception.getMessage()); } @Test public void notFoundComplex() { - Instance> instance = new Instance>(){}; + Instance, String>> instance = new Instance, String>>(){}; InstanceNotFoundException exception = assertThrows(InstanceNotFoundException.class, () -> instance.monadDefer()); - assertEquals("instance of type MonadDefer for type com.github.tonivade.purefun.Kind not found", + assertEquals("instance of type MonadDefer for type com.github.tonivade.purefun.Kind, java.lang.String> not found", exception.getMessage()); } diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/MonadStateTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/MonadStateTest.java index 8265d2a95..d917e2944 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/MonadStateTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/MonadStateTest.java @@ -12,12 +12,11 @@ import com.github.tonivade.purefun.data.ImmutableArray; import com.github.tonivade.purefun.monad.IO; -import com.github.tonivade.purefun.monad.IO_; public class MonadStateTest { - private MonadState> monadState = - MonadState.from(Instances.monadDefer(), ImmutableArray.empty()); + private MonadState, ImmutableArray> monadState = + MonadState.from(Instances.>monadDefer(), ImmutableArray.empty()); @Test public void program() { diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ReferenceTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ReferenceTest.java index 6bf6a8e5a..0a87e2930 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ReferenceTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ReferenceTest.java @@ -10,23 +10,23 @@ public abstract class ReferenceTest { - - private final Class type; - - public ReferenceTest(Class type) { - this.type = type; + + private final Instance instance; + + public ReferenceTest(Instance instance) { + this.instance = instance; } protected Reference makeRef(T value) { - return Instances.monadDefer(type).ref(value); + return instance.monadDefer().ref(value); } - + protected Kind doAndThen(Kind value, Kind next) { - return Instances.monad(type).andThen(value, () -> next); + return instance.monad().andThen(value, () -> next); } - + protected T run(Kind value) { - return Instances.runtime(type).run(value); + return instance.runtime().run(value); } @Test diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ResourceTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ResourceTest.java index 641804bb1..dcad9c18c 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ResourceTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ResourceTest.java @@ -27,67 +27,67 @@ @ExtendWith(MockitoExtension.class) public abstract class ResourceTest { - - private final Class type; - - protected ResourceTest(Class type) { - this.type = type; + + private final Instance instance; + + protected ResourceTest(Instance instance) { + this.instance = instance; } - + protected Resource makeResource(Kind acquire) { return makeResource(acquire, AutoCloseable::close); } protected Resource makeResource(Kind acquire, Consumer1 release) { - return Instances.monadDefer(type).resource(acquire, release); + return instance.monadDefer().resource(acquire, release); } - + protected T run(Kind result) { - return Instances.runtime(type).run(result); + return instance.runtime().run(result); } - + protected Kind pure(T value) { - return Instances.monadDefer(type).pure(value); + return instance.monadDefer().pure(value); } protected Kind later(Producer value) { - return Instances.monadDefer(type).later(value); + return instance.monadDefer().later(value); } @Test public void use(@Mock Consumer1 release) { Resource resource = makeResource(pure("hola"), release); - + Kind use = resource.use(string -> pure(string.toUpperCase())); - + assertEquals("HOLA", run(use)); verify(release).accept("hola"); } - + @Test public void map(@Mock Consumer1 release) { Resource resource = makeResource(pure("hola"), release).map(String::toUpperCase); - + Kind use = resource.use(string -> pure(string.length())); - + assertEquals(4, run(use)); verify(release).accept("hola"); } - + @Test - public void flatMap(@Mock DataSource dataSource, @Mock Connection connection, + public void flatMap(@Mock DataSource dataSource, @Mock Connection connection, @Mock PreparedStatement statement, @Mock ResultSet resultSet) throws SQLException { when(dataSource.getConnection()).thenReturn(connection); when(connection.prepareStatement("sql")).thenReturn(statement); when(statement.executeQuery()).thenReturn(resultSet); when(resultSet.getString(0)).thenReturn("result"); - + Resource flatMap = makeResource(later(dataSource::getConnection)) .flatMap(conn -> makeResource(later(() -> conn.prepareStatement("sql")))) .flatMap(stmt -> makeResource(later(() -> stmt.executeQuery()))); - + Kind use = flatMap.use(rs -> later(() -> rs.getString(0))); - + assertEquals("result", run(use)); InOrder inOrder = inOrder(resultSet, statement, connection); inOrder.verify(resultSet).close(); @@ -99,9 +99,9 @@ public void flatMap(@Mock DataSource dataSource, @Mock Connection connection, public void combine(@Mock Consumer1 release1, @Mock Consumer1 release2) { Resource res1 = makeResource(pure("hola"), release1); Resource res2 = makeResource(pure(5), release2); - + Resource> combine = res1.combine(res2); - + Kind use = combine.use(tuple -> later(tuple::toString)); assertEquals("Tuple2(hola, 5)", run(use)); diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ScheduleTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ScheduleTest.java index 8fe663f27..6328fc663 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ScheduleTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/ScheduleTest.java @@ -26,19 +26,17 @@ import com.github.tonivade.purefun.data.Sequence; import com.github.tonivade.purefun.effect.UIO; import com.github.tonivade.purefun.effect.UIOOf; -import com.github.tonivade.purefun.effect.UIO_; import com.github.tonivade.purefun.monad.IO; -import com.github.tonivade.purefun.monad.IO_; @ExtendWith(MockitoExtension.class) public class ScheduleTest { - private final MonadError monadError = Instances.monadError(); + private final MonadError, Throwable> monadError = Instances., Throwable>monadError(); @Test public void repeat(@Mock Consumer1 console) { IO print = IO.exec(() -> console.accept("hola")); - Schedule schedule = Schedule.recurs(2).zipRight(Schedule.identity()); + Schedule, Unit, Unit> schedule = Schedule., Unit>recurs(2).zipRight(Schedule.identity()); IO repeat = monadError.repeat(print, schedule).fix(toIO()); @@ -51,7 +49,7 @@ public void repeat(@Mock Consumer1 console) { @Test public void repeatStackSafeIO(@Mock Consumer1 console) { IO print = IO.exec(() -> console.accept("hola")); - Schedule schedule = Schedule.recurs(10000).zipRight(Schedule.identity()); + Schedule, Unit, Unit> schedule = Schedule., Unit>recurs(10000).zipRight(Schedule.identity()); IO repeat = monadError.repeat(print, schedule).fix(toIO()); @@ -64,9 +62,9 @@ public void repeatStackSafeIO(@Mock Consumer1 console) { @Test public void repeatStackSafeUIO(@Mock Consumer1 console) { UIO print = UIO.exec(() -> console.accept("hola")); - Schedule schedule = Schedule.recurs(10000).zipRight(Schedule.identity()); + Schedule, Unit, Unit> schedule = Schedule., Unit>recurs(10000).zipRight(Schedule.identity()); - UIO repeat = Instances.monadError().repeat(print, schedule).fix(UIOOf.toUIO()); + UIO repeat = Instances., Throwable>monadError().repeat(print, schedule).fix(UIOOf.toUIO()); Unit result = repeat.unsafeRunSync(); @@ -77,9 +75,9 @@ public void repeatStackSafeUIO(@Mock Consumer1 console) { @Test public void repeatDelay(@Mock Consumer1 console) { IO print = IO.exec(() -> console.accept("hola")); - Schedule recurs = Schedule.recurs(2).zipRight(Schedule.identity()); - Schedule spaced = Schedule.spaced(Duration.ofMillis(500)); - Schedule schedule = recurs.zipLeft(spaced); + Schedule, Unit, Unit> recurs = Schedule., Unit>recurs(2).zipRight(Schedule.identity()); + Schedule, Unit, Integer> spaced = Schedule.spaced(Duration.ofMillis(500)); + Schedule, Unit, Unit> schedule = recurs.zipLeft(spaced); IO repeat = monadError.repeat(print, schedule).fix(toIO()); IO> timed = repeat.timed(); @@ -121,8 +119,8 @@ public void retryDelay(@Mock Producer console) { when(console.get()).thenThrow(RuntimeException.class).thenReturn("hola"); IO read = IO.task(console::get); - Schedule recurs = Schedule.recurs(2); - Schedule spaced = Schedule.spaced(Duration.ofMillis(500)); + Schedule, Throwable, Integer> recurs = Schedule.recurs(2); + Schedule, Throwable, Integer> spaced = Schedule.spaced(Duration.ofMillis(500)); IO> retry = monadError.retry(read, recurs.zip(spaced)).fix(toIO()).timed(); Tuple2 result = retry.unsafeRunSync(); @@ -144,8 +142,8 @@ public void noRetry(@Mock Producer console) { @Test public void andThen(@Mock Consumer1 console) { - Schedule two = - Schedule.recurs(1).andThen(Schedule.recurs(1)); + Schedule, Unit, Integer> two = + Schedule., Unit>recurs(1).andThen(Schedule., Unit>recurs(1)); IO print = IO.exec(() -> console.accept("hola")); IO repeat = monadError.repeat(print, two).fix(toIO()); @@ -158,8 +156,8 @@ public void andThen(@Mock Consumer1 console) { @Test public void compose(@Mock Consumer1 console) { - Schedule two = - Schedule.recurs(2).compose(Schedule.recurs(2)); + Schedule, Unit, Integer> two = + Schedule., Unit>recurs(2).compose(Schedule., Integer>recurs(2)); IO print = IO.exec(() -> console.accept("hola")); IO repeat = monadError.repeat(print, two).fix(toIO()); @@ -174,7 +172,7 @@ public void compose(@Mock Consumer1 console) { public void collect() { IO pure = IO.unit(); - Schedule> schedule = Schedule.recurs(5).collectAll().zipLeft(Schedule.identity()); + Schedule, Unit, Sequence> schedule = Schedule., Unit>recurs(5).collectAll().zipLeft(Schedule.identity()); IO> repeat = monadError.repeat(pure, schedule).fix(toIO()); Sequence result = repeat.unsafeRunSync(); diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/SelectiveTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/SelectiveTest.java index 1e38556bb..3af36d902 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/SelectiveTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/SelectiveTest.java @@ -24,23 +24,20 @@ import com.github.tonivade.purefun.core.Producer; import com.github.tonivade.purefun.core.Unit; import com.github.tonivade.purefun.data.Sequence; -import com.github.tonivade.purefun.data.Sequence_; import com.github.tonivade.purefun.instances.SequenceInstances; import com.github.tonivade.purefun.instances.ValidationInstances; import com.github.tonivade.purefun.monad.IO; -import com.github.tonivade.purefun.monad.IO_; import com.github.tonivade.purefun.type.Either; import com.github.tonivade.purefun.type.Validation; -import com.github.tonivade.purefun.type.Validation_; @ExtendWith(MockitoExtension.class) class SelectiveTest { - private final Selective monad = Instances.monad(); + private final Selective> monad = Instances.>monad(); @Test void apply() { - Selective>> selective = + Selective, Sequence>> selective = ValidationInstances.selective(SequenceInstances.semigroup()); var validValue = Validation., Integer>valid(1); @@ -140,9 +137,9 @@ void orS() { @Test void allS() { - var match = monad.allS( + var match = monad., String>allS( listOf("a", "b", "c"), a -> monad.pure(a.length() == 1)); - var notMatch = monad.allS( + var notMatch = monad., String>allS( listOf("a", "b", "cd"), a -> monad.pure(a.length() == 1)); assertAll( @@ -153,9 +150,9 @@ void allS() { @Test void anyS() { - var match = monad.anyS( + var match = monad., String>anyS( listOf("a", "b", "cd"), a -> monad.pure(a.length() > 1)); - var notMatch = monad.anyS( + var notMatch = monad., String>anyS( listOf("a", "b", "c"), a -> monad.pure(a.length() > 1)); assertAll( diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TaskResourceTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TaskResourceTest.java index 8d387e021..5e2adb994 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TaskResourceTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TaskResourceTest.java @@ -4,11 +4,11 @@ */ package com.github.tonivade.purefun.typeclasses; -import com.github.tonivade.purefun.effect.Task_; +import com.github.tonivade.purefun.effect.Task; -public class TaskResourceTest extends ResourceTest { +public class TaskResourceTest extends ResourceTest> { public TaskResourceTest() { - super(Task_.class); + super(new Instance>() { }); } } \ No newline at end of file diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK1Test.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK1Test.java index 25f4702f1..d7f347996 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK1Test.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK1Test.java @@ -15,14 +15,14 @@ import org.mockito.junit.jupiter.MockitoExtension; import com.github.tonivade.purefun.core.Consumer1; -import com.github.tonivade.purefun.type.Option_; +import com.github.tonivade.purefun.type.Option; @ExtendWith(MockitoExtension.class) public class TupleK1Test { - + @Test public void tuple() { - TupleK1 tuple = TupleK.of(some("value")); + TupleK1, String> tuple = TupleK.of(some("value")); assertAll( () -> assertEquals(TupleK.of(some("value")), tuple), @@ -34,7 +34,7 @@ public void tuple() { @Test public void forEach(@Mock Consumer1 callback) { - TupleK1 tuple = TupleK.of(some("value")); + TupleK1, String> tuple = TupleK.of(some("value")); tuple.forEach(callback); diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK2Test.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK2Test.java index c3b440d5f..9b061adcf 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK2Test.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK2Test.java @@ -10,13 +10,13 @@ import org.junit.jupiter.api.Test; -import com.github.tonivade.purefun.type.Option_; +import com.github.tonivade.purefun.type.Option; public class TupleK2Test { - + @Test public void tuple() { - TupleK2 tuple = TupleK.of(some("value"), some(10)); + TupleK2, String, Integer> tuple = TupleK.of(some("value"), some(10)); assertAll(() -> assertEquals(TupleK.of(some("value"), some(10)), tuple), () -> assertEquals(TupleK.of(some("VALUE"), some(10)), tuple.map1(String::toUpperCase)), diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK3Test.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK3Test.java index b6f8338de..d29dd95d2 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK3Test.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK3Test.java @@ -12,14 +12,14 @@ import org.junit.jupiter.api.Test; -import com.github.tonivade.purefun.type.Option_; +import com.github.tonivade.purefun.type.Option; public class TupleK3Test { @Test public void tuple() { - TupleK3 tuple = TupleK.of(some("value"), some(10), some(LocalDate.of(2018, 11, 5))); + TupleK3, String, Integer, LocalDate> tuple = TupleK.of(some("value"), some(10), some(LocalDate.of(2018, 11, 5))); assertAll(() -> assertEquals(TupleK.of(some("value"), some(10), some(LocalDate.of(2018, 11, 5))), tuple), () -> assertEquals(TupleK.of(some("VALUE"), some(10), some(LocalDate.of(2018, 11, 5))), tuple.map1(String::toUpperCase)), diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK4Test.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK4Test.java index 7dfa8815a..e5cf4a487 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK4Test.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK4Test.java @@ -14,13 +14,13 @@ import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.core.Unit; -import com.github.tonivade.purefun.type.Option_; +import com.github.tonivade.purefun.type.Option; public class TupleK4Test { @Test public void tuple() { - TupleK4 tuple = + TupleK4, String, Integer, LocalDate, Unit> tuple = TupleK.of(some("value"), some(10), some(LocalDate.of(2018, 11, 5)), none()); assertAll(() -> assertEquals(TupleK.of(some("value"), some(10), some(LocalDate.of(2018, 11, 5)), none()), tuple), diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK5Test.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK5Test.java index c9dfc4136..fc324bbb7 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK5Test.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/TupleK5Test.java @@ -14,13 +14,13 @@ import org.junit.jupiter.api.Test; import com.github.tonivade.purefun.core.Unit; -import com.github.tonivade.purefun.type.Option_; +import com.github.tonivade.purefun.type.Option; public class TupleK5Test { @Test public void tuple() { - TupleK5 tuple = + TupleK5, String, Integer, LocalDate, Unit, Double> tuple = TupleK.of(some("value"), some(10), some(LocalDate.of(2018, 11, 5)), none(), some(1.0)); assertAll(() -> assertEquals(TupleK.of(some("value"), some(10), some(LocalDate.of(2018, 11, 5)), none(), some(1.0)), tuple), diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/UIOReferenceTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/UIOReferenceTest.java index 73005647c..5b912779a 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/UIOReferenceTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/UIOReferenceTest.java @@ -4,11 +4,11 @@ */ package com.github.tonivade.purefun.typeclasses; -import com.github.tonivade.purefun.effect.UIO_; +import com.github.tonivade.purefun.effect.UIO; -public class UIOReferenceTest extends ReferenceTest { +public class UIOReferenceTest extends ReferenceTest> { public UIOReferenceTest() { - super(UIO_.class); + super(new Instance>() {}); } } diff --git a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/UIOResourceTest.java b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/UIOResourceTest.java index 5d6a39aea..cd935b4b3 100644 --- a/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/UIOResourceTest.java +++ b/typeclasses/src/test/java/com/github/tonivade/purefun/typeclasses/UIOResourceTest.java @@ -4,11 +4,11 @@ */ package com.github.tonivade.purefun.typeclasses; -import com.github.tonivade.purefun.effect.UIO_; +import com.github.tonivade.purefun.effect.UIO; -public class UIOResourceTest extends ResourceTest { +public class UIOResourceTest extends ResourceTest> { public UIOResourceTest() { - super(UIO_.class); + super(new Instance>() {}); } } \ No newline at end of file