Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tonivade committed May 3, 2024
1 parent 484a66a commit 384c841
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
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.State;
import com.github.tonivade.purefun.monad.StateOf;
import com.github.tonivade.purefun.runtimes.ConsoleExecutor;
Expand All @@ -40,7 +39,7 @@ public void interpretState() {

@Test
public void interpretIO() {
var foldMap = echo.foldMap(Instances.<IO<?>>monad(), new IOProgramToIO());
var foldMap = echo.foldMap(Instances.monad(), new IOProgramToIO());

var executor = new ConsoleExecutor().read("Toni");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@ExtendWith(MockitoExtension.class)
public class MonadDeferTest {

private MonadDefer<IO<?>> ioMonadDefer = Instances.<IO<?>>monadDefer();
private MonadDefer<IO<?>> ioMonadDefer = Instances.monadDefer();
private MonadDefer<Kind<Kind<PureIO<?, ?, ?>, Void>, Throwable>> PureIOMonadDefer =
new Instance<Kind<Kind<PureIO<?, ?, ?>, Void>, Throwable>>(){}.monadDefer();
private MonadDefer<Kind<Kind<EitherT<?, ?, ?>, IO<?>>, Throwable>> eitherTMonadDefer =
Expand Down Expand Up @@ -92,8 +92,8 @@ public void ioBracketUseError() throws Exception {
@Test
public void eitherTBracket() throws Exception {
Kind<Kind<Kind<EitherT<?, ?, ?>, IO<?>>, Throwable>, String> bracket =
eitherTMonadDefer.bracket(EitherT.<IO<?>, Throwable, AutoCloseable>right(Instances.<IO<?>>monad(), resource),
r -> EitherT.<IO<?>, Throwable, String>right(Instances.<IO<?>>monad(), "done"));
eitherTMonadDefer.bracket(EitherT.<IO<?>, Throwable, AutoCloseable>right(Instances.monad(), resource),
r -> EitherT.<IO<?>, Throwable, String>right(Instances.monad(), "done"));

String result = bracket.fix(EitherTOf::narrowK).get().fix(toIO()).unsafeRunSync();

Expand All @@ -104,8 +104,8 @@ public void eitherTBracket() throws Exception {
@Test
public void eitherTBracketAcquireError() throws Exception {
Kind<Kind<Kind<EitherT<?, ?, ?>, IO<?>>, Throwable>, String> bracket =
eitherTMonadDefer.bracket(EitherT.<IO<?>, Throwable, AutoCloseable>left(Instances.<IO<?>>monad(), new IllegalStateException()),
r -> EitherT.<IO<?>, Throwable, String>right(Instances.<IO<?>>monad(), "done"));
eitherTMonadDefer.bracket(EitherT.<IO<?>, Throwable, AutoCloseable>left(Instances.monad(), new IllegalStateException()),
r -> EitherT.<IO<?>, Throwable, String>right(Instances.monad(), "done"));

assertThrows(IllegalStateException.class,
() -> bracket.fix(EitherTOf::narrowK).value().fix(toIO()).unsafeRunSync());
Expand All @@ -116,8 +116,8 @@ public void eitherTBracketAcquireError() throws Exception {
@Test
public void eitherTBracketUseError() throws Exception {
Kind<Kind<Kind<EitherT<?, ?, ?>, IO<?>>, Throwable>, String> bracket =
eitherTMonadDefer.bracket(EitherT.<IO<?>, Throwable, AutoCloseable>right(Instances.<IO<?>>monad(), resource),
r -> EitherT.<IO<?>, Throwable, String>left(Instances.<IO<?>>monad(), new UnsupportedOperationException()));
eitherTMonadDefer.bracket(EitherT.<IO<?>, Throwable, AutoCloseable>right(Instances.monad(), resource),
r -> EitherT.<IO<?>, Throwable, String>left(Instances.monad(), new UnsupportedOperationException()));

Either<Throwable, String> unsafeRunSync = bracket.fix(toEitherT()).value().fix(toIO()).unsafeRunSync();

Expand All @@ -128,8 +128,8 @@ public void eitherTBracketUseError() throws Exception {
@Test
public void optionTBracket() throws Exception {
Kind<Kind<OptionT<?, ?>, IO<?>>, String> bracket =
optionTMonadDefer.bracket(OptionT.some(Instances.<IO<?>>monad(), resource),
r -> OptionT.some(Instances.<IO<?>>monad(), "done"));
optionTMonadDefer.bracket(OptionT.some(Instances.monad(), resource),
r -> OptionT.some(Instances.monad(), "done"));

String result = bracket.fix(OptionTOf::narrowK).getOrElseThrow().fix(toIO()).unsafeRunSync();

Expand All @@ -141,8 +141,8 @@ public void optionTBracket() throws Exception {
@Disabled
public void optionTBracketAcquireError() throws Exception {
Kind<Kind<OptionT<?, ?>, IO<?>>, String> bracket =
optionTMonadDefer.bracket(OptionT.<IO<?>, AutoCloseable>none(Instances.<IO<?>>monad()),
r -> OptionT.some(Instances.<IO<?>>monad(), "done"));
optionTMonadDefer.bracket(OptionT.<IO<?>, AutoCloseable>none(Instances.monad()),
r -> OptionT.some(Instances.monad(), "done"));

NoSuchElementException error = assertThrows(NoSuchElementException.class,
() -> bracket.fix(OptionTOf::narrowK).getOrElseThrow().fix(toIO()).unsafeRunSync());
Expand All @@ -155,8 +155,8 @@ public void optionTBracketAcquireError() throws Exception {
@Disabled
public void optionTBracketUseError() throws Exception {
Kind<Kind<OptionT<?, ?>, IO<?>>, String> bracket =
optionTMonadDefer.bracket(OptionT.some(Instances.<IO<?>>monad(), resource),
r -> OptionT.<IO<?>, String>none(Instances.<IO<?>>monad()));
optionTMonadDefer.bracket(OptionT.some(Instances.monad(), resource),
r -> OptionT.<IO<?>, String>none(Instances.monad()));

NoSuchElementException error = assertThrows(NoSuchElementException.class,
() -> bracket.fix(OptionTOf::narrowK).getOrElseThrow().fix(toIO()).unsafeRunSync());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ default <G, T> Kind<G, Kind<F, T>> sequence(Applicative<G> applicative,

@Override
default <T, R> Kind<F, R> map(Kind<F, ? extends T> value, Function1<? super T, ? extends R> map) {
Kind<Id<?>, Kind<F, R>> traverse = traverse(Instances.<Id<?>>applicative(), value, t -> Id.of(map.apply(t)));
Kind<Id<?>, Kind<F, R>> traverse = traverse(Instances.applicative(), value, t -> Id.of(map.apply(t)));
return traverse.fix(toId()).value();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ConcurrentTest {

@Test
public void ioRaceA() {
Concurrent<IO<?>> concurrent = Instances.<IO<?>>concurrent();
Concurrent<IO<?>> concurrent = Instances.concurrent();

Kind<IO<?>, Either<Integer, String>> race = concurrent.race(
IO.delay(Duration.ofMillis(10), () -> 10),
Expand All @@ -36,7 +36,7 @@ public void ioRaceA() {

@Test
public void ioRaceB() {
Concurrent<IO<?>> concurrent = Instances.<IO<?>>concurrent();
Concurrent<IO<?>> concurrent = Instances.concurrent();

Kind<IO<?>, Either<Integer, String>> race = concurrent.race(
IO.delay(Duration.ofMillis(100), () -> 10),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@ExtendWith(MockitoExtension.class)
public class ForTest {

final Monad<Id<?>> monad = Instances.<Id<?>>monad();
final Monad<Id<?>> monad = Instances.monad();

@Test
public void map() {
Expand Down Expand Up @@ -55,9 +55,9 @@ public void returns(@Mock Function1<String, String> mapper) {

@Test
public void flatMap() {
Id<String> result = For.with(Instances.<Id<?>>monad())
.andThen(() -> Instances.<Id<?>>monad().pure("value"))
.flatMap(string -> Instances.<Id<?>>monad().pure(string.toUpperCase()))
Id<String> result = For.with(monad)
.andThen(() -> monad.pure("value"))
.flatMap(string -> monad.pure(string.toUpperCase()))
.fix(toId());

assertEquals(Id.of("VALUE"), result);
Expand All @@ -68,7 +68,7 @@ public void applyBug(@Mock Producer<String> task1, @Mock Producer<String> task2)
when(task1.get()).thenReturn("hola toni");
when(task2.get()).thenReturn("adios toni");

Applicative<IO<?>> monad = Instances.<IO<?>>applicative();
Applicative<IO<?>> monad = Instances.applicative();
var result = For.with(monad)
.then(IO.task(task1))
.then(IO.task(task2))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class MonadStateTest {

private MonadState<IO<?>, ImmutableArray<String>> monadState =
MonadState.from(Instances.<IO<?>>monadDefer(), ImmutableArray.empty());
MonadState.from(Instances.monadDefer(), ImmutableArray.empty());

@Test
public void program() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@ExtendWith(MockitoExtension.class)
class SelectiveTest {

private final Selective<IO<?>> monad = Instances.<IO<?>>monad();
private final Selective<IO<?>> monad = Instances.monad();

@Test
void apply() {
Expand Down

0 comments on commit 384c841

Please sign in to comment.