aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarvin Borner2022-07-18 23:22:26 +0200
committerMarvin Borner2022-07-18 23:22:26 +0200
commit4207797a2a96c2be9e07c4dee5a1fa1f89e845ed (patch)
tree92143ec4c047758ab54322cf986ee378109705b3
parent8f31ae96b5c3e76bce354cdf75a2c7d7872a560a (diff)
Improved std library
-rw-r--r--std/Number.bruijn2
-rw-r--r--std/Result.bruijn31
2 files changed, 31 insertions, 2 deletions
diff --git a/std/Number.bruijn b/std/Number.bruijn
index 5bc95a1..b8232dc 100644
--- a/std/Number.bruijn
+++ b/std/Number.bruijn
@@ -169,5 +169,3 @@ smul [[strip (mul 1 0)]]
:test eq? (mul -1 +42) -42 = T
:test eq? (mul +3 +11) +33 = T
:test eq? (mul +42 -4) -168 = T
-
-
diff --git a/std/Result.bruijn b/std/Result.bruijn
index 28b8181..a8d830c 100644
--- a/std/Result.bruijn
+++ b/std/Result.bruijn
@@ -1,6 +1,7 @@
# MIT License, Copyright (c) 2022 Marvin Borner
:import std/Combinator .
+:import std/Option .
# encapsulates a value in ok
ok [[[1 2]]]
@@ -19,3 +20,33 @@ ok? [0 [T] [F]]
err? [0 [F] [T]]
:test err? (ok [[0]]) = F
:test err? (err [[0]]) = T
+
+# encapsulates result ok value in a option
+option-ok [0 some [none]]
+:test option-ok (ok [[0]]) = some [[0]]
+:test option-ok (err [[0]]) = none
+
+# encapsulate result err value in a option
+option-err [0 [none] some]
+:test option-err (ok [[0]]) = none
+:test option-err (err [[0]]) = some [[0]]
+
+# extracts value from result or returns first arg
+unwrap-or [[0 I [2]]]
+:test unwrap-or [[1]] (ok [[0]]) = [[0]]
+:test unwrap-or [[1]] (err [[0]]) = [[1]]
+
+# applies a function to the value in ok result
+map [[0 [ok (2 0)] err]]
+:test map [[1]] (ok [[0]]) = ok [[[0]]]
+:test map [[1]] (err [[0]]) = err [[0]]
+
+# applies a function to the value in err result
+map-err [[0 ok [err (2 0)]]]
+:test map-err [[1]] (ok [[0]]) = (ok [[0]])
+:test map-err [[1]] (err [[0]]) = (err [[[0]]])
+
+# applies encapsulated value to given function (if ok)
+apply [[1 0 err]]
+:test apply (err [[0]]) [ok ([[1]] 0)] = err [[0]]
+:test apply (ok [[0]]) [ok ([[1]] 0)] = ok [[[0]]]