aboutsummaryrefslogtreecommitdiffhomepage
path: root/std/Box.bruijn
diff options
context:
space:
mode:
Diffstat (limited to 'std/Box.bruijn')
-rw-r--r--std/Box.bruijn41
1 files changed, 41 insertions, 0 deletions
diff --git a/std/Box.bruijn b/std/Box.bruijn
new file mode 100644
index 0000000..3e3cbfa
--- /dev/null
+++ b/std/Box.bruijn
@@ -0,0 +1,41 @@
+# MIT License, Copyright (c) 2023 Marvin Borner
+# a box can store a single item very efficiently
+# similar structure to std/Option
+
+:import std/Logic .
+:import std/Combinator .
+
+# a empty box
+empty true ⧗ (Box a)
+
+# returns true if the box is empty
+empty? [0 true [false]] ⧗ (Box a) → Boolean
+
+:test (empty? empty) (true)
+
+# builds a box out of a value
+box [[[0 2]]] ⧗ a → (Box a)
+
+# returns true if the box is set
+set? [0 false [true]] ⧗ (Box a) → Boolean
+
+:test (set? (box [0])) (true)
+:test (set? empty) (false)
+
+# sets the value of a empty box, ignores argument if already set
+store! [[empty? 1 (box 0) 1]] ⧗ (Box a) → a → (Box a)
+
+:test (store! (box [[0]]) [[1]]) (box [[0]])
+:test (store! empty [[1]]) (box [[1]])
+
+# sets/overrides the value of a box
+set! [[(box 0)]] ⧗ (Box a) → a → (Box a)
+
+:test (set! (box [[0]]) [[1]]) (box [[1]])
+:test (set! empty [[1]]) (box [[1]])
+
+# extracts value from a box or returns first argument if none
+get [[0 1 i]] ⧗ a → (Box b) → c
+
+:test (get [[0]] (box [[1]])) ([[1]])
+:test (get [[0]] empty) ([[0]])