Skip to content

A peer-to-peer stack for code collaboration

Utils for reading/writing keys from files. (#347)

James Haydon3f213e07457a
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 746bcea..337405d 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -700,6 +700,54 @@ Returns a string consisting of the results of applying ``f`` to each
character of ``xs``. Throws a type error if ``f`` returns something
other than a string
+``prelude/dict``
+----------------
+
+Functions for manipualting dicts.
+
+``(dict-from-seq xs)``
+~~~~~~~~~~~~~~~~~~~~~~
+
+Creates a dictionary from a list of key-value pairs.
+
+``(keys d)``
+~~~~~~~~~~~~
+
+Given a dict ``d``, returns a vector of its keys.
+
+``(values d)``
+~~~~~~~~~~~~~~
+
+Given a dict ``d``, returns a vector of its values.
+
+``(rekey old-key new-key d)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Change the key from ``old-key`` to ``new-key`` in a dict ``d``. If
+``new-key`` already exists, it is overwritten.
+
+``(modify-map k f d)``
+~~~~~~~~~~~~~~~~~~~~~~
+
+Given a key ``k``, a function ``f`` and a dict ``d``, applies the
+function to the value associated to that key.
+
+``(delete-many ks d)``
+~~~~~~~~~~~~~~~~~~~~~~
+
+Delete several keys ``ks`` from a dict ``d``.
+
+``(lookup-default key default dict)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Like ``lookup`` but returns ``default`` if the key is not in the map.
+
+``(lookup-maybe key dict)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Like ``lookup`` but returns ``[:just x]`` if the key is not in the map
+and ``:nothing`` otherwise.
+
``prelude/io``
--------------
@@ -766,6 +814,23 @@ Like ``process!``, but captures stdout.
Write ``contents`` to file ``filename``.
+``(init-file-dict! file)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Initiate a file with an empty dict, but only if the file doesn't already
+exist.
+
+``(read-file-key! file k)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Read a file key. Assumes that the file contents is a serialised dict.
+
+``(write-file-key! file k v)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Write a key to a file. Assumes that the file contents is a serialised
+dict.
+
``prelude/bool``
----------------
@@ -832,48 +897,6 @@ Returns ``ls`` with only the elements that satisfy ``pred``.
Returns all elements of a sequence ``ls`` until one does not satisfy
``pred``
-``prelude/dict``
-----------------
-
-Functions for manipualting dicts.
-
-``(dict-from-seq xs)``
-~~~~~~~~~~~~~~~~~~~~~~
-
-Creates a dictionary from a list of key-value pairs.
-
-``(keys d)``
-~~~~~~~~~~~~
-
-Given a dict ``d``, returns a vector of its keys.
-
-``(values d)``
-~~~~~~~~~~~~~~
-
-Given a dict ``d``, returns a vector of its values.
-
-``(rekey old-key new-key d)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Change the key from ``old-key`` to ``new-key`` in a dict ``d``. If
-``new-key`` already exists, it is overwritten.
-
-``(modify-map k f d)``
-~~~~~~~~~~~~~~~~~~~~~~
-
-Given a key ``k``, a function ``f`` and a dict ``d``, applies the
-function to the value associated to that key.
-
-``(delete-many ks d)``
-~~~~~~~~~~~~~~~~~~~~~~
-
-Delete several keys ``ks`` from a dict ``d``.
-
-``(lookup-default key default dict)``
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
-Like ``lookup`` but returns ``default`` if the key is not in the map.
-
``prelude/set``
---------------
diff --git a/rad/prelude.rad b/rad/prelude.rad
index ecd0e07..7515d04 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -4,11 +4,11 @@
(file-module! "rad/prelude/basic.rad")
(file-module! "rad/prelude/patterns.rad")
(file-module! "rad/prelude/strings.rad")
+(file-module! "rad/prelude/dict.rad")
(file-module! "rad/prelude/io.rad")
(file-module! "rad/prelude/bool.rad")
(file-module! "rad/prelude/exception.rad")
(file-module! "rad/prelude/list.rad")
-(file-module! "rad/prelude/dict.rad")
(file-module! "rad/prelude/set.rad")
(file-module! "rad/prelude/ref.rad")
(file-module! "rad/prelude/lens.rad")
@@ -21,10 +21,10 @@
(import prelude/basic :unqualified)
(import prelude/patterns :unqualified)
(import prelude/strings :as 'string)
+(import prelude/dict :unqualified)
(import prelude/io :unqualified)
(import prelude/bool :unqualified)
(import prelude/list :unqualified)
-(import prelude/dict :unqualified)
(import prelude/ref :unqualified)
(import prelude/validation :as 'validator)
(import prelude/util :unqualified)
diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.rad
index 19ebe21..0f55e62 100644
--- a/rad/prelude/chain.rad
+++ b/rad/prelude/chain.rad
@@ -189,9 +189,9 @@
["rad/prelude/basic.rad"
"rad/prelude/patterns.rad"
"rad/prelude/strings.rad"
+ "rad/prelude/dict.rad"
"rad/prelude/bool.rad"
"rad/prelude/list.rad"
- "rad/prelude/dict.rad"
"rad/prelude/set.rad"
"rad/prelude/ref.rad"
"rad/prelude/lens.rad"
diff --git a/rad/prelude/dict.rad b/rad/prelude/dict.rad
index b498df0..0f9564c 100644
--- a/rad/prelude/dict.rad
+++ b/rad/prelude/dict.rad
@@ -1,6 +1,6 @@
{:module 'prelude/dict
:doc "Functions for manipualting dicts."
- :exports '[dict-from-seq keys values rekey modify-map delete-many lookup-default]}
+ :exports '[dict-from-seq keys values rekey modify-map delete-many lookup-default lookup-maybe]}
(def dict-from-seq
"Creates a dictionary from a list of key-value pairs."
@@ -63,6 +63,14 @@
(lookup key dict)
default)))
+(def lookup-maybe
+ "Like `lookup` but returns `[:just x]` if the key is not in the map and
+ `:nothing` otherwise."
+ (fn [key dict]
+ (if (member? key dict)
+ [:just (lookup key dict)]
+ :nothing)))
+
(:test "lookup-default"
[ (lookup-default :one 2 {:one 1}) ==> 1 ]
[ (lookup-default :two 2 {:one 1}) ==> 2 ])
diff --git a/rad/prelude/io.rad b/rad/prelude/io.rad
index 3e78b85..491af2a 100644
--- a/rad/prelude/io.rad
+++ b/rad/prelude/io.rad
@@ -2,9 +2,11 @@
:doc "Some basic I/O functions."
:exports '[print! shell! process! read-line! read-file-value! read-file-values! send-code!
shell-with-stdout! shell-no-stdin! process-with-stdout! write-file!
+ init-file-dict! read-file-key! write-file-key!
]}
(import prelude/patterns :unqualified)
+(import prelude/dict :unqualified)
(def read-line!
"Read a single line of input and interpret it as radicle data."
@@ -171,3 +173,27 @@ Example: `(process! \"ls\" [\"-Glah\"] \"\")`. "
(fn []
;; Some systems support the -1 option, but this is more portable
(shell-with-stdout! "ls -a | cat" "")))
+
+(def modify-file!
+ "Modified the value stored in a file according to the function `f`."
+ (fn [file f]
+ (write-file! file (show (f (read-file-value! file))))))
+
+(def init-file-dict!
+ "Initiate a file with an empty dict, but only if the file doesn't already
+ exist."
+ (fn [file]
+ (catch 'any
+ (read-file! file)
+ (fn [_] (write-file! file "{}")))))
+
+(def write-file-key!
+ "Write a key to a file. Assumes that the file contents is a serialised dict."
+ (fn [file k v]
+ (modify-file! file
+ (fn [s] (insert k v s)))))
+
+(def read-file-key!
+ "Read a file key. Assumes that the file contents is a serialised dict."
+ (fn [file k]
+ (lookup-maybe k (read-file-value! file))))
diff --git a/reference-doc.yaml b/reference-doc.yaml
index 1e5f308..5fe3b77 100644
--- a/reference-doc.yaml
+++ b/reference-doc.yaml
@@ -125,11 +125,11 @@ modules:
- prelude/basic
- prelude/patterns
- prelude/strings
+- prelude/dict
- prelude/io
- prelude/bool
- prelude/exception
- prelude/list
-- prelude/dict
- prelude/set
- prelude/ref
- prelude/lens