aboutsummaryrefslogtreecommitdiff
path: root/src/atom.gleam
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2019-03-17 14:34:07 +0000
committerLouis Pilfold <louis@lpil.uk>2019-03-17 14:34:07 +0000
commit00ff9767dc61e698aac791b43704cfce1ab33600 (patch)
tree257392f879a3524b04043e5385ca1ba871aa6908 /src/atom.gleam
parentde6dca34edf5ab52bc7eab02745c59d8c0dd777d (diff)
downloadgleam_stdlib-00ff9767dc61e698aac791b43704cfce1ab33600.tar.gz
gleam_stdlib-00ff9767dc61e698aac791b43704cfce1ab33600.zip
stdlib atom
Diffstat (limited to 'src/atom.gleam')
-rw-r--r--src/atom.gleam64
1 files changed, 64 insertions, 0 deletions
diff --git a/src/atom.gleam b/src/atom.gleam
new file mode 100644
index 0000000..e887a01
--- /dev/null
+++ b/src/atom.gleam
@@ -0,0 +1,64 @@
+import expect
+
+pub external type Atom;
+
+pub enum AtomNotLoaded =
+ | AtomNotLoaded
+
+pub external fn from_string(String) -> Result(Atom, AtomNotLoaded) =
+ "gleam__stdlib" "atom_from_string";
+
+test from_string {
+ "ok"
+ |> from_string
+ |> expect:is_ok
+
+ "expect"
+ |> from_string
+ |> expect:is_ok
+
+ "this is not an atom we have seen before"
+ |> from_string
+ |> expect:equal(_, Error(AtomNotLoaded))
+}
+
+// This function can create a new atom if one does not already exist for
+// the given string. Atoms are not garbage collected so this can result
+// in a memory leak if called over time on new values
+//
+pub external fn create_from_string(String) -> Atom =
+ "gleam__stdlib" "atom_create_from_string";
+
+test create_from_string {
+ let ok = fn(x) { Ok(x) }
+
+ "ok"
+ |> create_from_string
+ |> ok
+ |> expect:equal(_, from_string("ok"))
+
+ "expect"
+ |> create_from_string
+ |> ok
+ |> expect:equal(_, from_string("expect"))
+
+ "this is another atom we have not seen before"
+ |> create_from_string
+ |> ok
+ |> expect:equal(_, from_string("this is another atom we have not seen before"))
+}
+
+pub external fn to_string(Atom) -> String =
+ "gleam__stdlib" "atom_to_string";
+
+test to_string {
+ "ok"
+ |> create_from_string
+ |> to_string
+ |> expect:equal(_, "ok")
+
+ "expect"
+ |> create_from_string
+ |> to_string
+ |> expect:equal(_, "expect")
+}