xeonie
xeonie

Reputation: 3

Taking the Vector out of Some

In my code, after an API call, I am getting

Some(Vector(72981, 72982)).

I need to get the vector out of Some so that I modify the vector. Tried many things but no result.

Upvotes: 0

Views: 167

Answers (1)

Gabriele Petronella
Gabriele Petronella

Reputation: 108159

Using map you can modify what's in the Option (i.e. the Some in this case)

Some(Vector(72981, 72982)).map(vector => // do something with vector)
// Some(modifiedVector)

this will return the modified vector inside an Option.

If you want to extract the value from the Option, you can use getOrElse

val v = Some(Vector(72981, 72982)).getOrElse(/* a fallback value */)

or a match

val opt = Some(Vector(72981, 72982))
opt match {
  case Some(vector) => // do something with vector 
  case None => // vector doesn't exist
}

Upvotes: 3

Related Questions