make std.result compatible with builtins.tryEval
This commit is contained in:
parent
8f347b9ad3
commit
a547a9bcaf
1 changed files with 8 additions and 11 deletions
|
|
@ -1,29 +1,26 @@
|
||||||
{lists, ...}: rec {
|
{lists, ...}: rec {
|
||||||
# Result Monad
|
# Result Monad
|
||||||
Ok = value: {
|
Ok = value: {
|
||||||
ok = true;
|
success = true;
|
||||||
value = value;
|
value = value;
|
||||||
};
|
};
|
||||||
Err = err: {
|
Err = err: {
|
||||||
ok = false;
|
success = false;
|
||||||
error = err;
|
value = err;
|
||||||
};
|
};
|
||||||
|
|
||||||
# Pattern Matching
|
# Pattern Matching
|
||||||
isResult = r:
|
isResult = r: builtins.attrNames r == ["success" "value"];
|
||||||
(builtins.length (builtins.attrNames r) == 2)
|
isOk = r: isResult r && r.success;
|
||||||
&& builtins.hasAttr "ok" r
|
isErr = r: isResult r && !r.success;
|
||||||
&& (builtins.hasAttr "value" r || builtins.hasAttr "error" r);
|
|
||||||
isOk = r: isResult r && r.ok;
|
|
||||||
isErr = r: isResult r && !r.ok;
|
|
||||||
|
|
||||||
# Unwrap (Monadic Return Operation)
|
# Unwrap (Monadic Return Operation)
|
||||||
unwrap = f: r:
|
unwrap = f: r:
|
||||||
if isOk r
|
if isOk r
|
||||||
then r.value
|
then r.value
|
||||||
else f r.error;
|
else f r.value;
|
||||||
|
|
||||||
unwrapDefault = default: unwrap (x: default);
|
unwrapDefault = default: unwrap (_: default);
|
||||||
|
|
||||||
# Map (Monadic Bind Operation)
|
# Map (Monadic Bind Operation)
|
||||||
identity = r: r;
|
identity = r: r;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue