“For” hack with Option monad in Java

There has been some discussion going on in the blogosphere about monads, and especially about the Haskell Maybe monad or the Scala option class. Those are ways to prevent problems with NULL and NPEs that Java lacks. Java returns NULL form many methods to indicate failure or no result. Suppose we have a method which returns a name:

The problem with this method is that we don’t know if it returns null. So the developers needs to deal with length == null, though the compiler doesn’t force the developer to deal with the returned NULL. A lazy developer then leads to null pointer exceptions. Other languages deal in different ways with this problem. Groovy has safe operators and Nice has option types.

Some posts like the one from James show how to use options in Java. All have the problem that you need to unwrap the value inside the option / maybe. Scala ans Haskell do that automatically for you, with the case classes in Scala for example.

But there is a construct with syntactic sugar in Java which unwraps the value from inside another class: the for loop from Java 1.5.

To make this work we need our option class to implement Iterable.

And the None and Some sub classes to return an empty iterator

or an iterator with one item.

Then voila Java does the unwrapping for us and instead of

we can write (sacrificing the else):

Thanks for listening.

Update: Completely ignoring the point of this post, Euxx posted a correction for the single element list creation I did.

Happens all the time in IT, people missing the point but nitpicking on something irrelevant. Other than that, if we start arguing performance, “The java.util.Collections class also have number of other utility methods that allow to shorten code like this and help to improve performance of your application.” I’d write (or reuse) a OneElementIterator something like this (could probably be optimized with some further thinking)

(Or again using google collections)

“I can’t not notice the ugly use of ArrayList to create collection with a single element, but what made matter worse, is that author suggested to use 3rd party library to replace those 3 lines of code.”

As far as I know, the JDK does not help you very much with Iterators or Iterables as third party libraries do. So, yes, I’d suggest using a third party library to implement an Iterator/Iterable for Option.

330 thoughts on ““For” hack with Option monad in Java”

  1. Pingback: shox shoes
  2. Pingback: ?????
  3. Pingback: nike lebron shoes
  4. Pingback: hermes belt replca
  5. Pingback: yeezy shoes
  6. Pingback: www.sebby.cc
  7. Pingback: cheap nike
  8. Pingback: nike tiempos
  9. Pingback: karen brown oakley
  10. Pingback: csgo steam codes
  11. Pingback: yrergp
  12. Pingback: xlitcd
  13. Pingback: dogyslnne
  14. Pingback: occhialo oakley
  15. Pingback: ?????
  16. Pingback: ray ban rb4127
  17. Pingback: ray ban b&l precio
  18. Pingback: CSGO
  19. Pingback: NBA2K16
  20. Pingback: ray ban sole donna
  21. Pingback: asics 1170 hombre
  22. Pingback: ray ban uomo sole
  23. Pingback: toms uomo italia
  24. Pingback: nike air max 46 kg
  25. Pingback: NBA
  26. Pingback: counterstrike
  27. Pingback: nba
  28. Pingback: oakley moto gp
  29. Pingback: ray ban india
  30. Pingback: toms blu suede 5s
  31. Pingback: NBA2K16
  32. Pingback: counterstrike
  33. Pingback: counterstrike
  34. Pingback: csgo
  35. Pingback: NBA
  36. Pingback: nba 2k16
  37. Pingback: csgo
  38. Pingback: oakley duryea
  39. Pingback: replica nike max
  40. Pingback: miss annie oakley
  41. Pingback: howtoplay
  42. Pingback: NBA2K
  43. Pingback: nike air max 40 70
  44. Pingback: nike official site
  45. Pingback: ray bans 4151
  46. Pingback: nike schoenen 38
  47. Pingback: nike dames laarzen
  48. Pingback: nike nuevas botas
  49. Pingback: nike air max 50
  50. Pingback: nike air max 87
  51. Pingback: nike dart 9 uomo
  52. Pingback: nike schoenen 40
  53. Pingback: nike air 2
  54. Pingback: nike free salg 70
  55. Pingback: Book Printing

Comments are closed.