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
2
votes
2 answers

Why this two code blocks are equivalent for Option class?

In documentation of Option class written that following two examples are equivalent: val name: Option[String] = request getParameter "name" val upper = name map { _.trim } filter { _.length != 0 } map { _.toUpperCase } println(upper getOrElse…
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
2
votes
3 answers

Scala Option map to another Option

Is there a way to avoid having to wrap a potentially null result inside another option when mapping from an option. option.flatMap(arg => Option(arg.somePotentiallyNullValue)) Eg something like option.optionMap(arg => arg.somePotentiallyNullValue…
sksamuel
  • 16,154
  • 8
  • 60
  • 108
2
votes
1 answer

Return values from a generic Option Array in Scala

I am having some difficulty making my function generic, and need some help. I have an array that takes Option's of T where T is a Fractional. In F#, there is a function "choose" which strips None's from a collection of Options. In scala, I am…
Alex
  • 2,342
  • 1
  • 18
  • 30
1
vote
2 answers

Using Option in functional way to avoid if-else

I have an Option instance, say O, which contains an instance of a class, say A, and A itself has some Options inside it. I have to achieve something like this, expressed in pseudo code as: if(A.x exists) { if(A.y exists) { //extract values…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
1
vote
3 answers

Using scala.Option functionally

I have an Option, say O, which can either be None or may have some value inside. If it has some value, that value may have a flag, say f. My requirement is that if O is None, then I create an object, say of type MyEntity,but if O has a value with…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
1
vote
2 answers

How is for comprehension returning None on comparison?

How is a below printed as None as the max is 3 here val firstNum: Option[Int] = None val secondNum: Option[Int] = Some(3) val a = for { f <- firstNum s <- secondNum } yield Math.max(f, s) println(a) output None
supernatural
  • 1,107
  • 11
  • 34
1
vote
1 answer

Option[Int] as index in loop

I am new at Scala so I do not know if I will ask something obvious. I am currently trying to define a function which can or can not receive a parameter called "position". This parameter is an Int (in case the user decides to pass it). Otherwise, it…
meisan
  • 37
  • 6
1
vote
1 answer

get long value from scala.Option in java

I have a Option from which i am trying to get long value. But this does not work. I tried the below steps but could not get the value as it shows compiler error. Please help. //#1 Option expireTimestamp =…
wandermonk
  • 6,856
  • 6
  • 43
  • 93
1
vote
2 answers

What is the possible use of (Monad) Option.size in scala

When we do Some(“some values”).size it returns 1. When we do None.size it returns 0. I was wondering what could be the possible use of the size method on an option. val x = Some(1,2,3,4).size println(x) // prints 1 val y = Some(List(“a”, ”b”,…
Raman Mishra
  • 2,635
  • 2
  • 15
  • 32
1
vote
2 answers

scala programming practice with option

May be my design is flawed (most probably it is) but I have been thinking about the way Option is used in Scala and I am not so very happy about it. Let's say I have 3 methods calling one another like this: def A(): reads a file and returns…
Mrinal
  • 1,826
  • 2
  • 19
  • 31
1
vote
2 answers

Scala Option[Seq[A]] exists

I have the following code: case class Person(name: String, age: Int) object Launcher extends App { val people = Option(Seq(Person("Andrii", 20), Person("John", 35), Person("Sam", 15))) def filterPeople(list: Option[Seq[Person]]): Boolean = …
Andrii Abramov
  • 10,019
  • 9
  • 74
  • 96
1
vote
1 answer

Scala Some redundant covariance

Scala standard library contains Option type. The Option type itself is covariant type, this is obvious from its declaration sealed abstract class Option[+A]. The questions are: Why its constructor Some is also covariant final case class Some[+A](x:…
1
vote
2 answers

scala Option type difference

What is the difference between passing two arguments vs passing one argument? val option1 = Option(String,String) and val option2 = Option(String)
G G
  • 1,049
  • 4
  • 17
  • 26
1
vote
1 answer

How to add List of Option of doubles in scala

I have a list of option like so, var data = List( List(Some(313.062468), Some(27.847252)), List(Some(301.873641), Some(42.884065)), List(Some(332.373186), Some(53.509768)) ) And I'd like to sum all the values of each nested list. I have the…
S. Nas
  • 331
  • 1
  • 4
  • 14
1
vote
2 answers

How to get the current and next element of List of list of options in scala

This question might be duplicated, however, I've not come across any question that answers my problem. So I have a List[ List[ Option[ Double ] ] ] With the following data: var tests = List( List(Some(313.062468), Some(27.847252)), …
S. Nas
  • 331
  • 1
  • 4
  • 14