restructure std -> nib.types + change type naming convention
This commit is contained in:
parent
b9f2acf7e7
commit
eac4161b36
11 changed files with 129 additions and 139 deletions
40
nib/types/result.nix
Normal file
40
nib/types/result.nix
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
{nib, ...}: rec {
|
||||
# Res (Result) Monad
|
||||
Res = success: value: {inherit success value;};
|
||||
Ok = value: Res true value;
|
||||
Ok' = Ok "ok";
|
||||
Err = value: Res false value;
|
||||
Err' = Err "err";
|
||||
|
||||
# Pattern Matching
|
||||
isRes = R: builtins.attrNames R == ["success" "value"];
|
||||
isOk = R: isRes R && R.success;
|
||||
isErr = R: isRes R && !R.success;
|
||||
|
||||
# Unwrap (Monadic Return Operation)
|
||||
unwrapRes = f: R:
|
||||
if isErr R
|
||||
then f R.value
|
||||
else R.value;
|
||||
|
||||
# Map (Monadic Bind Operation)
|
||||
mapRes = f: g: R:
|
||||
if isOk R
|
||||
then Ok (f R.value)
|
||||
else Err (g R.value);
|
||||
mapOk = f: mapRes f (x: x);
|
||||
mapErr = f: mapRes (x: x) f;
|
||||
|
||||
# Conditionals
|
||||
okOr = f: R:
|
||||
if isOk R
|
||||
then R
|
||||
else f R;
|
||||
|
||||
errOr = f: R:
|
||||
if isErr R
|
||||
then R
|
||||
else f R;
|
||||
|
||||
firstErr = nib.types.findFirst isErr Ok';
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue