Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have a library method to build Option-s that takes into account empty strings?

Tags:

scala

I want to filter out empty strings to put them into an Option. So I quickly built this now:

def StrictOption(s: String) = s match {
  case s if s != null && s.trim.length() > 0 => Some(s)
  case _ => None
}

Question: is this maybe already somewhere in the standard library?

like image 527
Manuel Bernhardt Avatar asked Dec 16 '11 12:12

Manuel Bernhardt


People also ask

What is Option [] in Scala?

The Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null.

What is getOrElse in Scala?

As we know getOrElse method is the member function of Option class in scala. This method is used to return an optional value. This option can contain two objects first is Some and another one is None in scala. Some class represent some value and None is represent a not defined value.


4 Answers

I don't think there's one single method in the standard library to do this, but you can do this much more tersely than your implementation.

Option(s).filter(_.trim.nonEmpty)
like image 178
Ben James Avatar answered Oct 18 '22 23:10

Ben James


If you care at all about performance then

if (s.trim.isEmpty) None else Some(s)

is only 4 characters longer than Ben James's solution, and runs 3 times faster, in my benchmark (47 vs 141).

like image 22
Luigi Plinge Avatar answered Oct 18 '22 22:10

Luigi Plinge


Starting Scala 2.13, for those not expecting nulls (non-Java context), Option::unless and Option::when are now an alternative option:

// val str = "hello"
Option.unless(str.isEmpty)(str)
// Option[String] = Some(hello)
Option.when(str.nonEmpty)(str)
// Option[String] = Some(hello)

// val str: String = ""
Option.unless(str.isEmpty)(str)
// Option[String] = None
Option.when(str.nonEmpty)(str)
// Option[String] = None
like image 6
Xavier Guihot Avatar answered Oct 18 '22 23:10

Xavier Guihot


There's nothing built in; Ben's filter is the best brief version if performance isn't an issue (e.g. you're validating user input). Typically, performance will not be an issue.

Also, note that it's a little strange to use match when you're not actually matching anything; it's just more boilerplate to get an if-else statement. Just say

if (s ne null && s.trim.length > 0) Some(s) else None

which is about as fast and brief as anything, unless you want to write your own is-it-whitespace method. Note that trim uses a peculiar criterion: anything above \u0020 (i.e. ' ') is not trimmed, and anything equal or below is. So you could also write your own trimmed-string-is-empty detector, if performance of this operation was particularly important:

def ContentOption(s: String): Option[String] = {
  if (s ne null) {
    var i = s.length-1
    while (i >= 0) {
      if (s.charAt(i) > ' ') return Some(s)
      i -= 1
    }
  }
  None
}
like image 5
Rex Kerr Avatar answered Oct 18 '22 22:10

Rex Kerr