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
1
vote
1 answer

How to generate json string if the `Json.obj` contains a `None` value?

In playframework: import play.api.libs.json._ val obj = Json.obj( "aaa" -> 111, "bbb" -> Some(222) ) println(obj.toString) Which outputs: {"aaa":111,"bbb":222} But if I change the code to: val obj = Json.obj( "aaa" -> 111, "bbb" ->…
Freewind
  • 193,756
  • 157
  • 432
  • 708
1
vote
3 answers

Override toString behavior for Option[_] in scala

I would prefer to see just the value of the Option (if it were not None) , instead of the following additional Some() noise: List((Some(OP(_)),Some(share),3), (Some(OP(D)),Some(shaara),4), (Some(OP(I)),Some(shaaee),4)) Now, I could write a method…
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560
1
vote
3 answers

Scala Option ||

I want to get the value of one of several Option in scala, like this: def or(a:Option[Int], b:Option[Int]):Option[Int]= if (a.isDefined) a else b val a= Option(1) val b= Option(2) or(a,b).get but I wonder why isn't the || operator defined for…
loopbackbee
  • 21,962
  • 10
  • 62
  • 97
1
vote
1 answer

How to retrieve an Option[List[X]] instead of List[X] from a select statement in Play2!Scala Anorm?

In my Play2 app, I am trying to retrieve a list of users from one of my database table. The query responsible for this may potentially be empty if there is no row in the database matching the criteria (which is the firstName in our case). That is…
kaffein
  • 1,766
  • 2
  • 28
  • 54
0
votes
5 answers

Processing Scala Option[T]

I have a Scala Option[T]. If the value is Some(x) I want to process it with a a process that does not return a value (Unit), but if it is None, I want to print an error. I can use the following code to do this, but I understand that the more…
Ralph
  • 31,584
  • 38
  • 145
  • 282
0
votes
3 answers

scala: updating string value in an option

I have an Option of a string. I want to update the contained value: if(x.isEmpty) { ...another calculation } else { x.map(val => ...update val) } Is this an idiomatic way?
Mandroid
  • 6,200
  • 12
  • 64
  • 134
0
votes
1 answer

Subtracting two Option[Double]

I have two variables that are optional double and I want to consider them as 0 when they are None var x: Option[Double] = Option(0) var y: Option[Double] = Option(0) println("Expected 0 : "+(x ++ y).reduceOption(_ - _)) // 0 x = Option(4) y =…
Ranveer Singh
  • 27
  • 1
  • 7
0
votes
1 answer

Scala Option: Usage of unless with getOrElse

I have an Option of string, say O, which can be empty. And there is a condition, say cond. My requirement is to construct Option of the value inside O if cond is true, else None. I do this: Option.unless(cond)(o.getOrElse(None)) Is this a correct…
Mandroid
  • 6,200
  • 12
  • 64
  • 134
0
votes
0 answers

OutOfMemory error at option2Iterable in scala

I am converting list of strings to list of BigDecimal values in below scala code. But when queryResults list is very large (> 10 million), I'm getting below error. We have some limitations on memory usage and cannot increase memory. Is there any way…
0
votes
1 answer

Extract Option from Future in ActionBuilder

I am implementing authentication based on this Scala Play Authentication example. Therefore I use the following ActionBuilder to build an UserAction. UserAction.scala class UserRequest[A](val user: Option[Admin], request: Request[A]) extends…
tomole
  • 927
  • 3
  • 12
  • 35
0
votes
1 answer

Scala. How to Unmarshall Option values using json spray?

I'm using json-spray library to unmarshall a json array. Here is the code. import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._ import spray.json.DefaultJsonProtocol._ def unmarshalProfiles(response: HttpResponse):…
0
votes
1 answer

Whats the best way of getting a list of a field from a object?

So right now I have a list of a object with a Optional Field like this in scala case class Foo( id: String, description: String, OptionalTag: Option[String], ) What I want is to iterate…
Mylies
  • 396
  • 4
  • 18
0
votes
2 answers

Determine non-empty additional fields in a subclass

Assume I have a trait which looks something like this trait MyTrait { val x: Option[String] = None val y: Option[String] = None } Post defining the trait I extend this trait to a class MyClass which looks something like this case class…
Rakshith
  • 644
  • 1
  • 8
  • 24
0
votes
2 answers

find method in scala's collection

Per the documentation, find function in scala's collection wraps the resulting elements in Option object. In the below, the size operation ends in error; where as the endsWith produces correct result. The second element in the list being null, both…
user3103957
  • 636
  • 4
  • 16
0
votes
2 answers

Process Scala Option type with higher-order functions

There is a class class MyType { def status(): String = "Connected" // demo implementation } I have a val x: Option[Option[MyType]]. I need to process this value like the following: x match { case None => "Error1" case Some(None) => "Error2" …
Loom
  • 9,768
  • 22
  • 60
  • 112