Is there an idiomatic convention for achieving this effect ("orElse") with the Scala library:
How should the definition of that interface look like?
The cleanest way I could think to do this is:
Option(List("A", "B', "C")).filterNot(_.isEmpty).getOrElse(List("Default"))
If you find yourself doing this a lot it's probably worth writing a pimp for it.
On Sat, Mar 10, 2012 at 6:54 AM, JND <jndev...@gmail.com> wrote:
> Is there an idiomatic convention for achieving this effect ("orElse") withThe cleanest way I could think to do this is:
> the Scala library:
>
> List("A", "B", "C").orElse("Default element") = List("A", B", "C")
> List[String]().orElse("Default element") = List("Default element")
> Nil || "Default element" = List("Default element")
Option(List("A", "B', "C")).filterNot(_.isEmpty).getOrElse(List("Default"))
If you find yourself doing this a lot it's probably worth writing a pimp for it.
See Scalaz.
Option.orElse generalises to a semigroup and a default element is the identity for any monoid.
See Scalaz.
Option.orElse generalises to a semigroup and a default element is the identity for any monoid
I'm not sure what you mean. Option.orElse and same again but flipped both form a monoid with an identity of None. They might be called "the first option monoid" and "the last option monoid" or something like that.
Do you mean something else?