From d867b91bc26335514d4f82c24ffe80091076c009 Mon Sep 17 00:00:00 2001 From: Emile Clark-Boman Date: Tue, 27 Jan 2026 12:42:38 +1000 Subject: [PATCH] add unwrapMaybe/mapMaybe --- nt/primitives/bootstrap/maybe.nix | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/nt/primitives/bootstrap/maybe.nix b/nt/primitives/bootstrap/maybe.nix index 653fd71..9016985 100644 --- a/nt/primitives/bootstrap/maybe.nix +++ b/nt/primitives/bootstrap/maybe.nix @@ -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