Value Option
Opt is a container, which can hold a single value or be empty
Option can be converted to value with method or
, which also takes default value for the case when option is empty
Option is often used as Boolean, where ''Option'' with value is converted to 'true' and empty option is converted to 'false'
val l: Lookup[Int,String] = ???
val o: Opt[String] = l.getOpt(12)
if(o) println("Value found.")
else println("Value not found.")
- Source
- __.scala
Def
Value filter and converter
Value filter and converter
Discards value if given partial function is not defined
Otherwise value is converted to the function result
val o: Opt[String] = "foo"
o.collect { case v if v.startsWith("a") => "bar" }.tp // Prints: \/
o.collect { case v if v.startsWith("f") => "bar" }.tp // Prints: Opt(bar)
Note: If Opt is empty, it is returned as is
Note: collect
is similar to mapOpt
, but is less efficient, because PartialFunction has to be evaluated twice
- Source
- __.scala
Check contains
Check contains
Returns 'true' if option contains given value
'false' - otherwise
val o : Opt[String] = "foo"
o.contains("foo").tp // Prints: true
o.contains("bar").tp // Prints: false
- Source
- __.scala
Default value
Default value
Does nothing if option already contains a value
For void option, returns a new option with given value
var o : Opt[String] = "foo"
o.default("bar").tp // Prints: Opt(foo)
o=VOID
o.default("bar").tp // Prints: Opt(bar)
- Source
- __.scala
Reversed filter
Reversed filter
Discards value if it satisfies given predicate
val o : Opt[String] = "foo"
o.drop(_.length > 2).tp // Prints: Opt(VOID)
o.drop(_.length > 3).tp // Prints: Opt(foo)
- Source
- __.scala
Reversed value filter
Reversed value filter
Discards value if it is equal to given value
val o : Opt[String] = "foo"
o.dropOnly("foo").tp // Prints: Opt(VOID)
o.dropOnly("bar").tp // Prints: Opt(foo)
- Source
- __.scala
Reversed void filter
Reversed void filter
Discards value if it is void, so the option itself becomes void
val s : Stream[String] =VOID
var o : Opt[Stream[String]] = s
o.tp // Prints: Opt(Stream())
o = o.dropVoid
o.tp // Prints: Opt(VOID)
- Source
- __.scala
Convert or default
Convert or default
This is a synthetic operation:
val o: Opt[String]=VOID
o.foldAs(0)(_.length)
// same as
o.map(_.length) or 0
foldAs is more readable if mapping code is substantial:
o.foldAs(0){ v =>
var l = v.length
if (l > 1000) l = 1000
else if(l < 0) l = 0
l
}
- Source
- __.scala
Process nonexistent value
Process nonexistent value
Executes given function if option is void
Returns option itself
- Source
- __.scala
Process option value
Process option value
Executes given function with option value
Does nothing if option is void
Returns option itself
- Source
- __.scala
Void check
Void check
Returns true
if value is void and false
otherwise
Note: This operation is implicitly available for all types. It is explicit here to indicate efficient implementation.
- Source
- __.scala
Convert value
Convert value
Creates new option with value converted by the given function
Void option allways yeilds void option
"Abc".?.map(_.length) // Prints: Int.Opt(3)
Note. Operation returns specialized options for primitive values
- Source
- __.scala
Optional map
Optional map
Creates new option with value converted by optional function.
Void option allways yeilds void option
"a" .?.mapOpt(s => if(s.length > 2) s.toUpperCase else VOID).tp // Prints Opt(VOID)
"abc".?.mapOpt(s => if(s.length > 2) s.toUpperCase else VOID).tp // Prints Opt(ABC)
"a" .?.mapOpt(s => if(s.length > 2) s.length else VOID).tp // Prints Int.Opt(VOID)
"abc".?.mapOpt(s => if(s.length > 2) s.length else VOID).tp // Prints Int.Opt(3)
"abc".?.mapOpt{
case s if s.length > 2 => s.toUpperCase
case _ => VOID
}.tp
Note. In case of matching, the last default case must be given, so it becomes regular (not a partial) function
- Source
- __.scala
Mix two option values
Mix two option values
If either option is void, the void option is returned
Otherwise, the given function is applied with both values, resulting in a valued option
val io: Int.Opt = 4
var so: Opt[String]=VOID
so.mix(io, _ * _).tp // Prints Opt(VOID)
so = "abc_"
so.mix(io, _ * _).tp // Prints Opt(abc_abc_abc_abc_)
- Source
- __.scala
Not void check
Not void check
Returns true
if value is not void and false
otherwise
Note: This operation is implicitly available for all types. It is explicit here to indicate efficient implementation.
- Source
- __.scala
Value or default
Value or default
Returns option value, or if option is void, given default value
var o : Opt[String] = "foo"
(o or "bar").tp // Prints foo
o=VOID
(o or "bar").tp // Prints bar
- Source
- __.scala
Default option
Default option
Returns this option if it is not void or given option otherwise
var o : Opt[String] = "foo"
var o2 : Opt[String] = "bar"
(o orOpt o2).tp // Prints Opt(foo)
o=VOID
(o orOpt o2).tp // Prints Opt(bar)
- Source
- __.scala
Process value or no value
Process value or no value
Executes given function with option value or second given function if option is void
Returns option itself
- Source
- __.scala
Make specialized option
Make specialized option
Converts this option to specialized on primitive type
The operation will not compile if converting to a reference type
var o : Opt[Int] = 12
var io : Int.Opt = o.raw
- Source
- __.scala
Filter
Filter
Discards value if it does not satisfy given predicate
val o : Opt[String] = "foo"
o.take(_.length < 2).tp // Prints: Opt(VOID)
o.take(_.length > 2).tp // Prints: Opt(foo)
- Source
- __.scala
Value filter
Value filter
Discards value if it is not equal to given value
val o : Opt[String] = "foo"
o.takeOnly("foo").tp // Prints: Opt(foo)
o.takeOnly("bar").tp // Prints: Opt(VOID)
- Source
- __.scala
Value filter and type converter
Value filter and type converter
Discards value if it does not belong to the given type
Note, the result is mapped to the given type
val o: Opt[Any] = "1"
println(o.takeType[String]) // Prints: Opt(1)
println(o.takeType[Int]) // Prints: \/
Note: If Opt is empty, it is returned as is
- Source
- __.scala