Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/fix #146

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
*/
package com.github.tonivade.purefun;

import java.util.function.Function;

public interface Kind<F extends Kind<F, ?>, A> {

default <R extends Kind<F, ?>> R fix(Function<? super Kind<F, ? extends A>, ? extends R> fixer) {
return fixer.apply(this);
@SuppressWarnings("unchecked")
default <R> R fix() {
return (R) this;
}

default Kind<F, A> kind() {
Expand Down
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ dependencies {
jacocoAggregation projects.purefunInstances
jacocoAggregation projects.purefunMonad
jacocoAggregation projects.purefunOptics
jacocoAggregation projects.purefunProcessor
jacocoAggregation projects.purefunStream
jacocoAggregation projects.purefunTransformer
jacocoAggregation projects.purefunTypeclasses
Expand All @@ -31,7 +30,6 @@ dependencies {
testReportAggregation projects.purefunInstances
testReportAggregation projects.purefunMonad
testReportAggregation projects.purefunOptics
testReportAggregation projects.purefunProcessor
testReportAggregation projects.purefunStream
testReportAggregation projects.purefunTransformer
testReportAggregation projects.purefunTypeclasses
Expand Down
1 change: 0 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

dependencies {
annotationProcessor projects.purefunProcessor
api projects.purefunAnnotation
api libs.pcollections
testImplementation libs.assertj
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.core.Bindable;
import com.github.tonivade.purefun.core.CheckedRunnable;
Expand All @@ -32,7 +31,6 @@
import com.github.tonivade.purefun.data.ImmutableList;
import com.github.tonivade.purefun.data.Sequence;
import com.github.tonivade.purefun.type.Try;
import com.github.tonivade.purefun.type.TryOf;

/**
* <p>This type is an abstraction of a computation executed in another thread. To run the computation an {@code Executor}
Expand Down Expand Up @@ -66,8 +64,7 @@
* @see Try
* @see Promise
*/
@HigherKind
public sealed interface Future<T> extends FutureOf<T>, Bindable<Future<?>, T> {
public sealed interface Future<T> extends Kind<Future<?>, T>, Bindable<Future<?>, T> {

Executor DEFAULT_EXECUTOR = Executors.newVirtualThreadPerTaskExecutor();

Expand Down Expand Up @@ -382,7 +379,7 @@ public Future<T> orElse(Future<? extends T> other) {
@Override
public <X extends Throwable> Future<T> recoverWith(Class<X> type, Function1<? super X, ? extends T> mapper) {
return transform(value -> {
Try<T> try1 = TryOf.toTry(value);
Try<T> try1 = value.fix();
return try1.recoverWith(type, mapper);
});
}
Expand Down Expand Up @@ -424,7 +421,7 @@ private <R> Future<R> chain(Function1<? super Try<? extends T>, ? extends Kind<F
checkNonNull(mapper);
return new FutureImpl<>(executor,
(p, c) ->
promise.onComplete(value -> mapper.andThen(FutureOf::<R>toFuture).apply(value).onComplete(p::tryComplete)), this::cancel);
promise.onComplete(value -> mapper.apply(value).<Future<R>>fix().onComplete(p::tryComplete)), this::cancel);
}

static <T> Future<T> sync(Executor executor, Try<? extends T> result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.core.Applicable;
import com.github.tonivade.purefun.core.Bindable;
Expand All @@ -32,10 +31,8 @@
import com.github.tonivade.purefun.core.Unit;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.type.Try;
import com.github.tonivade.purefun.type.TryOf;

@HigherKind
public sealed interface Promise<T> extends PromiseOf<T>, Bindable<Promise<?>, T>, Applicable<Promise<?>, T> permits PromiseImpl {
public sealed interface Promise<T> extends Kind<Promise<?>, T>, Bindable<Promise<?>, T>, Applicable<Promise<?>, T> permits PromiseImpl {

boolean tryComplete(Try<? extends T> value);

Expand Down Expand Up @@ -75,7 +72,7 @@ default Promise<T> onFailure(Consumer1<? super Throwable> consumer) {

@Override
default <R> Promise<R> andThen(Kind<Promise<?>, ? extends R> next) {
return PromiseOf.toPromise(Bindable.super.andThen(next));
return Bindable.super.andThen(next).fix();
}

@Override
Expand Down Expand Up @@ -238,7 +235,7 @@ public Promise<T> onComplete(Consumer1<? super Try<? extends T>> consumer) {
@Override
public <R> Promise<R> ap(Kind<Promise<?>, ? extends Function1<? super T, ? extends R>> apply) {
Promise<R> result = new PromiseImpl<>(executor);
onComplete(try1 -> PromiseOf.toPromise(apply).onComplete(
onComplete(try1 -> apply.<Promise<Function1<T, R>>>fix().onComplete(
try2 -> result.tryComplete(Try.map2(try2, try1, Function1::apply))));
return result;
}
Expand All @@ -254,7 +251,7 @@ public <R> Promise<R> map(Function1<? super T, ? extends R> mapper) {
public <R> Promise<R> flatMap(Function1<? super T, ? extends Kind<Promise<?>, ? extends R>> mapper) {
Promise<R> other = new PromiseImpl<>(executor);
onComplete(value -> {
Try<Promise<R>> map = value.map(mapper.andThen(PromiseOf::toPromise));
Try<Promise<R>> map = value.map(mapper).fix();
map.fold(
error -> other.tryComplete(Try.failure(error)),
next -> next.onComplete(other::tryComplete));
Expand All @@ -276,9 +273,9 @@ private Option<Try<T>> current(Consumer1<? super Try<? extends T>> consumer) {
return Option.of(safeGet());
}

@SuppressWarnings("NullAway")
@SuppressWarnings({ "NullAway", "unchecked" })
private Try<T> safeGet() {
return TryOf.toTry(reference.get());
return (Try<T>) reference.get();
}

private void set(Try<? extends T> value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package com.github.tonivade.purefun.core;

import static com.github.tonivade.purefun.data.Sequence.listOf;

import java.util.Optional;
import java.util.stream.Stream;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.concurrent.Future;
import com.github.tonivade.purefun.data.Sequence;
import com.github.tonivade.purefun.type.Either;
Expand All @@ -23,9 +24,8 @@
* @param <A> type of function parameter
* @param <R> type of return value
*/
@HigherKind
@FunctionalInterface
public non-sealed interface Function1<A, R> extends Function1Of<A, R>, Recoverable {
public interface Function1<A, R> extends Kind<Function1<A, ?>, R>, Recoverable {

default R apply(A value) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
package com.github.tonivade.purefun.core;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.type.Either;
import com.github.tonivade.purefun.type.Option;
import com.github.tonivade.purefun.type.Try;
Expand All @@ -14,9 +14,8 @@
* but with additional functionality like the ability to memoize the result.
* @param <T> the returned type
*/
@HigherKind
@FunctionalInterface
public non-sealed interface Producer<T> extends ProducerOf<T>, Recoverable {
public interface Producer<T> extends Kind<Producer<?>, T>, Recoverable {

default T get() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
import static com.github.tonivade.purefun.core.Precondition.checkNonNull;
import java.util.stream.Stream;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;

@HigherKind
public sealed interface Trampoline<T> extends TrampolineOf<T>, Bindable<Trampoline<?>, T> {
public sealed interface Trampoline<T> extends Kind<Trampoline<?>, T>, Bindable<Trampoline<?>, T> {

@Override
default <R> Trampoline<R> map(Function1<? super T, ? extends R> map) {
Expand All @@ -25,7 +23,7 @@ default <R> Trampoline<R> map(Function1<? super T, ? extends R> map) {
default <R> Trampoline<R> flatMap(Function1<? super T, ? extends Kind<Trampoline<?>, ? extends R>> map) {
return fold(
next -> more(() -> next.flatMap(map)),
value -> map.apply(value).fix(TrampolineOf::toTrampoline));
value -> map.apply(value).<Trampoline<R>>fix());
}

default <R> R fold(Function1<Trampoline<T>, R> more, Function1<T, R> done) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
import java.io.Serializable;
import java.util.Objects;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.data.Sequence;

@HigherKind
public final class Tuple1<A> implements Tuple, Tuple1Of<A>, Serializable {
public final class Tuple1<A> implements Tuple, Kind<Tuple1<?>, A>, Serializable {

@Serial
private static final long serialVersionUID = 6343431593011527978L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
import java.io.Serializable;
import java.util.Objects;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.data.Sequence;

@HigherKind
public final class Tuple2<A, B> implements Tuple, Tuple2Of<A, B>, Serializable {
public final class Tuple2<A, B> implements Tuple, Serializable {

@Serial
private static final long serialVersionUID = 5034828839532504174L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ default <R> ImmutableArray<R> map(Function1<? super E, ? extends R> mapper) {

@Override
default <R> ImmutableArray<R> flatMap(Function1<? super E, ? extends Kind<Sequence<?>, ? extends R>> mapper) {
return ImmutableArray.from(stream().flatMap(mapper.andThen(SequenceOf::toSequence).andThen(Sequence::stream)::apply));
return ImmutableArray.from(stream().flatMap(e -> mapper.apply(e).<Sequence<R>>fix().stream()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ default <R> ImmutableList<R> map(Function1<? super E, ? extends R> mapper) {

@Override
default <R> ImmutableList<R> flatMap(Function1<? super E, ? extends Kind<Sequence<?>, ? extends R>> mapper) {
return ImmutableList.from(stream().flatMap(mapper.andThen(SequenceOf::toSequence).andThen(Sequence::stream)::apply));
return ImmutableList.from(stream().flatMap(e -> mapper.apply(e).<Sequence<R>>fix().stream()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ default <R> ImmutableSet<R> map(Function1<? super E, ? extends R> mapper) {

@Override
default <R> ImmutableSet<R> flatMap(Function1<? super E, ? extends Kind<Sequence<?>, ? extends R>> mapper) {
return ImmutableSet.from(stream().flatMap(mapper.andThen(SequenceOf::toSequence).andThen(Sequence::stream)::apply));
return ImmutableSet.from(stream().flatMap(e -> mapper.apply(e).<Sequence<R>>fix().stream()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ default <R> ImmutableTree<R> map(Comparator<? super R> comparator, Function1<? s

@Override
default <R> ImmutableTree<R> flatMap(Function1<? super E, ? extends Kind<Sequence<?>, ? extends R>> mapper) {
return ImmutableTree.from(naturalOrder(), stream().flatMap(mapper.andThen(SequenceOf::toSequence).andThen(Sequence::stream)::apply));
return ImmutableTree.from(naturalOrder(), stream().flatMap(e -> mapper.apply(e).<Sequence<R>>fix().stream()));
}

default <R> ImmutableTree<R> flatMap(Comparator<? super R> comparator, Function1<? super E, ? extends Kind<Sequence<?>, ? extends R>> mapper) {
return ImmutableTree.from(comparator, stream().flatMap(mapper.andThen(SequenceOf::toSequence).andThen(Sequence::stream)::apply));
return ImmutableTree.from(comparator, stream().flatMap(e -> mapper.apply(e).<Sequence<R>>fix().stream()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.core.Bindable;
import com.github.tonivade.purefun.core.Function1;
Expand All @@ -32,8 +31,7 @@
import com.github.tonivade.purefun.core.Tuple2;
import com.github.tonivade.purefun.type.Option;

@HigherKind
public non-sealed interface Sequence<E> extends SequenceOf<E>, Iterable<E>, Bindable<Sequence<?>, E> {
public interface Sequence<E> extends Kind<Sequence<?>, E>, Iterable<E>, Bindable<Sequence<?>, E> {

int size();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

import java.io.Serializable;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;

@HigherKind
public record Const<T, A>(T value) implements ConstOf<T, A>, Serializable {
public record Const<T, A>(T value) implements Kind<Const<T, ?>, A>, Serializable {

public Const {
checkNonNull(value);
Expand Down
12 changes: 5 additions & 7 deletions core/src/main/java/com/github/tonivade/purefun/type/Either.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.NoSuchElementException;
import java.util.stream.Stream;

import com.github.tonivade.purefun.HigherKind;
import com.github.tonivade.purefun.Kind;
import com.github.tonivade.purefun.Nullable;
import com.github.tonivade.purefun.core.Applicable;
Expand All @@ -37,8 +36,7 @@
* @param <L> type of the left value, negative case
* @param <R> type of the right value, positive case
*/
@HigherKind
public sealed interface Either<L, R> extends EitherOf<L, R>, Bindable<Either<L, ?>, R>, Applicable<Either<L, ?>, R> {
public sealed interface Either<L, R> extends Kind<Either<L, ?>, R>, Bindable<Either<L, ?>, R>, Applicable<Either<L, ?>, R> {

static <L, R> Either<L, R> left(L value) {
return new Left<>(value);
Expand Down Expand Up @@ -108,7 +106,7 @@ default <T> Either<L, T> map(Function1<? super R, ? extends T> map) {
@Override
default <T> Either<L, T> ap(
Kind<Either<L, ?>, ? extends Function1<? super R, ? extends T>> apply) {
return apply.fix(EitherOf::toEither).flatMap(this::map);
return apply.<Either<L, Function1<R, T>>>fix().flatMap(this::map);
}

default <T> Either<T, R> mapLeft(Function1<? super L, ? extends T> map) {
Expand All @@ -119,7 +117,7 @@ default <T> Either<T, R> mapLeft(Function1<? super L, ? extends T> map) {
@SuppressWarnings("unchecked")
default <T> Either<L, T> flatMap(Function1<? super R, ? extends Kind<Either<L, ?>, ? extends T>> map) {
if (this instanceof Right<L, R>(var right)) {
return map.andThen(EitherOf::<L, T>toEither).apply(right);
return map.apply(right).fix();
}
return (Either<L, T>) this;
}
Expand Down Expand Up @@ -150,12 +148,12 @@ default Either<L, R> filterOrElse(Matcher1<? super R> matcher, Producer<? extend
if (this instanceof Right<L, R>(var right) && matcher.match(right)) {
return this;
}
return orElse.andThen(EitherOf::toEither).get();
return orElse.get().fix();
}

default Either<L, R> or(Producer<Kind<Either<L, ?>, R>> orElse) {
if (this instanceof Left) {
return orElse.andThen(EitherOf::toEither).get();
return orElse.get().fix();
}
return this;
}
Expand Down
Loading