scala-option

How to get an Option from index in Collection in Scala?

霸气de小男生 提交于 2019-11-29 02:01:06
问题 Is there a way, only using the Scala collection API, to get an Option in a List when trying to get an element by its index? I'm looking for the equivalent of this function, does it exist? def optionalValue[T](l: List[T], index: Int) = { if (l.size < (index+1)) None else Some(l(index)) } Thanks 回答1: Yes, you can lift your collection to a function Int => Option[A] : scala> List(1,2,3).lift res0: Int => Option[Int] = <function1> scala> List(1,2,3).lift(9) res1: Option[Int] = None 来源: https:/

How to flatten list of options using higher order functions?

冷暖自知 提交于 2019-11-28 22:34:51
Using Scala 2.7.7: If I have a list of Options, I can flatten them using a for-comprehension: val listOfOptions = List(None, Some("hi"), None) listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None) scala> for (opt <- listOfOptions; string <- opt) yield string res0: List[java.lang.String] = List(hi) I don't like this style, and would rather use a HOF. This attempt is too verbose to be acceptable: scala> listOfOptions.flatMap(opt => if (opt.isDefined) Some(opt.get) else None) res1: List[java.lang.String] = List(hi) Intuitively I would have expected the following to work, but

Why doesn't Option have a fold method?

喜欢而已 提交于 2019-11-28 19:09:22
I wonder why scala.Option doesn't have a method fold like this defined: fold(ifSome: A => B , ifNone: => B) equivalent to map(ifSome).getOrElse(ifNone) Is there no better than using map + getOrElse ? Debilski You can do: opt foldLeft (els) ((x, y) => fun(x)) or (els /: opt) ((x,y) => fun(x)) (Both solutions will evaluate els by value, which might not be what you want. Thanks to Rex Kerr for pointing at it.) Edit: But what you really want is Scalaz’s catamorphism cata (basically a fold which not only handles the Some value but also maps the None part, which is what you described) opt.cata(fun,

Combining 2 Options into 1

只愿长相守 提交于 2019-11-28 13:17:02
Is there a predefined function x in Scala that combine 2 Options so that Some(a) x None => Some(a) None x Some(b) => Some(b) None x None => None Yes, this is the orElse method. It chooses the first defined value, or None if neither is defined. scala> Some(1) orElse None res0: Option[Int] = Some(1) scala> None orElse Some(1) res1: Option[Int] = Some(1) scala> None orElse None res2: Option[Nothing] = None scala> Some(1) orElse Some(2) res3: Option[Int] = Some(1) In the question comments, you mention you can't have Some(a) and Some(b) , so what you really have is Option[Either[Int,Int]] . In that

How do you stop building an Option[Collection] upon reaching the first None?

若如初见. 提交于 2019-11-28 00:55:14
问题 When building up a collection inside an Option , each attempt to make the next member of the collection might fail, making the collection as a whole a failure, too. Upon the first failure to make a member, I'd like to give up immediately and return None for the whole collection. What is an idiomatic way to do this in Scala? Here's one approach I've come up with: def findPartByName(name: String): Option[Part] = . . . def allParts(names: Seq[String]): Option[Seq[Part]] = names.foldLeft(Some(Seq

How to transform Scala collection of Option[X] to collection of X

爱⌒轻易说出口 提交于 2019-11-27 18:39:09
I'm starting to explore Scala, and one of the things I'm intrigued by is the Option type and the promise of being able to eliminate null related errors. However I haven't been able to work out how to transform a list (or other collection) of, say, Option[String] , to a collection of String (obviously filtering out any values that are None ). In other words, how do I get from this: List[Option[Int]] = List(Some(1)) ... to this: List[Int] = List(1) I'm using Scala 2.8 if that has any impact on the answer. val list1 = List(Some(1), None, Some(2)) val list2 = list1.flatten // will be: List(1,2)

How to flatten list of options using higher order functions?

烈酒焚心 提交于 2019-11-27 13:28:48
问题 Using Scala 2.7.7: If I have a list of Options, I can flatten them using a for-comprehension: val listOfOptions = List(None, Some("hi"), None) listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None) scala> for (opt <- listOfOptions; string <- opt) yield string res0: List[java.lang.String] = List(hi) I don't like this style, and would rather use a HOF. This attempt is too verbose to be acceptable: scala> listOfOptions.flatMap(opt => if (opt.isDefined) Some(opt.get) else None

Combining 2 Options into 1

亡梦爱人 提交于 2019-11-27 07:35:22
问题 Is there a predefined function x in Scala that combine 2 Options so that Some(a) x None => Some(a) None x Some(b) => Some(b) None x None => None 回答1: Yes, this is the orElse method. It chooses the first defined value, or None if neither is defined. scala> Some(1) orElse None res0: Option[Int] = Some(1) scala> None orElse Some(1) res1: Option[Int] = Some(1) scala> None orElse None res2: Option[Nothing] = None scala> Some(1) orElse Some(2) res3: Option[Int] = Some(1) 回答2: In the question

How to transform Scala collection of Option[X] to collection of X

拈花ヽ惹草 提交于 2019-11-27 04:19:00
问题 I'm starting to explore Scala, and one of the things I'm intrigued by is the Option type and the promise of being able to eliminate null related errors. However I haven't been able to work out how to transform a list (or other collection) of, say, Option[String] , to a collection of String (obviously filtering out any values that are None ). In other words, how do I get from this: List[Option[Int]] = List(Some(1)) ... to this: List[Int] = List(1) I'm using Scala 2.8 if that has any impact on

Type Mismatch on Scala For Comprehension

廉价感情. 提交于 2019-11-26 12:54:39
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[(Int, Int)] required: Option[?] for (first <- Some(1); second <- List(1,2,3)) yield (first,second) If I switch the Some with the List it compiles fine: for (first <- List(1,2,3); second <- Some(1)) yield (first,second) res41: List[(Int, Int)] = List((1,1), (2,1), (3,1)) This also works fine: for (first <- Some(1); second <- Some(2)) yield (first,second) For comprehensions are converted into calls to the map or