scala-option

Simplifying Option[Boolean] expression in Scala

微笑、不失礼 提交于 2021-01-02 07:52:28
问题 I have code like that: optionBoolean.getOrElse(false) && otherOptionBoolean.getOrElse(false) And Scalastyle tells me that it can be simplified. How? 回答1: You can try the following: Seq(optionBoolean, otherOptionBoolean).forall(_.contains(true)) In Scala 2.13 (it is very similar in prior versions) the forall method is located at IterableOnce, and its implementation is: def forall(p: A => Boolean): Boolean = { var res = true val it = iterator while (res && it.hasNext) res = p(it.next()) res }

scala return on first Some in list

只愿长相守 提交于 2020-01-22 09:08:50
问题 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, flatten throws away all the None's and find returns the first element of the list if any. This seems a bit hacky to me. Im thinking that there might exist some for-comprehension or similar that will do this a bit less wasteful or more clever. For example: I dont need any subsequent answers if myfun returns

scala return on first Some in list

会有一股神秘感。 提交于 2020-01-22 09:05:08
问题 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, flatten throws away all the None's and find returns the first element of the list if any. This seems a bit hacky to me. Im thinking that there might exist some for-comprehension or similar that will do this a bit less wasteful or more clever. For example: I dont need any subsequent answers if myfun returns

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

谁都会走 提交于 2020-01-06 05:26:33
问题 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 invalid. ERROR This is the error in the implementation below: found : (Option[Seq[A]], Option[A]) => Option[Seq[A]] [error] required: (Option[Any], Option[Any]) => Option[Any] [error] s.fold(init)(liftOptionItem[A]) IMPLEMENTATION def liftOptionItem[A](acc:

Apache Spark: dealing with Option/Some/None in RDDs

时间秒杀一切 提交于 2020-01-03 12:44:26
问题 I'm mapping over an HBase table, generating one RDD element per HBase row. However, sometimes the row has bad data (throwing a NullPointerException in the parsing code), in which case I just want to skip it. I have my initial mapper return an Option to indicate that it returns 0 or 1 elements, then filter for Some , then get the contained value: // myRDD is RDD[(ImmutableBytesWritable, Result)] val output = myRDD. map( tuple => getData(tuple._2) ). filter( {case Some(y) => true; case None =>

Why is foreach better than get for Scala Options?

不羁的心 提交于 2019-12-31 08:42:43
问题 Why using foreach , map , flatMap etc. are considered better than using get for Scala Options? If I use isEmpty I can call get safely. 回答1: Well, it kind of comes back to "tell, don't ask". Consider these two lines: if (opt.isDefined) println(opt.get) // versus opt foreach println In the first case, you are looking inside opt and then reacting depending on what you see. In the second case you are just telling opt what you want done, and let it deal with it. The first case knows too much about

Scala Mutable Option?

一曲冷凌霜 提交于 2019-12-23 11:47:43
问题 I want something like this: private val cachedResponse = mutable.Option.empty[A] def get: A = cachedResponse getOrElseUpdate db.findModel() def update: Unit = { db.updateModel cachedResponse.empty() // set it to None/Option.empty } I am not looking for a generic HashMap based memoization like this. I tried implementing it using a var Option[A] but it did not look very idiomatic to me: private var cachedResponse: Option[A] = None def get: A = cachedResponse getOrElse { cachedResponse = Option

Could/should an implicit conversion from T to Option[T] be added/created in Scala?

。_饼干妹妹 提交于 2019-12-20 10:30:15
问题 Is this an opportunity to make things a bit more efficient (for the prorammer): I find it gets a bit tiresome having to wrap things in Some , e.g. Some(5) . What about something like this: implicit def T2OptionT( x : T) : Option[T] = if ( x == null ) None else Some(x) 回答1: You would lose some type safety and possibly cause confusion. For example: val iThinkThisIsAList = 2 for (i <- iThinkThisIsAList) yield { i + 1 } I (for whatever reason) thought I had a list, and it didn't get caught by the

Extracting field from Some in Scala

自古美人都是妖i 提交于 2019-12-18 09:33:34
问题 I know the fact that a Some object can be a None or one of the objects I passed. What is the ideal way of extracting a field from the Some object given the fact that it is not None? I have made a class 'At' that has 'date' as one of its fields. Since the Some class has a mixin with Product trait, the following works fine: (An object with return type Some(At)).productElement(0).asInstanceOf[At].date But is there an ideal way to do this? 回答1: There are several safe ways to work with Option . If

Scala: Making implicit conversion A->B work for Option[A] -> Option[B]

梦想的初衷 提交于 2019-12-17 22:50:13
问题 I'm trying to write a function which re-uses the implicit conversions which I have for Object A -> Object B when they are wrapped in an Option in a generic way so that Option[A] -> Option[B] conversions also work. What I've come up with is: implicit def fromOptionToOption[A, B](from: Option[A])(implicit conversion: (A) => B): Option[B] = from.map(conversion(_)) This works when I assign a Some(..) to a value but not when I assign an Option val; see the following console output: scala> trait T