How to assign an option only if it is missing

In ruby, this can be done like this:

first ||= number

and first get the value of the number only if it is nil

How to do similar in scala?

So far I have come up with this, and it doesn’t look as good as in a ruby.

var first: Option[Int] = None
...
first = if (first == None) Option(number) else first
// or
first = Some(first.getOrElse(number))
+4
source share
3 answers

This is not best practice or something else, but here you can do something similar:

scala> implicit class RubyFriend[T](val value: Option[T]) extends AnyVal {
     |   def ||=(alt: T): Option[T] = value orElse Some(alt)
     | }
defined class RubyFriend

scala> var something: Option[Int] = None
something: Option[Int] = None

scala> something = something ||= 5
something: Option[Int] = Some(5)

scala> something ||= 4
res11: Option[Int] = Some(5)

scala> (None: Option[Int]) ||= 3
res12: Option[Int] = Some(3)

We create an implicit conversion from Optionto RubyFriendthat has a method ||=. This is the pimp of my library. The only difference that it cannot really assign to the original variable, because in Scala the assignment cannot be overloaded, as far as I know.

In general, try to avoid vars in Scala.

:

scala> import scala.language.implicitConversions
import scala.language.implicitConversions

scala> implicit class RubyFriend[T](var value: Option[T]) {
     |   def ||=(alt: T): Unit = value = value orElse Some(alt)
     | }
defined class RubyFriend

scala> implicit def ruby2Option[T](smth: RubyFriend[T]): Option[T] = smth.value
ruby2Option: [T](smth: RubyFriend[T])Option[T]

scala> var ropt: RubyFriend[Int] = None
ropt: RubyFriend[Int] = RubyFriend@2d483fef

scala> ropt.value
res0: Option[Int] = None

scala> val opt: Option[Int] = ropt
opt: Option[Int] = None

scala> ropt ||= 6

scala> ropt.value
res2: Option[Int] = Some(6)

scala> val opt: Option[Int] = ropt
opt: Option[Int] = Some(6)

scala> ropt ||= 7

scala> ropt.value
res4: Option[Int] = Some(6)

Option . (yikes).

- , Scala :).

"" , @senia:

scala> implicit class RubyFriend[T](val value: Option[T]) extends AnyVal {
     |   def ||(alt: T): Option[T] = value orElse Some(alt)
     | }
defined class RubyFriend

scala> var something: Option[Int] = None
something: Option[Int] = None

scala> something ||= 5

scala> something
res1: Option[Int] = Some(5)

scala> something ||= 6

scala> something
res3: Option[Int] = Some(5)
+3

scalaz:

import scalaz._, Scalaz._

none[Int] <+> 1.some
// Some(1)

2.some <+> 1.some
// Some(2)

a <+>= b a = a <+> b:

var i = none[Int] // i == None
i <+>= 1.some // i == Some(1)
i <+>= 2.some // i == Some(1)

. [F [_]] .

Option plus orElse.

(List, Vector, Stream ..) plus append:

Vector(1, 2) <+> Vector(3, 4)
// Vector(1, 2, 3, 4)

Option[T] T , , :

implicit class PlusT[T, M[_]: Plus : Applicative](m: M[T]) {
  def ?+?(t: T) = m <+> t.point[M]
}

none[Int] ?+? 1
// Some(1)

2.some ?+? 1
// Some(2)

var i = none[Int] // i == None
i ?+?= 1 // i == Some(1)
i ?+?= 2 // i == Some(1)

|| ||=, Ruby, - || Boolean.

+5

I don’t know why another poster deleted my answer, but I would go for this approach, also suggested in the comments:

first = first orElse Some(number)
+3
source

All Articles