Skip to content

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.

aivi
use aivi.result (
    isOk
    isErr
    mapErr
    withDefault
    orElse
    flatMap
    flatten
    toOption
    toList
    map
    mapBoth
    fold
    fromOption
)

At a glance ​

FunctionTypeUse it for
isOkResult E A -> BoolCheck for success
isErrResult E A -> BoolCheck for failure
mapErr(E1 -> E2) -> Result E1 A -> Result E2 ATransform only the error side
withDefaultA -> Result E A -> AUnwrap with a fallback value
orElseResult E A -> Result E A -> Result E AFall back to another result
flatMap(A -> Result E B) -> Result E A -> Result E BChain Result-returning steps
flattenResult E (Result E A) -> Result E ARemove one layer of nesting
toOptionResult E A -> Option ADrop the error and keep only success
toListResult E A -> List ATurn Ok into a one-item list
map(A -> B) -> Result E A -> Result E BTransform only the success side
mapBoth(E1 -> E2) -> (A -> B) -> Result E1 A -> Result E2 BTransform both error and success
fold(E -> B) -> (A -> B) -> Result E A -> BCollapse a result to one output type
fromOptionE -> Option A -> Result E ATurn None into an explicit error

isOk ​

Returns True if the result is Ok.

Type: opt:(Result E A) -> Bool

aivi
use aivi.result (isOk)

type Result Text Int -> Bool
func succeeded =
  |> isOk

isErr ​

Returns True if the result is Err.

Type: opt:(Result E A) -> Bool

aivi
use aivi.result (isErr)

type Result Text Int -> Bool
func failed =
  |> isErr

mapErr ​

Transforms the error inside Err, leaving Ok untouched.

Type: transform:(E1 -> E2) -> opt:(Result E1 A) -> Result E2 A

aivi
use aivi.result (mapErr)

type Text -> Int
func toCode = message =>
    42

type Result Text Int -> (Result Int Int)
func withErrorCode =
  |> mapErr toCode

withDefault ​

Extracts the value from Ok, or returns the fallback if Err.

Type: fallback:A -> opt:(Result E A) -> A

aivi
use aivi.result (withDefault)

type Result Text Int -> Int
func safeScore =
  |> withDefault 0

orElse ​

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

aivi
use aivi.result (orElse)

type Result Text Int -> (Result Text Int) -> (Result Text Int)
func withFallback = primary! secondary
  |> orElse secondary

flatMap ​

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

aivi
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 ensurePositive

flatten ​

Removes one layer of nesting from a Result E (Result E A).

Type: opt:(Result E (Result E A)) -> Result E A

aivi
use aivi.result (flatten)

type Result Text (Result Text Int) -> (Result Text Int)
func unwrapNested =
  |> flatten

toOption ​

Converts a Result to an Option, discarding the error. Ok value becomes Some value; Err becomes None.

Type: opt:(Result E A) -> Option A

aivi
use aivi.result (toOption)

type Result Text Int -> (Option Int)
func justValue =
  |> toOption

toList ​

Converts Ok value to a one-element list, or Err to an empty list.

Type: opt:(Result E A) -> List A

aivi
use aivi.result (toList)

type Result Text Int -> (List Int)
func resultItems =
  |> toList

map ​

Transforms the value inside Ok using a function, leaving Err untouched.

Type: transform:(A -> B) -> opt:(Result E A) -> Result E B

aivi
use aivi.result (map)

type Int -> Int
func double = . * 2

type Result Text Int -> (Result Text Int)
func doubleResult =
  |> map double

mapBoth ​

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

aivi
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 double

fold ​

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

aivi
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 identity

fromOption ​

Converts an Option to a Result. Some value becomes Ok value; None becomes Err error.

Type: error:E -> opt:(Option A) -> Result E A

aivi
use aivi.result (fromOption)

type Option Int -> (Result Text Int)
func requireAge = opt =>
    fromOption "Age is required" opt

(c) 2026 by Andreas Herd