aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2019-03-12 18:53:16 +0000
committerLouis Pilfold <louis@lpil.uk>2019-03-12 18:53:16 +0000
commit447cd2ba25ac0414ed7604a49484a7590f40d202 (patch)
tree082e73d0bca06f843cf018de76b0fab329d2ab20 /src
parent80a8083b143eaa24218ec5fda16f947658fb823d (diff)
downloadgleam_stdlib-447cd2ba25ac0414ed7604a49484a7590f40d202.tar.gz
gleam_stdlib-447cd2ba25ac0414ed7604a49484a7590f40d202.zip
list:flatten, list:append, test iodata
Diffstat (limited to 'src')
-rw-r--r--src/bool.gleam12
-rw-r--r--src/iodata.gleam15
-rw-r--r--src/list.gleam51
3 files changed, 48 insertions, 30 deletions
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 {