mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00

Implements the new `module` header syntax as described in "module and package changes" [1]: ``` module [Request, Response, req] ``` The old syntax should still work fine, and is automatically upgraded to the new one when running `roc format`. [1] https://docs.google.com/document/d/1E_77fO-44BtoBtXoVeWyGh1xN2KRTWTu8q6i25RNNx0/edit
94 lines
3.2 KiB
Text
94 lines
3.2 KiB
Text
module [Result, isOk, isErr, map, mapErr, try, onErr, withDefault]
|
|
|
|
import Bool exposing [Bool]
|
|
|
|
## The result of an operation that could fail: either the operation went
|
|
## okay, or else there was an error of some sort.
|
|
Result ok err : [Ok ok, Err err]
|
|
|
|
## Returns `Bool.true` if the result indicates a success, else returns `Bool.false`
|
|
## ```roc
|
|
## Result.isOk (Ok 5)
|
|
## ```
|
|
isOk : Result ok err -> Bool
|
|
isOk = \result ->
|
|
when result is
|
|
Ok _ -> Bool.true
|
|
Err _ -> Bool.false
|
|
|
|
## Returns `Bool.true` if the result indicates a failure, else returns `Bool.false`
|
|
## ```roc
|
|
## Result.isErr (Err "uh oh")
|
|
## ```
|
|
isErr : Result ok err -> Bool
|
|
isErr = \result ->
|
|
when result is
|
|
Ok _ -> Bool.false
|
|
Err _ -> Bool.true
|
|
|
|
## If the result is `Ok`, returns the value it holds. Otherwise, returns
|
|
## the given default value.
|
|
## ```roc
|
|
## Result.withDefault (Ok 7) 42
|
|
## Result.withDefault (Err "uh oh") 42
|
|
## ```
|
|
withDefault : Result ok err, ok -> ok
|
|
withDefault = \result, default ->
|
|
when result is
|
|
Ok value -> value
|
|
Err _ -> default
|
|
|
|
## If the result is `Ok`, transforms the value it holds by running a conversion
|
|
## function on it. Then returns a new `Ok` holding the transformed value. If the
|
|
## result is `Err`, this has no effect. Use [mapErr] to transform an `Err`.
|
|
## ```roc
|
|
## Result.map (Ok 12) Num.neg
|
|
## Result.map (Err "yipes!") Num.neg
|
|
## ```
|
|
##
|
|
## Functions like `map` are common in Roc; see for example [List.map],
|
|
## `Set.map`, and `Dict.map`.
|
|
map : Result a err, (a -> b) -> Result b err
|
|
map = \result, transform ->
|
|
when result is
|
|
Ok v -> Ok (transform v)
|
|
Err e -> Err e
|
|
|
|
## If the result is `Err`, transforms the value it holds by running a conversion
|
|
## function on it. Then returns a new `Err` holding the transformed value. If
|
|
## the result is `Ok`, this has no effect. Use [map] to transform an `Ok`.
|
|
## ```roc
|
|
## Result.mapErr (Err "yipes!") Str.isEmpty
|
|
## Result.mapErr (Ok 12) Str.isEmpty
|
|
## ```
|
|
mapErr : Result ok a, (a -> b) -> Result ok b
|
|
mapErr = \result, transform ->
|
|
when result is
|
|
Ok v -> Ok v
|
|
Err e -> Err (transform e)
|
|
|
|
## If the result is `Ok`, transforms the entire result by running a conversion
|
|
## function on the value the `Ok` holds. Then returns that new result. If the
|
|
## result is `Err`, this has no effect. Use `onErr` to transform an `Err`.
|
|
## ```roc
|
|
## Result.try (Ok -1) \num -> if num < 0 then Err "negative!" else Ok -num
|
|
## Result.try (Err "yipes!") \num -> if num < 0 then Err "negative!" else Ok -num
|
|
## ```
|
|
try : Result a err, (a -> Result b err) -> Result b err
|
|
try = \result, transform ->
|
|
when result is
|
|
Ok v -> transform v
|
|
Err e -> Err e
|
|
|
|
## If the result is `Err`, transforms the entire result by running a conversion
|
|
## function on the value the `Err` holds. Then returns that new result. If the
|
|
## result is `Ok`, this has no effect. Use `try` to transform an `Ok`.
|
|
## ```roc
|
|
## Result.onErr (Ok 10) \errorNum -> Str.toU64 errorNum
|
|
## Result.onErr (Err "42") \errorNum -> Str.toU64 errorNum
|
|
## ```
|
|
onErr : Result a err, (err -> Result a otherErr) -> Result a otherErr
|
|
onErr = \result, transform ->
|
|
when result is
|
|
Ok v -> Ok v
|
|
Err e -> transform e
|