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

Scala syntactic sugar for mandatory Option-type function parameters

Is there a syntatic sugar to call a function with multiple parameters of type Option but without default value of None present in any? def func1(param1: Option[String], param2: Option[String]): String = …
enyone
  • 21
  • 1
2
votes
6 answers

Need to convert a Seq[Option[A]] to Option[Seq[A]]

USE CASE I have a list of files that can might have a valid mime type or not. In my code, I represent this using an Option. I need to convert a Seq[Option[T]] to Option[Seq[T]] so that I do not process the list if some of the files are…
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
2
votes
0 answers

Scala Json validation does not result in JsError on Option field

I am using PlayFramework 2.6. I have the following classes case class Account(homePage: URL, name: String) object Account { import implicits.ReadsWrites.urlFormat implicit val format: Format[Account] = Json.format[Account] } And account is…
Alex To
  • 21
  • 2
2
votes
3 answers

Scala : Nested getOrElse

I would like to create a method that takes as arguments an array of options and a default value and return the first non empty option otherwise the default value : def customGetOrElse[T](options : Array[Option[T]], defaultValue : T) : T = { //…
Zied Koubaa
  • 213
  • 1
  • 18
2
votes
2 answers

How to optimize this method in Scala?

Suppose I've got class A with method foo: class A(x: Int) { def foo(): Option[Int] = if (x > 0) Some(x) else None } Now I am writing function bar like this: def bar(as: List[A]): Option[(A, Int)] = for { a <- as.view.find(_.foo.nonEmpty) foo…
Michael
  • 41,026
  • 70
  • 193
  • 341
2
votes
1 answer

Scala options pattern matching alternatives?

I've been coding on Scala for 2 years and I use a lot of Option's in my code. I feel it is very clear, easy to understand and can easily be treated. This is a normal approach: val optString = Some("hello") optString match { case Some(str) =>…
Sohum Sachdev
  • 1,397
  • 1
  • 11
  • 23
2
votes
1 answer

How to correctly handle Option in Spark/Scala?

I have a method, createDataFrame, which returns an Option[DataFrame]. I then want to 'get' the DataFrame and use it in later code. I'm getting a type mismatch that I can't fix: val df2: DataFrame = createDataFrame("filename.txt") match { case…
LucieCBurgess
  • 759
  • 5
  • 12
  • 26
2
votes
1 answer

Spark 2 Datasets of Options

I have a Dataset of Strings that I parse into a Dataset of a case class using a function that can fail ( for instance if the data I try to parse is not usable). For that reason that function returns an Option (Scala). So I end up with a Dataset of…
sam
  • 3,441
  • 2
  • 33
  • 42
2
votes
1 answer

getting a java.io.Serializable from Option[..] in Scala

(just started with Scala a few weeks ago, so bear with me) Reading/trying out this little article here, ran into some surprises http://danielwestheide.com/blog/2012/12/19/the-neophytes-guide-to-scala-part-5-the-option-type.html After defining a case…
alexakarpov
  • 1,848
  • 2
  • 21
  • 39
2
votes
0 answers

How can I apply Java functions to Scala Option in Java code

Given Scala Options Option[String], Option[Long], Option[Boolean] and given Java consumers for String, long, boolean I want to apply these cleanly, with static type checks, in Java code. The problem is, Option[Long] and Option[Boolean] become…
2
votes
1 answer

scala: complicated flatMap on a tuple of Options

My data type is: List[(Option[Set[Long]], Option[Set[(Long, Long)]])] (i.e. a List of a 2-tuple of Options, one is for a Set of Long, one is for a Set of a 2-tuple of Longs). E.g.: val l = List((Option(Set(1L, 2L, 3L)), Option(Set((4L, 5L), (6L,…
Giora Simchoni
  • 3,487
  • 3
  • 34
  • 72
2
votes
2 answers

Drools rule validating objects within Scala Option type

(Mandatory Newbie Disclaimer) I'm trying to write a rule that fires whenever an object within a (scala) list matches a condition. The issue here is that the list is actually an Option(List[TypeA])... (Also, I realise it isn't best practice to store…
GroomedGorilla
  • 920
  • 2
  • 10
  • 30
2
votes
4 answers

Scala idiom to find first Some of Option from iterator

I have an iterator of Options, and would like to find the first member that is: Some and meets a predicate What's the best idiomatic way to do this? Also: If an exception is thrown along the way, I'd like to ignore it and move on to the next…
SRobertJames
  • 8,210
  • 14
  • 60
  • 107
2
votes
3 answers

Appending list of optional elements

I need to make a sequence that, given a list containing optional list of strings, concatenates all of them to make a new list. This how my code looks like: res.foldLeft(Seq[Option[Seq[String]]]())((acc, v) => v match { case Some(x) =>…
Core_Dumped
  • 4,577
  • 11
  • 47
  • 71
2
votes
3 answers

Scala: Using Option for key/value pairs in a list

I'm trying to write the get method for a key, value pair implemented using a list. I want to use the Option type as I heard its good for this but I'm new to Scala and I'm not really sure how to use it in this case... This is as far as I got, only…
JrPtn
  • 83
  • 2
  • 8