add unwrapMaybe/mapMaybe
This commit is contained in:
parent
e2021b7093
commit
d867b91bc2
1 changed files with 26 additions and 3 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
{...}: let
|
{this, ...}: let
|
||||||
inherit
|
inherit
|
||||||
(builtins)
|
(builtins)
|
||||||
attrNames
|
attrNames
|
||||||
|
|
@ -6,6 +6,11 @@
|
||||||
isAttrs
|
isAttrs
|
||||||
typeOf
|
typeOf
|
||||||
;
|
;
|
||||||
|
|
||||||
|
inherit
|
||||||
|
(this.std)
|
||||||
|
id
|
||||||
|
;
|
||||||
in rec {
|
in rec {
|
||||||
# NOTE: Maybe intentionally doesn't use the NixTypes.
|
# NOTE: Maybe intentionally doesn't use the NixTypes.
|
||||||
# NOTE: Maybe is used to aid in parsing and bootstrapping.
|
# NOTE: Maybe is used to aid in parsing and bootstrapping.
|
||||||
|
|
@ -31,11 +36,11 @@ in rec {
|
||||||
else throw' "value \"${toString T}\" of primitive type \"${typeOf T}\"";
|
else throw' "value \"${toString T}\" of primitive type \"${typeOf T}\"";
|
||||||
|
|
||||||
isSome = T:
|
isSome = T:
|
||||||
assert enfIsMaybe T "isMaybeSome";
|
assert enfIsMaybe T "isSome";
|
||||||
T._some;
|
T._some;
|
||||||
|
|
||||||
isNone = T:
|
isNone = T:
|
||||||
assert enfIsMaybe T "isMaybeNone";
|
assert enfIsMaybe T "isNone";
|
||||||
! T._some;
|
! T._some;
|
||||||
|
|
||||||
# Monadic Bind Operation
|
# Monadic Bind Operation
|
||||||
|
|
@ -44,6 +49,24 @@ in rec {
|
||||||
then Some (f T._value)
|
then Some (f T._value)
|
||||||
else T;
|
else T;
|
||||||
|
|
||||||
|
# Unwrap Operations
|
||||||
|
# Lift a value out of the monadic context.
|
||||||
|
unwrapMaybe = T:
|
||||||
|
assert enfIsMaybe T "unwrapMaybe"; T._value;
|
||||||
|
|
||||||
|
# Map Operations
|
||||||
|
# Lift a value out of the monadic context and expect a new monadic.
|
||||||
|
mapMaybe = f: g: T: let
|
||||||
|
value =
|
||||||
|
if isSome T
|
||||||
|
then f T._value
|
||||||
|
else g T._value;
|
||||||
|
in
|
||||||
|
assert enfIsMaybe value "mapMaybe"; value;
|
||||||
|
|
||||||
|
mapSome = f: mapMaybe f id;
|
||||||
|
mapNone = mapMaybe id;
|
||||||
|
|
||||||
# Utility Functions
|
# Utility Functions
|
||||||
boolToMaybe = x:
|
boolToMaybe = x:
|
||||||
if x
|
if x
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue