aboutsummaryrefslogtreecommitdiff
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
parentde6dca34edf5ab52bc7eab02745c59d8c0dd777d (diff)
downloadgleam_stdlib-00ff9767dc61e698aac791b43704cfce1ab33600.tar.gz
gleam_stdlib-00ff9767dc61e698aac791b43704cfce1ab33600.zip
stdlib atom
-rw-r--r--gen/atom.erl38
-rw-r--r--gen/expect.erl8
-rw-r--r--gen/list.erl6
-rw-r--r--src/atom.gleam64
-rw-r--r--src/expect.gleam4
-rw-r--r--src/gleam__stdlib.erl18
-rw-r--r--src/list.gleam6
7 files changed, 135 insertions, 9 deletions
diff --git a/gen/atom.erl b/gen/atom.erl
new file mode 100644
index 0000000..70c899b
--- /dev/null
+++ b/gen/atom.erl
@@ -0,0 +1,38 @@
+-module(atom).
+-compile(no_auto_import).
+-include_lib("eunit/include/eunit.hrl").
+
+-export([from_string/1, create_from_string/1, to_string/1]).
+
+from_string(A) ->
+ gleam__stdlib:atom_from_string(A).
+
+-ifdef(TEST).
+from_string_test() ->
+ expect:is_ok(from_string(<<"ok">>)),
+ expect:is_ok(from_string(<<"expect">>)),
+ expect:equal(from_string(<<"this is not an atom we have seen before">>),
+ {error, atom_not_loaded}).
+-endif.
+
+create_from_string(A) ->
+ gleam__stdlib:atom_create_from_string(A).
+
+-ifdef(TEST).
+create_from_string_test() ->
+ Ok = fun(X) -> {ok, X} end,
+ expect:equal(Ok(create_from_string(<<"ok">>)), from_string(<<"ok">>)),
+ expect:equal(Ok(create_from_string(<<"expect">>)),
+ from_string(<<"expect">>)),
+ expect:equal(Ok(create_from_string(<<"this is another atom we have not seen before">>)),
+ from_string(<<"this is another atom we have not seen before">>)).
+-endif.
+
+to_string(A) ->
+ gleam__stdlib:atom_to_string(A).
+
+-ifdef(TEST).
+to_string_test() ->
+ expect:equal(to_string(create_from_string(<<"ok">>)), <<"ok">>),
+ expect:equal(to_string(create_from_string(<<"expect">>)), <<"expect">>).
+-endif.
diff --git a/gen/expect.erl b/gen/expect.erl
index 4d42f10..602f3fb 100644
--- a/gen/expect.erl
+++ b/gen/expect.erl
@@ -1,7 +1,7 @@
-module(expect).
-compile(no_auto_import).
--export([equal/2, not_equal/2, true/1, false/1, fail/0]).
+-export([equal/2, not_equal/2, true/1, false/1, is_ok/1, is_error/1, fail/0]).
equal(A, B) ->
gleam__stdlib:expect_equal(A, B).
@@ -15,5 +15,11 @@ true(A) ->
false(A) ->
gleam__stdlib:expect_false(A).
+is_ok(A) ->
+ gleam__stdlib:expect_is_ok(A).
+
+is_error(A) ->
+ gleam__stdlib:expect_is_error(A).
+
fail() ->
true(false).
diff --git a/gen/list.erl b/gen/list.erl
index 5c8e8e6..a5df23e 100644
--- a/gen/list.erl
+++ b/gen/list.erl
@@ -101,8 +101,8 @@ filter(List, Fun) ->
-ifdef(TEST).
filter_test() ->
- expect:equal(filter([], fun(X) -> true end), []),
- expect:equal(filter([0, 4, 5, 7, 3], fun(X) -> true end), [0, 4, 5, 7, 3]),
+ expect:equal(filter([], fun(_) -> true end), []),
+ expect:equal(filter([0, 4, 5, 7, 3], fun(_) -> true end), [0, 4, 5, 7, 3]),
expect:equal(filter([0, 4, 5, 7, 3], fun(X) -> X > 4 end), [5, 7]),
expect:equal(filter([0, 4, 5, 7, 3], fun(X) -> X < 4 end), [0, 3]).
-endif.
@@ -166,7 +166,7 @@ drop(List, N) ->
[] ->
[];
- [X | Xs] ->
+ [_ | Xs] ->
drop(Xs, N - 1)
end
end.
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")
+}
diff --git a/src/expect.gleam b/src/expect.gleam
index a597d9a..5ea6a93 100644
--- a/src/expect.gleam
+++ b/src/expect.gleam
@@ -11,6 +11,10 @@ pub external fn true(Bool) -> Expectation = "gleam__stdlib" "expect_true";
pub external fn false(Bool) -> Expectation = "gleam__stdlib" "expect_false";
+pub external fn is_ok(Result(a, b)) -> Expectation = "gleam__stdlib" "expect_is_ok";
+
+pub external fn is_error(Result(a, b)) -> Expectation = "gleam__stdlib" "expect_is_error";
+
pub fn fail() {
true(False)
}
diff --git a/src/gleam__stdlib.erl b/src/gleam__stdlib.erl
index 25abb25..24668a7 100644
--- a/src/gleam__stdlib.erl
+++ b/src/gleam__stdlib.erl
@@ -1,13 +1,16 @@
-module(gleam__stdlib).
-include_lib("eunit/include/eunit.hrl").
--export([expect_equal/2, expect_not_equal/2, expect_true/1, expect_false/1, map_fetch/2,
- iodata_append/2, iodata_prepend/2, identity/1]).
+-export([expect_equal/2, expect_not_equal/2, expect_true/1, expect_false/1, expect_is_ok/1,
+ expect_is_error/1, atom_from_string/1, atom_create_from_string/1, atom_to_string/1,
+ map_fetch/2, iodata_append/2, iodata_prepend/2, identity/1]).
expect_equal(A, Expected) -> ?assertEqual(Expected, A).
expect_not_equal(A, Expected) -> ?assertNotEqual(Expected, A).
expect_true(A) -> ?assert(A).
expect_false(A) -> ?assertNot(A).
+expect_is_ok(A) -> ?assertMatch({ok, _}, A).
+expect_is_error(A) -> ?assertMatch({error, _}, A).
map_fetch(Map, Key) ->
case maps:find(Key, Map) of
@@ -15,6 +18,17 @@ map_fetch(Map, Key) ->
OkFound -> OkFound
end.
+atom_create_from_string(S) ->
+ binary_to_atom(S, utf8).
+
+atom_to_string(S) ->
+ atom_to_binary(S, utf8).
+
+atom_from_string(S) ->
+ try {ok, binary_to_existing_atom(S, utf8)} catch
+ error:badarg -> {error, atom_not_loaded}
+ end.
+
iodata_append(Iodata, String) -> [Iodata, String].
iodata_prepend(Iodata, String) -> [String, Iodata].
diff --git a/src/list.gleam b/src/list.gleam
index 527ec51..6687acf 100644
--- a/src/list.gleam
+++ b/src/list.gleam
@@ -97,11 +97,11 @@ pub fn filter(list, fun) {
test filter {
[]
- |> filter(_, fn(x) { True })
+ |> filter(_, fn(_) { True })
|> expect:equal(_, [])
[0, 4, 5, 7, 3]
- |> filter(_, fn(x) { True })
+ |> filter(_, fn(_) { True })
|> expect:equal(_, [0, 4, 5, 7, 3])
[0, 4, 5, 7, 3]
@@ -173,7 +173,7 @@ pub fn drop(list, n) {
| False ->
case list {
| [] -> []
- | [x | xs] -> drop(xs, n - 1)
+ | [_ | xs] -> drop(xs, n - 1)
}
}
}