Questions tagged [scala-option]

Option in the Scala language is a container for zero or one element of a given type.

Option in is a container for zero or one element of a given type.

Related tags

232 questions
127
votes
4 answers

How to filter None's out of List[Option]?

If I have a List[Option[A]] in Scala, what is the idiomatic way to filter out the None values? One way is to use the following: val someList: List[Option[String]] = List(Some("Hello"), None, Some("Goodbye")) someList.filter(_ != None) Is there a…
Ralph
  • 31,584
  • 38
  • 145
  • 282
119
votes
4 answers

Wrapping null-returning method in Java with Option in Scala?

Suppose I have a method session.get(str: String): String but you don't know whether it will return you a string or a null, because it comes from Java. Is there an easier way to treat this in Scala instead of session.get("foo") == null ? Maybe some…
José Leal
  • 7,989
  • 9
  • 35
  • 54
85
votes
5 answers

Composing Option with List in for-comprehension gives type mismatch depending on order

Why does this construction cause a Type Mismatch error in Scala? for (first <- Some(1); second <- List(1,2,3)) yield (first,second) :6: error: type mismatch; found : List[(Int, Int)] required: Option[?] for (first <- Some(1);…
77
votes
2 answers

How to transform Scala collection of Option[X] to collection of X

I'm starting to explore Scala, and one of the things I'm intrigued by is the Option type and the promise of being able to eliminate null related errors. However I haven't been able to work out how to transform a list (or other collection) of, say,…
npad
  • 5,036
  • 4
  • 24
  • 22
75
votes
4 answers

Is there a scala identity function?

If I have something like a List[Option[A]] and I want to convert this into a List[A], the standard way is to use flatMap: scala> val l = List(Some("Hello"), None, Some("World")) l: List[Option[java.lang.String]] = List(Some(Hello), None,…
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
74
votes
1 answer

How to get an Option from index in Collection in Scala?

Is there a way, only using the Scala collection API, to get an Option in a List when trying to get an element by its index? I'm looking for the equivalent of this function, does it exist? def optionalValue[T](l: List[T], index: Int) = { if (l.size…
Loic
  • 3,310
  • 4
  • 25
  • 43
48
votes
6 answers

Why is foreach better than get for Scala Options?

Why using foreach, map, flatMap etc. are considered better than using get for Scala Options? If I useisEmpty I can call get safely.
Michael
  • 10,185
  • 12
  • 59
  • 110
46
votes
4 answers

Scala: convert string to Int or None

I am trying to get a number out of an xml field ... 12 ... via Some((recipe \ "Main" \ "Quantity").text.toInt) Sometimes there may not be a value in the xml, though. The text will be "" and this throws an…
doub1ejack
  • 10,627
  • 20
  • 66
  • 125
43
votes
6 answers

Why Some(null) isn't considered None?

I am curious: scala> Some(null) == None res10: Boolean = false Why isn't Some(null) transformed to None?
Geo
  • 93,257
  • 117
  • 344
  • 520
41
votes
6 answers

Un-optioning an optioned Option

Say I have a val s: Option[Option[String]]. It can thus have the following values: Some(Some("foo")) Some(None) None I want to reduce it so that the first becomes Some("foo") while the two others become None. Obviously there are many ways to…
Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59
40
votes
2 answers

How to flatten list of options using higher order functions?

Using Scala 2.7.7: If I have a list of Options, I can flatten them using a for-comprehension: val listOfOptions = List(None, Some("hi"), None) listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None) scala> for (opt <-…
Synesso
  • 37,610
  • 35
  • 136
  • 207
38
votes
6 answers

Could/should an implicit conversion from T to Option[T] be added/created in Scala?

Is this an opportunity to make things a bit more efficient (for the prorammer): I find it gets a bit tiresome having to wrap things in Some, e.g. Some(5). What about something like this: implicit def T2OptionT( x : T) : Option[T] = if ( x == null )…
waterlooalex
  • 13,642
  • 16
  • 78
  • 99
35
votes
4 answers

Why doesn't Option have a fold method?

I wonder why scala.Option doesn't have a method fold like this defined: fold(ifSome: A => B , ifNone: => B) equivalent to map(ifSome).getOrElse(ifNone) Is there no better than using map + getOrElse?
soc
  • 27,983
  • 20
  • 111
  • 215
34
votes
9 answers

Check if a string is blank or doesn't exist in Scala

I have an Option[String]. I want to check if there is a string exists and if it's exists its not blank. def isBlank( input : Option[String]) : Boolean = { input.isEmpty || input.filter(_.trim.length > 0).isEmpty } Is there is a…
Soumya Simanta
  • 11,523
  • 24
  • 106
  • 161
33
votes
3 answers

Convert Option to Either in Scala

Suppose I need to convert Option[Int] to Either[String, Int] in Scala. I'd like to do it like this: def foo(ox: Option[Int]): Either[String, Int] = ox.fold(Left("No number")) {x => Right(x)} Unfortunately the code above doesn't compile and I need…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
2 3
15 16