aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gen/iodata.erl16
-rw-r--r--gen/list.erl38
-rw-r--r--src/bool.gleam12
-rw-r--r--src/iodata.gleam15
-rw-r--r--src/list.gleam51
5 files changed, 98 insertions, 34 deletions
diff --git a/gen/iodata.erl b/gen/iodata.erl
index 9704413..a17f87a 100644
--- a/gen/iodata.erl
+++ b/gen/iodata.erl
@@ -1,10 +1,11 @@
-module(iodata).
-compile(no_auto_import).
+-include_lib("eunit/include/eunit.hrl").
-export([prepend/2, append/2, from/1, to_string/1, byte_size/1]).
prepend(A, B) ->
- gleam__stdlib:iodata_concat(A, B).
+ gleam__stdlib:iodata_prepend(A, B).
append(A, B) ->
gleam__stdlib:iodata_append(A, B).
@@ -17,3 +18,16 @@ to_string(A) ->
byte_size(A) ->
erlang:iolist_size(A).
+
+-ifdef(TEST).
+iodata_test() ->
+ Iodata = (fun(Capture1) ->
+ prepend(Capture1, <<"H">>)
+ end)((fun(Capture1) ->
+ append(Capture1, <<" world!">>)
+ end)((fun(Capture1) ->
+ append(Capture1, <<",">>)
+ end)(from([<<"ello">>])))),
+ expect:equal(to_string(Iodata), <<"Hello, world!">>),
+ expect:equal(byte_size(Iodata), 13).
+-endif.
diff --git a/gen/list.erl b/gen/list.erl
index 964dc69..7364f66 100644
--- a/gen/list.erl
+++ b/gen/list.erl
@@ -2,7 +2,7 @@
-compile(no_auto_import).
-include_lib("eunit/include/eunit.hrl").
--export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, map/2, do_traverse/3, traverse/2, new/0, foldl/3, foldr/3]).
+-export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, map/2, do_traverse/3, traverse/2, new/0, append/2, flatten/1, foldl/3, foldr/3]).
length(A) ->
erlang:length(A).
@@ -54,7 +54,7 @@ head(List) ->
[] ->
{error, empty};
- [X | Xs] ->
+ [X | _] ->
{ok, X}
end.
@@ -71,7 +71,7 @@ tail(List) ->
[] ->
{error, empty};
- [X | Xs] ->
+ [_ | Xs] ->
{ok, Xs}
end.
@@ -151,6 +151,38 @@ new_test() ->
(fun(Capture1) -> expect:equal(Capture1, []) end)(new()).
-endif.
+append(A, B) ->
+ lists:append(A, B).
+
+-ifdef(TEST).
+append_test() ->
+ expect:equal(append([1], [2, 3]), [1, 2, 3]).
+-endif.
+
+do_flatten(Lists, Acc) ->
+ case Lists of
+ [] ->
+ Acc;
+
+ [L | Rest] ->
+ do_flatten(Rest, append(Acc, L))
+ end.
+
+flatten(Lists) ->
+ do_flatten(Lists, []).
+
+-ifdef(TEST).
+flatten_test() ->
+ _ = (fun(Capture1) -> expect:equal(Capture1, []) end)(flatten([])),
+ _ = (fun(Capture1) -> expect:equal(Capture1, []) end)(flatten([[]])),
+ _ = (fun(Capture1) ->
+ expect:equal(Capture1, [])
+ end)(flatten([[], [], []])),
+ (fun(Capture1) ->
+ expect:equal(Capture1, [1, 2, 3, 4])
+ end)(flatten([[1, 2], [], [3, 4]])).
+-endif.
+
foldl(List, Acc, Fun) ->
case List of
[] ->
diff --git a/src/bool.gleam b/src/bool.gleam
index 5375d0e..5a89287 100644
--- a/src/bool.gleam
+++ b/src/bool.gleam
@@ -1,5 +1,5 @@
import expect
-// import order:[Gt, Eq, Lt]
+import order
// pub fn not(bool) {
// case bool {
@@ -9,7 +9,7 @@ import expect
// }
// test not {
-// not(True)
+// let _ = not(True)
// |> expect:false
// not(False)
@@ -18,10 +18,10 @@ import expect
// pub fn compare(a, b) {
// case {a, b} {
-// | {True, True} -> Eq
-// | {True, False} -> Gt
-// | {False, False} -> Eq
-// | {False, True} -> Gt
+// | {True, True} -> order:Eq
+// | {True, False} -> order:Gt
+// | {False, False} -> order:Eq
+// | {False, True} -> order:Gt
// }
// }
diff --git a/src/iodata.gleam b/src/iodata.gleam
index 1b28577..4d81e38 100644
--- a/src/iodata.gleam
+++ b/src/iodata.gleam
@@ -1,11 +1,10 @@
import any
-
-// TODO: Tests
+import expect
pub external type Iodata;
pub external fn prepend(Iodata, String) -> Iodata =
- "gleam__stdlib" "iodata_concat";
+ "gleam__stdlib" "iodata_prepend";
pub external fn append(Iodata, String) -> Iodata =
"gleam__stdlib" "iodata_append";
@@ -18,3 +17,13 @@ pub external fn to_string(Iodata) -> String =
pub external fn byte_size(Iodata) -> Int =
"erlang" "iolist_size";
+
+test iodata {
+ let iodata = from(["ello"])
+ |> append(_, ",")
+ |> append(_, " world!")
+ |> prepend(_, "H")
+
+ expect:equal(to_string(iodata), "Hello, world!")
+ expect:equal(byte_size(iodata), 13)
+}
diff --git a/src/list.gleam b/src/list.gleam
index 027afd6..f9fb3b6 100644
--- a/src/list.gleam
+++ b/src/list.gleam
@@ -48,7 +48,7 @@ test has_member {
pub fn head(list) {
case list {
| [] -> Error(Empty)
- | [x | xs] -> Ok(x)
+ | [x | _] -> Ok(x)
}
}
@@ -63,7 +63,7 @@ test head {
pub fn tail(list) {
case list {
| [] -> Error(Empty)
- | [x | xs] -> Ok(xs)
+ | [_ | xs] -> Ok(xs)
}
}
@@ -220,30 +220,39 @@ test new {
new() |> expect:equal(_, [])
}
-// fn do_flatten(lists, acc) {
-// case lists {
-// | [] -> acc
-// | [l | rest] -> flatten(rest, acc ++ l)
-// }
-// }
+pub external fn append(List(a), List(a)) -> List(a) = "lists" "append";
-// pub fn flatten(lists) {
-// do_flatten(lists, [])
-// }
+test append {
+ expect:equal(
+ append([1], [2, 3]),
+ [1, 2, 3],
+ )
+}
-// test flatten {
-// let _ = flatten([])
-// |> expect:equal(_, [])
+fn do_flatten(lists, acc) {
+ case lists {
+ | [] -> acc
+ | [l | rest] -> do_flatten(rest, append(acc, l))
+ }
+}
-// let _ = flatten([[]])
-// |> expect:equal(_, [])
+pub fn flatten(lists) {
+ do_flatten(lists, [])
+}
-// let _ = flatten([[], [], []])
-// |> expect:equal(_, [])
+test flatten {
+ let _ = flatten([])
+ |> expect:equal(_, [])
-// flatten([[1, 2], [], [3, 4]])
-// |> expect:equal(_, [1, 2, 3, 4])
-// }
+ let _ = flatten([[]])
+ |> expect:equal(_, [])
+
+ let _ = flatten([[], [], []])
+ |> expect:equal(_, [])
+
+ flatten([[1, 2], [], [3, 4]])
+ |> expect:equal(_, [1, 2, 3, 4])
+}
pub fn foldl(list, acc, fun) {
case list {