Skip to content

Instantly share code, notes, and snippets.

@rikvdkleij
Last active September 26, 2016 11:59
Show Gist options
  • Save rikvdkleij/c6aaad2707ec0564a314af2dffc21503 to your computer and use it in GitHub Desktop.
Save rikvdkleij/c6aaad2707ec0564a314af2dffc21503 to your computer and use it in GitHub Desktop.
In Scala I can do:
Option<Response> responseForTransaction = Option(getResponseForTransaction(transaction))
responseForTransaction match {
case Some(r) => // do something with r
case None => ....
}
In Javaslang I tried this:
Option<Response> responseForTransaction = getResponseForTransaction(transaction);
Match(responseForTransaction).of(
Case(Option($())), r -> .....)
Case(None()), ....)
)
@danieldietrich
Copy link

Javaslang currently has the API.run() method in order to perform side-effect.

String value = Match(responseForTransaction).of(
        Case(Some($()), String::valueOf),
        Case(None(), "no value")
);

/*Void nothing =*/
Match(responseForTransaction).of(
        Case(Some($()), r -> run(() -> System.out.println("response: " + r))),
        Case(None(), () -> run(() -> System.out.println("no response")))
);

But we still work on something better, e.g.

Match(...).of(
    Case(Some($()), r -> { ... }),
    Case(None(), () -> { ... })
);

Maybe it will be run() instead of of(). And we will need Comsumer1..8.

@rikvdkleij
Copy link
Author

Or:

responseForTransaction.<Runnable>map(r -> () -> { ....}).getOrElse(() -> .... ).run();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment