Scala offers a hierarchy of classes Option[T]
, Some[T] extends Option[T]
, and None extends Option[Nothing]
that I have found useful for wrapping Java method calls that can return null
, among other things:
val result = Option(someJavaMethodThatReturnsNull())
result
acts like a sequence of zero or one items, depending on whether the Java method returned an object or null
. Option
has methods like map
, filter
, etc. that you can use just like those on a sequence and either return a new sequence (Some
), if the original was Some[T]
, or None
if the original was None
.
It appears that the Clojure function seq
behaves similarly: (seq x)
will be a sequence of one item if x is non-null or nil
if x is null. This value can then be passed to (map ...)
, (filter ...)
, etc., just like the Scala Option
methods.
Am I missing something? Does this pattern make sense? Is this a "Eureka!" moment that is obvious to experienced Clojure programmers?