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
|
||||
(builtins)
|
||||
attrNames
|
||||
|
|
@ -6,6 +6,11 @@
|
|||
isAttrs
|
||||
typeOf
|
||||
;
|
||||
|
||||
inherit
|
||||
(this.std)
|
||||
id
|
||||
;
|
||||
in rec {
|
||||
# NOTE: Maybe intentionally doesn't use the NixTypes.
|
||||
# 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}\"";
|
||||
|
||||
isSome = T:
|
||||
assert enfIsMaybe T "isMaybeSome";
|
||||
assert enfIsMaybe T "isSome";
|
||||
T._some;
|
||||
|
||||
isNone = T:
|
||||
assert enfIsMaybe T "isMaybeNone";
|
||||
assert enfIsMaybe T "isNone";
|
||||
! T._some;
|
||||
|
||||
# Monadic Bind Operation
|
||||
|
|
@ -44,6 +49,24 @@ in rec {
|
|||
then Some (f T._value)
|
||||
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
|
||||
boolToMaybe = x:
|
||||
if x
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue