Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tonivade committed May 5, 2024
1 parent 3893cf1 commit 3563aec
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public class FreeAlgTest {

@Test
public void algebra() {
var hello =
read().flatMap(name -> write("hello " + name))
.andThen(send("toni@home", "hello"));
var hello = read()
.flatMap(name -> write("hello " + name))
.andThen(send("toni@home", "hello"));

ConsoleExecutor executor = new ConsoleExecutor().read("toni");

Expand All @@ -57,25 +57,19 @@ public <T> Kind<IO<?>, T> apply(Kind<Kind<Kind<EitherK<?, ?, ?>, ConsoleAlg<?>>,
new FunctionK<>() {
@Override
public <X> Kind<IO<?>, X> apply(Kind<ConsoleAlg<?>, ? extends X> kind) {
ConsoleAlg<X> consoleAlg = kind.fix(ConsoleAlgOf::narrowK);
if (consoleAlg instanceof ConsoleAlg.ReadLine) {
return (Kind<IO<?>, X>) console.readln();
}
if (consoleAlg instanceof ConsoleAlg.WriteLine writeLine) {
return (Kind<IO<?>, X>) console.println(writeLine.line());
}
throw new IllegalStateException();
return (Kind<IO<?>, X>) switch(kind.fix(ConsoleAlgOf::narrowK)) {
case ConsoleAlg.ReadLine r -> console.readln();
case ConsoleAlg.WriteLine(var line) -> console.println(line);
};
}
},
new FunctionK<>() {
@Override
public <X> Kind<IO<?>, X> apply(Kind<EmailAlg<?>, ? extends X> kind) {
EmailAlg<X> emailAlg = kind.fix(EmailAlgOf::narrowK);
if (emailAlg instanceof EmailAlg.SendEmail sendEmail) {
return (Kind<IO<?>, X>) console.println(
"email to " + sendEmail.to() + " with content " + sendEmail.content());
}
throw new IllegalStateException();
return (Kind<IO<?>, X>) switch (kind.fix(EmailAlgOf::narrowK)) {
case EmailAlg.SendEmail(var to, var content)
-> console.println("email to " + to + " with content " + content);
};
}
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import com.github.tonivade.purefun.Kind;


public interface InjectK<F, G> {

<T> Kind<G, T> inject(Kind<F, ? extends T> value);
Expand Down

0 comments on commit 3563aec

Please sign in to comment.