Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // sample snippet that does not use monads
- // note explicit null checks and imperative flow
- String param(String key) {
- //.. fetch value from request/session
- return value;
- }
- Trade queryTrade(String ref) {
- //.. query from db
- return trade
- }
- public static void main(String[] args) {
- String key;
- //.. set key
- String refNo = param(key);
- if (refNo == null) {
- //.. exception processing
- }
- Trade trade = queryTrade (refNo);
- if (trade == null) { //.. explicit null check
- //.. exception processing
- }
- }
- // monadic version
- def param(key: String): Option[String] = { //.. monadic return
- //..
- }
- def queryTrade(ref: String): Option[Trade] = { //.. monadic return
- //..
- }
- def main(args: Array[String]) {
- // monadic comprehension block
- // automatic sequencing plus NO explicit null handling
- // makes program structure look better
- val trade =
- (
- for {
- r <- param("refNo")
- t <- queryTrade (r)
- }
- yield t
- ) getOrElse error("not found")
- //..
Add Comment
Please, Sign In to add comment