]> gitweb.factorcode.org Git - factor.git/commitdiff
namespaces.extras: Add `required` word which ensures a variable is set
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 10 Feb 2023 03:24:45 +0000 (21:24 -0600)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 10 Feb 2023 03:28:15 +0000 (21:28 -0600)
also add ?xor and one-of which ensures one of two variables is set

extra/namespaces/extras/authors.txt [new file with mode: 0644]
extra/namespaces/extras/extras.factor [new file with mode: 0644]

diff --git a/extra/namespaces/extras/authors.txt b/extra/namespaces/extras/authors.txt
new file mode 100644 (file)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/extra/namespaces/extras/extras.factor b/extra/namespaces/extras/extras.factor
new file mode 100644 (file)
index 0000000..d107c48
--- /dev/null
@@ -0,0 +1,22 @@
+! Copyright (C) 2023 Doug Coleman.
+! See https://factorcode.org/license.txt for BSD license.
+USING: kernel namespaces ;
+IN: namespaces.extras
+
+ERROR: variable-required variable ;
+
+: required ( symbol -- str/f )
+    dup get [ nip ] [ variable-required ] if* ;
+
+: 2required ( symbol1 symbol2 -- obj1 obj2 ) [ required ] bi@ ; inline
+: 2get ( symbol1 symbol2 -- obj1 obj2 ) [ get ] bi@ ; inline
+
+: ?xor ( obj1 obj2 -- xor first? )
+    [ swap [ 2drop f f ] [ f ] if* ]
+    [ [ t ] [ f f ] if* ] if* ;
+
+ERROR: one-variable-only symbol1 symbol2 value1 value2 ;
+
+: one-of ( symbol1 symbol2 -- str/f first? )
+    2dup [ get ] bi@ 2dup ?xor over
+    [ [ 4drop ] 2dip ] [ one-variable-only ] if ;