Value Result
Result is a container, which holds either 'value' or 'problem', which explains why value is not available.
Unlike Opt, Result is never void, even if there is no value, then, there must be a problem.
val r1: Result[Int] = "123".toIntResult
r1.tp // Prints Result(123)
val r2: Result[Int] = "ABC".toIntResult
r2.tp // Prints Result(Problem(For input string: "ABC"))
- Source
- __.scala
Def
Check contains
Check contains
Returns 'true' if result contains given value
'false' - otherwise
val r : Result[String] = "foo"
r.contains("foo").tp // Prints: true
r.contains("bar").tp // Prints: false
- Source
- __.scala
Reversed filter
Reversed filter
If the result value does satisfy given predicate, the problem is created with given function
The result with problem is not affected at all
val r : Result[String] = "foo"
r.drop(_.length < 2, "'" + _ + "' is too short").tp // Prints Result(foo)
r.drop(_.length < 4, "'" + _ + "' is too short").tp // Prints Result(Problem('foo' is too short))
- Source
- __.scala
Process problem
Process problem
Executes given function if there is a problem
Returns option itself
- Source
- __.scala
Process value
Process value
Executes given function with value
Does nothing if there is a problem
Returns result itself
- Source
- __.scala
Problem check
Problem check
Returns true
if there is a problem, false
if there is a value
- Source
- __.scala
Value check
Value check
Returns true
if there is a value, false
if there is a problem
- Source
- __.scala
Convert value
Convert value
Creates new result with value converted by the given function
Results with problem are cast to a new type without change
Result("Abc").map(_.length).tp // Prints: Result(3)
- Source
- __.scala
Map result
Map result
Results with problem are cast to a new type without change
Otherwise, value is converted to a new result by given function.
- Source
- __.scala
Value or default
Value or default
Returns result value, or if there is a problem, then given default value
- Source
- __.scala
Default result
Default result
Returns this result if it has value or given result otherwise
- Source
- __.scala
Optional problem
Optional problem
Returns problem option, which is void if there is a value
- Source
- __.scala
Process value or problem
Process value or problem
Executes either given function for value or problem
Returns result itself
- Source
- __.scala
Optionally fix problem
Optionally fix problem
Results with value are not affected
Given function is run with problem and optional value is returned. If option is not void, result value is restored
- Source
- __.scala
Filter
Filter
If the result value does not satisfy given predicate, the problem is created with given function
The result with problem is not affected at all
val r : Result[String] = "foo"
r.take(_.length < 2, "'" + _ + "' is too long").tp // Prints Result(Problem('foo' is too long))
r.take(_.length < 4, "'" + _ + "' is too long").tp // Prints Result(foo)
- Source
- __.scala
Make Scala Try
Make Scala Try
Result is converted to scala.util.Try, which is a natural fit
- Source
- __.scala