Guest User

Untitled

a guest
Jul 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. // sample snippet that does not use monads
  2. // note explicit null checks and imperative flow
  3.  
  4. String param(String key) {
  5. //.. fetch value from request/session
  6. return value;
  7. }
  8.  
  9. Trade queryTrade(String ref) {
  10. //.. query from db
  11. return trade
  12. }
  13.  
  14. public static void main(String[] args) {
  15. String key;
  16. //.. set key
  17.  
  18. String refNo = param(key);
  19. if (refNo == null) {
  20. //.. exception processing
  21. }
  22.  
  23. Trade trade = queryTrade (refNo);
  24. if (trade == null) { //.. explicit null check
  25. //.. exception processing
  26. }
  27. }
  28.  
  29. // monadic version
  30.  
  31. def param(key: String): Option[String] = { //.. monadic return
  32. //..
  33. }
  34. def queryTrade(ref: String): Option[Trade] = { //.. monadic return
  35. //..
  36. }
  37.  
  38. def main(args: Array[String]) {
  39.  
  40. // monadic comprehension block
  41. // automatic sequencing plus NO explicit null handling
  42. // makes program structure look better
  43. val trade =
  44. (
  45. for {
  46. r <- param("refNo")
  47. t <- queryTrade (r)
  48. }
  49. yield t
  50. ) getOrElse error("not found")
  51.  
  52. //..
Add Comment
Please, Sign In to add comment