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

scala merge option sequences

Want to merge val A = Option(Seq(1,2)) and val B = Option(Seq(3,4)) to yield a new option sequence val C = Option(Seq(1,2,3,4)) This val C = Option(A.getOrElse(Nil) ++ B.getOrElse(Nil)), seems faster and more idiomatic than val C =…
Nat G
  • 191
  • 1
  • 15
1
vote
1 answer

make play-json read the empty string as None for a type of Option[T]

I'm attempting to parse json from the GitHub API with play-json, and encountering a problem with the merge_commit_sha field on Pull Requests (incidentally, I know this field is deprecated, but don't want to discuss that in this parsing problem!).…
Roberto Tyley
  • 24,513
  • 11
  • 72
  • 101
1
vote
4 answers

How to convert an Option[String]to List [Int]in Scala

I'm able to convert Option[String] to List[String] with the following code : def convertOptionToListString(a: Option[String]): List[String] = { return a.toList.flatMap(_.split(",")) // ex : ("value1", "value2", "value3", etc) } But how can i…
user708683
  • 500
  • 3
  • 9
  • 26
1
vote
1 answer

Scala - Option Type Var Manipulation

I am working on an online exercise practicing Options and threads, both of which I have very little experience. The online exercise comes with a test suite, so right now I am trying to get my Option test cases to pass before I move on to the thread…
lmcphers
  • 468
  • 3
  • 18
1
vote
0 answers

Creating Java object off Scala Case Class : Using None (Option)

I'm trying to create a new Java object based off a Scala case class. All seemed to be fine, including creating new objects within the first initialisation, until I needed to fill in attributes of type Option[X] (where X is some other type. It varies…
GroomedGorilla
  • 920
  • 2
  • 10
  • 30
1
vote
2 answers

Are Maybes a good pattern for scala?

For a while I have been struggling to integrate scala with java methods that might return null. I came up with the following utility which helps a lot: // produce an Option, nulls become None object Maybe { def apply[T](t:T) = if (t==null)…
Fred Haslam
  • 8,873
  • 5
  • 31
  • 31
1
vote
2 answers

Scala: How to return a Some or Option

I have the following piece of code that I am trying to enhance: I am using the java.nio.file package to represent a directory or a file as a Path. So here goes: import java.nio.file.{Paths,DirectoryStream,Files, …
user3825558
  • 585
  • 1
  • 8
  • 24
1
vote
2 answers

Some with multiple arguments creates Option of Tuple

How does the following code work in Scala ? scala> Some(2,true,3, false) res13: Some[(Int, Boolean, Int, Boolean)] = Some((2,true,3,false)) I don't see an apply method defined for some which can take more than one argument.
aakashns
  • 115
  • 5
1
vote
1 answer

Scala Future with Option()

I'm creating three actor tasks using future, and then trying to collect all three when finished. The current code is the following: implicit val timeout = Timeout(5.seconds) val result1 = actor1 ? DataForActor(data) val result2 = actor2 ?…
user3370773
  • 435
  • 6
  • 11
1
vote
2 answers

Option method like `collect` that returns Unit?

Is there a method on the Option class that works like collect (it takes a partial function), but has a return type of Unit? That is: map is to collect as foreach is to ?. Sorry, I'm new to Scala--I've looked over the Option docs and did a little…
Jacob Brown
  • 7,221
  • 4
  • 30
  • 50
1
vote
2 answers

Scala Method Argument: Option of Collection or Default Value

I have method that takes a Map of [Int, MyClass] as an argument. Something like this: myMethod(someMap : Map[Int, MyClass]) However, the someMap might be not always be present (null in Java world and None in Scala). Which of the following is a…
exifguy
  • 650
  • 7
  • 16
1
vote
1 answer

ClassCastException when creating actor in for comprehension

I have an actor that creates children actors of type Child1 in this example. Child1 constructor takes two String's which are extracted from variables that reside in SomeTrait that is mixed into SomeActor isntance. trait SuperTrait { lazy val…
goral
  • 1,275
  • 11
  • 17
1
vote
2 answers

How to simplify this code, which uses Option.fold in Scala?

Suppose I have the following functions def foo(x: Int): Option[String] = ... def bar(s: String): Option[String] = ... and def f(s: String): String = ... def g(s: String): String = ... I would like to compose all these functions as follows: def…
Michael
  • 41,026
  • 70
  • 193
  • 341
1
vote
1 answer

Scalaz .option - what is this shorthand for?

I'm seeing the following code: val a = (x: Int) => (x == 1).option(doSomethingUnrealiable(1)) Is this equivalent to: val a = (x: Int) => if (x == 1) Option(doSomethingUnrealiable(1)) I ask because I'm struggling to find doco on Scalaz for this…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
1
vote
3 answers

"Or"-ing two Options in Scala?

I want to do something like this: def or[A](x: Option[A], y: Option[A]) = x match { case None => y case _ => x } What is the idiomatic way to do this? The best I can come up with was Seq(x, y).flatten.headOption
pathikrit
  • 32,469
  • 37
  • 142
  • 221