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
6
votes
3 answers

How to flatten a Try[Option[T]]

I want to flatten a Try[Option[T]] into a Try[T] Here is my code def flattenTry[T](t: Try[Option[T]]) : Try[T] = { t match { case f : Failure[T] => f.asInstanceOf[Failure[T]] case Success(e) => e match { case None =>…
Yann Moisan
  • 8,161
  • 8
  • 47
  • 91
6
votes
7 answers

Scala: Something like Option (Some, None) but with three states: Some, None, Unknown

I need to return values, and when someone asks for a value, tell them one of three things: Here is the value There is no value We have no information on this value (unknown) case 2 is subtly different than case 3. Example: val radio =…
waterlooalex
  • 13,642
  • 16
  • 78
  • 99
6
votes
2 answers

Compact syntax for get head of list as Option

Is there a compact way to get the head of a list as a Some when the list is non-empty, getting None otherwise? This is what I am currently doing, val ms = moves.filter { ...some predicate... } if (ms.nonEmpty) Some(ms.head) else None
Janek Bogucki
  • 5,033
  • 3
  • 30
  • 40
5
votes
2 answers

scala newbie having troubles with Option, what's the equivalent of the ternary operator

I've already read that the if statement in scala always returns an expression So I'm trying to do the following (pseudo code) sql = "select * from xx" + iif(order.isDefined, "order by " order.get, "") I'm trying with val sql: String = "select *…
opensas
  • 60,462
  • 79
  • 252
  • 386
5
votes
1 answer

Scala: Can I convert an Option to varargs?

I have an Option: val myOption: Option[Int] = fooBar() And a method that takes a varargs param: def myMethod(a: String, b: Int*) = {...} Is there any way to pass the option to the method as a varargs param? i.e. if the option is Some(3) then pass…
Chris B
  • 9,149
  • 4
  • 32
  • 38
5
votes
4 answers

Simplifying Option[Boolean] expression in Scala

I have code like that: optionBoolean.getOrElse(false) && otherOptionBoolean.getOrElse(false) And Scalastyle tells me that it can be simplified. How?
Ava
  • 818
  • 10
  • 18
5
votes
2 answers

Scala Option[(A, B)] pattern matching

I am writing a Java code generator. I have an immutable Map that contains a mapping from java.sql.Types [Int] to a tuple of (String, String) where the first value is a Java type and the second a Java package from which to import the type if it is…
Ralph
  • 31,584
  • 38
  • 145
  • 282
5
votes
2 answers

How to turn `Either[Error, Option[Either[Error, Account]]]` to `Either[Error, Option[Account]]` with typelevel cats?

I'm using cats, wonder how to turn a data with it. From val data = Either[Error, Option[Either[Error, Account]]] to val target: Either[Error, Option[Account]] = howToConvert(data) If there is any Error happens, the result will be Left(error) with…
Freewind
  • 193,756
  • 157
  • 432
  • 708
5
votes
3 answers

Option monad in scala

how is meant to work Option monad? I'm browsing the scala api and there is an example (I mean the second one), Because of how for comprehension works, if None is returned from request.getParameter, the entire expression results in None But when I…
ryskajakub
  • 6,351
  • 8
  • 45
  • 75
5
votes
8 answers

Correct way to work with two instances of Option together

When I have one Option[T] instance it is quite easy to perform any operation on T using monadic operations such as map() and flatMap(). This way I don't have to do checks to see whether it is defined or empty, and chain operations together to…
jbx
  • 21,365
  • 18
  • 90
  • 144
5
votes
3 answers

Apache Spark: dealing with Option/Some/None in RDDs

I'm mapping over an HBase table, generating one RDD element per HBase row. However, sometimes the row has bad data (throwing a NullPointerException in the parsing code), in which case I just want to skip it. I have my initial mapper return an…
Ken Williams
  • 22,756
  • 10
  • 85
  • 147
5
votes
1 answer

Scala Mutable Option?

I want something like this: private val cachedResponse = mutable.Option.empty[A] def get: A = cachedResponse getOrElseUpdate db.findModel() def update: Unit = { db.updateModel cachedResponse.empty() // set it to None/Option.empty } I am…
pathikrit
  • 32,469
  • 37
  • 142
  • 221
5
votes
2 answers

Option.fold - why is its second argument not a binary operator?

I'm sure there's a good reason for this, but I'm not seeing it. Fold on (say) List returns the result of applying fold operator op between all the elements and z It has an obvious relationship with foldLeft and foldRight that do the same thing but…
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
5
votes
3 answers

Fold collection with None as start value

I want to fold a collection or Y's and return an Option[X]. I want to start with None. Like this... def f(optX: Option[X], y: Y): Option[X] val optX = collectionOfY.fold(None) { case (prev, y) => f(prev,y) } adding unneeded types to make it…
opus111
  • 2,744
  • 4
  • 25
  • 41
5
votes
3 answers

How to efficiently check that two Options is defined?

Suppose, I have two options: val a: Option = Some("string") val b: Option = None How to efficiently check that both a and b is defined? I now that I can wrote something like this: if (a.isDefined && b.isDefined) { .... } But, it looks ugly and…
atygaev.mi
  • 144
  • 2
  • 11