aivi.result
Utilities for working with Result E A — a value that is either a success (Ok) or a failure (Err). All functions are pure and can be freely composed with pipes.
use aivi.result (
isOk
isErr
mapErr
withDefault
orElse
flatMap
flatten
toOption
toList
map
mapBoth
fold
fromOption
)At a glance
| Function | Type | Use it for |
|---|---|---|
isOk | Result E A -> Bool | Check for success |
isErr | Result E A -> Bool | Check for failure |
mapErr | (E1 -> E2) -> Result E1 A -> Result E2 A | Transform only the error side |
withDefault | A -> Result E A -> A | Unwrap with a fallback value |
orElse | Result E A -> Result E A -> Result E A | Fall back to another result |
flatMap | (A -> Result E B) -> Result E A -> Result E B | Chain Result-returning steps |
flatten | Result E (Result E A) -> Result E A | Remove one layer of nesting |
toOption | Result E A -> Option A | Drop the error and keep only success |
toList | Result E A -> List A | Turn Ok into a one-item list |
map | (A -> B) -> Result E A -> Result E B | Transform only the success side |
mapBoth | (E1 -> E2) -> (A -> B) -> Result E1 A -> Result E2 B | Transform both error and success |
fold | (E -> B) -> (A -> B) -> Result E A -> B | Collapse a result to one output type |
fromOption | E -> Option A -> Result E A | Turn None into an explicit error |
isOk
Returns True if the result is Ok.
Type: opt:(Result E A) -> Bool
use aivi.result (isOk)
type Result Text Int -> Bool
func succeeded =
|> isOkisErr
Returns True if the result is Err.
Type: opt:(Result E A) -> Bool
use aivi.result (isErr)
type Result Text Int -> Bool
func failed =
|> isErrmapErr
Transforms the error inside Err, leaving Ok untouched.
Type: transform:(E1 -> E2) -> opt:(Result E1 A) -> Result E2 A
use aivi.result (mapErr)
type Text -> Int
func toCode = message =>
42
type Result Text Int -> (Result Int Int)
func withErrorCode =
|> mapErr toCodewithDefault
Extracts the value from Ok, or returns the fallback if Err.
Type: fallback:A -> opt:(Result E A) -> A
use aivi.result (withDefault)
type Result Text Int -> Int
func safeScore =
|> withDefault 0orElse
Returns the result unchanged if it is Ok, otherwise returns the fallback result.
Type: fallback:(Result E A) -> opt:(Result E A) -> Result E A
use aivi.result (orElse)
type Result Text Int -> (Result Text Int) -> (Result Text Int)
func withFallback = primary! secondary
|> orElse secondaryflatMap
Chains a Result-returning function over an Ok value. Propagates Err without calling the function.
Type: next:(A -> Result E B) -> opt:(Result E A) -> Result E B
use aivi.result (flatMap)
type Int -> (Result Text Int)
func ensurePositive = n => n > 0
T|> Ok n
F|> Err "must be positive"
type Result Text Int -> (Result Text Int)
func validateCount =
|> flatMap ensurePositiveflatten
Removes one layer of nesting from a Result E (Result E A).
Type: opt:(Result E (Result E A)) -> Result E A
use aivi.result (flatten)
type Result Text (Result Text Int) -> (Result Text Int)
func unwrapNested =
|> flattentoOption
Converts a Result to an Option, discarding the error. Ok value becomes Some value; Err becomes None.
Type: opt:(Result E A) -> Option A
use aivi.result (toOption)
type Result Text Int -> (Option Int)
func justValue =
|> toOptiontoList
Converts Ok value to a one-element list, or Err to an empty list.
Type: opt:(Result E A) -> List A
use aivi.result (toList)
type Result Text Int -> (List Int)
func resultItems =
|> toListmap
Transforms the value inside Ok using a function, leaving Err untouched.
Type: transform:(A -> B) -> opt:(Result E A) -> Result E B
use aivi.result (map)
type Int -> Int
func double = . * 2
type Result Text Int -> (Result Text Int)
func doubleResult =
|> map doublemapBoth
Transforms both sides of a Result simultaneously: onErr for Err, onOk for Ok.
Type: onErr:(E1 -> E2) -> onOk:(A -> B) -> opt:(Result E1 A) -> Result E2 B
use aivi.result (mapBoth)
type Text -> Int
func toCode = message =>
500
type Int -> Int
func double = . * 2
type Result Text Int -> (Result Int Int)
func normalise =
|> mapBoth toCode doublefold
Collapses a Result to a single value by applying onOk to Ok or onErr to Err.
Type: onErr:(E -> B) -> onOk:(A -> B) -> opt:(Result E A) -> B
use aivi.result (fold)
type Text -> Int
func zero = ignored =>
0
type Int -> Int
func identity = .
type Result Text Int -> Int
func resultToInt =
|> fold zero identityfromOption
Converts an Option to a Result. Some value becomes Ok value; None becomes Err error.
Type: error:E -> opt:(Option A) -> Result E A
use aivi.result (fromOption)
type Option Int -> (Result Text Int)
func requireAge = opt =>
fromOption "Age is required" opt