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
0
votes
1 answer

How do I rewrite this java function in scala keeping the same Optional input parameter?

I have the following method in java protected T getObjectFromNullableOptional(final Optional opt) { return Optional.ofNullable(opt).flatMap(innerOptional -> innerOptional).orElse(null); } It takes a java Optional that can itself be null…
kane
  • 5,465
  • 6
  • 44
  • 72
0
votes
3 answers

Finding the average of option values containg object in a list

I'm new to Scala and I have been struggling with Option and Lists. I have the following object: object Person { case class Person(fName: String, lName: String, jeans: Option[Jeans], …
James
  • 9
  • 2
0
votes
2 answers

For comprehension parsing of optional string to int

Say I have the following for comprehension: val validatedInput = for { stringID <- parseToInt(optionalInputID) } yield (stringID) where optionalInputID is an input parameter of type Option[String]. I want to be able to convert an Option[String]…
Chris
  • 785
  • 10
  • 24
0
votes
1 answer

Cats OptionT future not completing

I'm having an issue where my future (wrapped with OptionT) doesn't complete. When I explicitly await the result withoutn OptionT, I can see the correct output (See logs below Some(2,2)). But in my for comprehension, we never see the "Ids: " logs.…
smur89
  • 329
  • 1
  • 3
  • 15
0
votes
3 answers

Fetch elements from Option[Any] of List

scala> val a = jsonMap.get("L2_ID") a: Option[Any] = Some(List(24493, 22774, 23609, 20517, 22829, 23646, 22779, 23578, 22765, 23657)) I want to fetch the first element of list i.e 24493. So, tried below code: scala> var b = a.map(_.toString) b:…
Kamal Kishore
  • 325
  • 2
  • 4
  • 15
0
votes
2 answers

Convert Option[String] to Map[String,trait] in SCALA

I am new to Scala and searched for the same as to how can we change from Option[String] to a Map[String,trait] but could not find much . The thing is I have a field of type Option[String] and I have to pass that value to a case class which takes…
neil
  • 149
  • 1
  • 12
0
votes
2 answers

Define nested strucType

I have the following question: Having an Option of List of elements I want to transform it into a List of elements by avoiding the use of .get on the Option. Below is the snippet of code that I want to change : Any idea how to modify this please…
scalacode
  • 1,096
  • 1
  • 16
  • 38
0
votes
1 answer

How to yield None in for-comprehension if some condition meets

I'm experimenting with for comprehension and wrote the following code: object Main extends App { val resultOption: Option[Int] = for{ i1 <- opt1 i2 <- opt2 } yield { if(i1 + i2 > 10) null.asInstanceOf[Int] …
St.Antario
  • 26,175
  • 41
  • 130
  • 318
0
votes
1 answer

Using map on Scala with option return types

say I have a function that takes in some kind of Option[]... namely: def help(x: Int, y : Option[BigInteger], ec: ExecutionContext, sc: SecurityContext): Future[Long] = { ... } and I have an object that calls on it…
bZhang
  • 307
  • 1
  • 2
  • 12
0
votes
4 answers

Get max in a ListBuffer of Some[Long] in Scala

This piece of code works fine and returns 343423 as expected: val longList: ListBuffer[Long] = ListBuffer(103948,343423,209754) val maxLong = longList.max But it doesn't work for Some[Long]: val longSomeList: ListBuffer[Some[Long]] =…
Aryan Firouzian
  • 1,940
  • 5
  • 27
  • 41
0
votes
0 answers

How to check spark-scala case class is null or not?

I am new to spark-scala, I have a case class as below case class Session(src: String, id: String, meterId: Option[Long], computerId: Option[String], startTime:…
naveen p
  • 74
  • 9
0
votes
2 answers

Scala returning option value

The problem is that I'd like this function to return option value (in form of Option[None]) or Option[something], but it only returns a Unit. What am I missing? def intersection(another: Interval){ var test: Option[Interval] = None if…
T.Simmari
  • 195
  • 1
  • 2
  • 5
0
votes
1 answer

Scala ifNotPresent concise form

I have the following collection: private val commandChain: mutable.Buffer[mutable.Buffer[Long]] = ArrayBuffer() I need to do the following: def :->(command: Long) = { commandChain.headOption match { case Some(node) => node += command case…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
0
votes
2 answers

how to combine the results Future[ Option[ T ] ] into Seq[ T ]

I have a method def readTree(id: String): Future[Option[CategoryTreeResponse]] and a list of String channels:List[String]. How to iterate and combine all the results into a non Future Sequence ? such as : def readAllTrees():…
Raymond Chenon
  • 11,482
  • 15
  • 77
  • 110
0
votes
2 answers

Scala : Pattern matching with Option[Foo] and parameter of Foo

How can rewrite the following to make it more 'Scala way' or use just one match? case class Foo(bar: Any) val fooOpt = Some(Foo("bar as String")) def isValid(p: Any) = p match { case _ @ (_: String | _: Int) => true case _ => false } //Is it…
Bruno
  • 252
  • 1
  • 8