scala-option Questions

10

Solved

How would you find minValue below? I have my own solution but want to see how others would do it. val i1: Option[Int] = ... val i2: Option[Int] = ... val defaultValue: Int = ... val minValue = ? ...
Lubra asked 28/9, 2012 at 12:22

4

Solved

Suppose I need to convert Option[Int] to Either[String, Int] in Scala. I'd like to do it like this: def foo(ox: Option[Int]): Either[String, Int] = ox.fold(Left("No number")) {x => Right(x)} ...
Stipe asked 10/1, 2016 at 14:19

6

Solved

I have a method that should convert a list to an Option of an object, or None if the list is empty. def listToOption(myList: List[Foo]): Option[Bar] = { if(myList.nonEmpty) Some(Bar(myList)) els...
Marylou asked 30/1, 2015 at 20:42

7

Solved

I need to return values, and when someone asks for a value, tell them one of three things: Here is the value There is no value We have no information on this value (unknown) case 2 is subtly d...
Pneumatometer asked 15/11, 2009 at 16:47

4

Solved

If I have something like a List[Option[A]] and I want to convert this into a List[A], the standard way is to use flatMap: scala> val l = List(Some("Hello"), None, Some("World")) l: List[Option[...
Furan asked 25/11, 2009 at 15:0

4

Solved

If I have a List[Option[A]] in Scala, what is the idiomatic way to filter out the None values? One way is to use the following: val someList: List[Option[String]] = List(Some("Hello"), None, Some...
Podgy asked 11/4, 2012 at 10:56

8

Solved

How can I (best) convert an Option returned by a method call into a Try (by preference, although an Either or a scalaz \/ or even a Validation might be OK) including specifying a Failure value if a...
Tb asked 8/7, 2013 at 8:5

6

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 p...
Prosper asked 28/9, 2018 at 0:49

1

I use upickle for serializing json in scalajs. I need to be able to parse optional fields, represented by a null value and by a missing field (standard json on the web). With OptionPickler, I can ...
Leverick asked 4/11, 2019 at 13:42

5

Solved

Why does this construction cause a Type Mismatch error in Scala? for (first <- Some(1); second <- List(1,2,3)) yield (first,second) <console>:6: error: type mismatch; found : List[(I...

4

Solved

I have code like that: optionBoolean.getOrElse(false) && otherOptionBoolean.getOrElse(false) And Scalastyle tells me that it can be simplified. How?
Counterirritant asked 3/12, 2020 at 9:41

5

Solved

I am trying to find a cleaner way to express code that looks similar to this: def method1: Try[Option[String]] = ??? def method2: Try[Option[String]] = ??? def method3: Try[Option[String]] = ??? ...
Turco asked 19/12, 2013 at 23:24

9

Solved

I have an Option[String]. I want to check if there is a string exists and if it's exists its not blank. def isBlank( input : Option[String]) : Boolean = { input.isEmpty || input.filter(_.t...
Knurly asked 6/6, 2014 at 16:21

4

Solved

Suppose I have a method session.get(str: String): String but you don't know whether it will return you a string or a null, because it comes from Java. Is there an easier way to treat this in Scal...
Albie asked 14/1, 2011 at 15:13

5

Solved

This is basically to wrap java factory methods which throw exceptions if the item can't be created based on the inputs. I'm looking for something in the base library like: def exceptionToOption[A...
Epiphytotic asked 4/11, 2011 at 17:29

5

Solved

I want to transform a List[Option[T]] into a Option[List[T]]. The signature type of the function is def lo2ol[T](lo: List[Option[T]]): Option[List[T]] The expected behavior is to map a list that...
Prefix asked 2/4, 2010 at 20:28

5

Solved

This question is the opposite of this question. val x = Some((1, 2)) val (y: Option[Int], z: Option[Int]) = ??? Both pure Scala answers and Scalaz anwers are helpful.
Hydropathy asked 30/1, 2014 at 17:11

2

Solved

At the very end, this article introducing to new Java 8 Optional, states that Optional is not nearly as powerful as Option[T] in Scala (but at least it doesn’t allow wrapping null). The API is ...
Ilium asked 11/2, 2014 at 22:34

3

Solved

I am trying to figure out the best way to refactor the following code to eliminate the use of Option.get(). I know that using the get method is considered bad practice. if (myConnection.isDefined)...
Hipparch asked 21/11, 2018 at 19:1

5

Solved

List.max returns the "largest" element of a list based on some ordering... But if the list is empty you'll get a java.lang.UnsupportedOperationException: empty.max exception. I don't real...
Primary asked 9/12, 2013 at 22:25

5

Solved

I have a list l:List[T1] and currently im doing the following: myfun : T1 -> Option[T2] val x: Option[T2] = l.map{ myfun(l) }.flatten.find(_=>true) The myfun function returns None or Some,...
Crowfoot asked 4/9, 2010 at 22:2

4

Solved

I am trying to get a number out of an xml field ... <Quantity>12</Quantity> ... via Some((recipe \ "Main" \ "Quantity").text.toInt) Sometimes there may not be a value in the xml, ...
Orometer asked 22/5, 2014 at 15:40

3

I have a method that I wrote to enrich person data by performing an API call and adding the enriched data. I have this case class: case class Person(personData: PersonData, dataEnrichment: Option[D...
Snotty asked 2/6, 2018 at 22:27

3

Solved

I want to flatten a Try[Option[T]] into a Try[T] Here is my code def flattenTry[T](t: Try[Option[T]]) : Try[T] = { t match { case f : Failure[T] => f.asInstanceOf[Failure[T]] case Success(e...
Gambol asked 26/11, 2013 at 13:48

1

Solved

I have a map and want to: retrieve value without handling Option log a message when there is no such the key. nice to return a default value ( in addition to log a message) when the key is not p...
Gildea asked 13/3, 2018 at 11:18

© 2022 - 2025 — McMap. All rights reserved.