aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2019-10-28 15:26:08 +0000
committerLouis Pilfold <louis@lpil.uk>2019-10-28 15:26:08 +0000
commit0a5b2ef05bcc0440aca9928a2188c891437588cc (patch)
treee04c4d2b4ae65c92d0050586d9792b08d7065b8f
parent9dcd4c6acf2212c6014fbc5dfa8d79dccbd5661e (diff)
downloadgleam_stdlib-0a5b2ef05bcc0440aca9928a2188c891437588cc.tar.gz
gleam_stdlib-0a5b2ef05bcc0440aca9928a2188c891437588cc.zip
Update for Gleam v0.5 syntax
-rw-r--r--src/gleam/atom.gleam4
-rw-r--r--src/gleam/bool.gleam24
-rw-r--r--src/gleam/float.gleam16
-rw-r--r--src/gleam/int.gleam16
-rw-r--r--src/gleam/iodata.gleam5
-rw-r--r--src/gleam/list.gleam203
-rw-r--r--src/gleam/map.gleam8
-rw-r--r--src/gleam/order.gleam46
-rw-r--r--src/gleam/result.gleam28
-rw-r--r--test/gleam/list_test.gleam8
-rw-r--r--test/gleam/map_test.gleam4
11 files changed, 184 insertions, 178 deletions
diff --git a/src/gleam/atom.gleam b/src/gleam/atom.gleam
index a5418d5..e870178 100644
--- a/src/gleam/atom.gleam
+++ b/src/gleam/atom.gleam
@@ -1,6 +1,8 @@
pub external type Atom;
-pub enum AtomNotLoaded = | AtomNotLoaded
+pub enum AtomNotLoaded {
+ AtomNotLoaded
+}
pub external fn from_string(String) -> Result(Atom, AtomNotLoaded) =
"gleam_stdlib" "atom_from_string";
diff --git a/src/gleam/bool.gleam b/src/gleam/bool.gleam
index 0c3b836..815688e 100644
--- a/src/gleam/bool.gleam
+++ b/src/gleam/bool.gleam
@@ -2,37 +2,37 @@ import gleam/order
pub fn negate(bool) {
case bool {
- | True -> False
- | False -> True
+ True -> False
+ False -> True
}
}
pub fn compare(a, b) {
case a, b {
- | True, True -> order.Eq
- | True, False -> order.Gt
- | False, False -> order.Eq
- | False, True -> order.Lt
+ True, True -> order.Eq
+ True, False -> order.Gt
+ False, False -> order.Eq
+ False, True -> order.Lt
}
}
pub fn max(a, b) {
case a {
- | True -> True
- | False -> b
+ True -> True
+ False -> b
}
}
pub fn min(a, b) {
case a {
- | False -> False
- | True -> b
+ False -> False
+ True -> b
}
}
pub fn to_int(bool) {
case bool {
- | False -> 0
- | True -> 1
+ False -> 0
+ True -> 1
}
}
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index d27587c..6f6a52d 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -12,26 +12,26 @@ pub fn to_string(f) {
pub fn compare(a, b) {
case a == b {
- | True -> order.Eq
- | False ->
+ True -> order.Eq
+ False ->
case a <. b {
- | True -> order.Lt
- | False -> order.Gt
+ True -> order.Lt
+ False -> order.Gt
}
}
}
pub fn min(a, b) {
case a <. b {
- | True -> a
- | False -> b
+ True -> a
+ False -> b
}
}
pub fn max(a, b) {
case a >. b {
- | True -> a
- | False -> b
+ True -> a
+ False -> b
}
}
diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam
index 2171852..fc3526e 100644
--- a/src/gleam/int.gleam
+++ b/src/gleam/int.gleam
@@ -8,26 +8,26 @@ pub external fn to_base_string(Int, Int) -> String = "erlang" "integer_to_binary
pub fn compare(a, b) {
case a == b {
- | True -> order.Eq
- | False ->
+ True -> order.Eq
+ False ->
case a < b {
- | True -> order.Lt
- | False -> order.Gt
+ True -> order.Lt
+ False -> order.Gt
}
}
}
pub fn min(a, b) {
case a < b {
- | True -> a
- | False -> b
+ True -> a
+ False -> b
}
}
pub fn max(a, b) {
case a > b {
- | True -> a
- | False -> b
+ True -> a
+ False -> b
}
}
diff --git a/src/gleam/iodata.gleam b/src/gleam/iodata.gleam
index 46f2369..3a7f5f2 100644
--- a/src/gleam/iodata.gleam
+++ b/src/gleam/iodata.gleam
@@ -36,8 +36,9 @@ pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase"
pub external fn reverse(Iodata) -> Iodata = "string" "reverse"
-enum Direction =
- | All
+enum Direction {
+ All
+}
external fn erl_split(Iodata, String, Direction) -> List(Iodata) =
"string" "split"
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 00d53a2..6628caa 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -2,7 +2,9 @@ import gleam/int
import gleam/order
import gleam/pair.{Pair}
-pub enum LengthMismatch = | LengthMismatch
+pub enum LengthMismatch {
+ LengthMismatch
+}
// Using the Erlang C BIF implementation.
//
@@ -18,35 +20,35 @@ pub fn is_empty(list) {
pub fn contains(list, has elem) {
case list {
- | [] -> False
- | [head | rest] -> head == elem || contains(rest, elem)
+ [] -> False
+ [head | rest] -> head == elem || contains(rest, elem)
}
}
pub fn head(list) {
case list {
- | [] -> Error(Nil)
- | [x | _] -> Ok(x)
+ [] -> Error(Nil)
+ [x | _] -> Ok(x)
}
}
pub fn tail(list) {
case list {
- | [] -> Error(Nil)
- | [_ | xs] -> Ok(xs)
+ [] -> Error(Nil)
+ [_ | xs] -> Ok(xs)
}
}
fn do_filter(list, fun, acc) {
case list {
- | [] -> reverse(acc)
- | [x | xs] ->
- let new_acc =
- case fun(x) {
- | True -> [x | acc]
- | False -> acc
- }
+ [] -> reverse(acc)
+ [x | xs] -> {
+ let new_acc = case fun(x) {
+ True -> [x | acc]
+ False -> acc
+ }
do_filter(xs, fun, new_acc)
+ }
}
}
@@ -56,8 +58,8 @@ pub fn filter(list, for predicate) {
fn do_map(list, fun, acc) {
case list {
- | [] -> reverse(acc)
- | [x | xs] -> do_map(xs, fun, [fun(x) | acc])
+ [] -> reverse(acc)
+ [x | xs] -> do_map(xs, fun, [fun(x) | acc])
}
}
@@ -67,8 +69,8 @@ pub fn map(list, with fun) {
fn do_index_map(list, fun, index, acc) {
case list {
- | [] -> reverse(acc)
- | [x | xs] -> do_index_map(xs, fun, index + 1, [fun(index, x) | acc])
+ [] -> reverse(acc)
+ [x | xs] -> do_index_map(xs, fun, index + 1, [fun(index, x) | acc])
}
}
@@ -78,11 +80,11 @@ pub fn index_map(list, with fun) {
fn do_traverse(list, fun, acc) {
case list {
- | [] -> Ok(reverse(acc))
- | [x | xs] ->
+ [] -> Ok(reverse(acc))
+ [x | xs] ->
case fun(x) {
- | Ok(y) -> do_traverse(xs, fun, [y | acc])
- | Error(error) -> Error(error)
+ Ok(y) -> do_traverse(xs, fun, [y | acc])
+ Error(error) -> Error(error)
}
}
}
@@ -93,22 +95,22 @@ pub fn traverse(list, with fun) {
pub fn drop(from list, up_to n) {
case n <= 0 {
- | True -> list
- | False ->
+ True -> list
+ False ->
case list {
- | [] -> []
- | [_ | xs] -> drop(xs, n - 1)
+ [] -> []
+ [_ | xs] -> drop(xs, n - 1)
}
}
}
fn do_take(list, n, acc) {
case n <= 0 {
- | True -> reverse(acc)
- | False ->
+ True -> reverse(acc)
+ False ->
case list {
- | [] -> reverse(acc)
- | [x | xs] -> do_take(xs, n - 1, [x | acc])
+ [] -> reverse(acc)
+ [x | xs] -> do_take(xs, n - 1, [x | acc])
}
}
}
@@ -125,8 +127,8 @@ pub external fn append(List(a), List(a)) -> List(a) = "lists" "append";
fn do_flatten(lists, acc) {
case lists {
- | [] -> acc
- | [l | rest] -> do_flatten(rest, append(acc, l))
+ [] -> acc
+ [l | rest] -> do_flatten(rest, append(acc, l))
}
}
@@ -136,95 +138,95 @@ pub fn flatten(lists) {
pub fn fold(list, from initial, with fun) {
case list {
- | [] -> initial
- | [x | rest] -> fold(rest, fun(x, initial), fun)
+ [] -> initial
+ [x | rest] -> fold(rest, fun(x, initial), fun)
}
}
pub fn fold_right(list, from initial, with fun) {
case list {
- | [] -> initial
- | [x | rest] -> fun(x, fold_right(rest, initial, fun))
+ [] -> initial
+ [x | rest] -> fun(x, fold_right(rest, initial, fun))
}
}
pub fn find(in haystack, one_that is_desired) {
case haystack {
- | [] -> Error(Nil)
- | [x | rest] ->
+ [] -> Error(Nil)
+ [x | rest] ->
case is_desired(x) {
- | True -> Ok(x)
- | _ -> find(in: rest, one_that: is_desired)
+ True -> Ok(x)
+ _ -> find(in: rest, one_that: is_desired)
}
}
}
pub fn find_map(in haystack, with fun) {
case haystack {
- | [] -> Error(Nil)
- | [x | rest] ->
+ [] -> Error(Nil)
+ [x | rest] ->
case fun(x) {
- | Ok(x) -> Ok(x)
- | _ -> find_map(in: rest, with: fun)
+ Ok(x) -> Ok(x)
+ _ -> find_map(in: rest, with: fun)
}
}
}
pub fn all(in list, satisfying predicate) {
case list {
- | [] -> True
- | [x | rest] ->
+ [] -> True
+ [x | rest] ->
case predicate(x) {
- | True -> all(rest, predicate)
- | _ -> False
+ True -> all(rest, predicate)
+ _ -> False
}
}
}
pub fn any(in list, satisfying predicate) {
case list {
- | [] -> False
- | [x | rest] ->
+ [] -> False
+ [x | rest] ->
case predicate(x) {
- | False -> any(rest, predicate)
- | _ -> True
+ False -> any(rest, predicate)
+ _ -> True
}
}
}
pub fn zip(xs, ys) {
case xs, ys {
- | [], _ -> []
- | _, [] -> []
- | [x | xs], [y | ys] -> [ Pair(x, y) | zip(xs, ys) ]
+ [], _ -> []
+ _, [] -> []
+ [x | xs], [y | ys] -> [ Pair(x, y) | zip(xs, ys) ]
}
}
pub fn strict_zip(l1, l2) {
case length(l1) == length(l2) {
- | True -> Ok(zip(l1, l2))
- | False -> Error(LengthMismatch)
+ True -> Ok(zip(l1, l2))
+ False -> Error(LengthMismatch)
}
}
pub fn intersperse(list, with elem) {
case list {
- | [] -> []
- | [x | []] -> [x]
- | [x | rest] -> [x | [elem | intersperse(rest, elem)]]
+ [] -> []
+ [x | []] -> [x]
+ [x | rest] -> [x | [elem | intersperse(rest, elem)]]
}
}
pub fn at(in list, get index) {
case index < 0 {
- | True -> Error(Nil)
- | False ->
+ True -> Error(Nil)
+ False ->
case list {
- | [] -> Error(Nil)
- | [x | rest] ->
+ [] -> Error(Nil)
+ [x | rest] ->
case index == 0 {
- | True -> Ok(x)
- | False -> at(rest, index - 1)
+ True -> Ok(x)
+ False -> at(rest, index - 1)
}
}
}
@@ -232,35 +234,36 @@ pub fn at(in list, get index) {
pub fn unique(list) {
case list {
- | [] -> []
- | [x | rest] -> [x | unique(filter(rest, fn(y) { y != x }))]
+ [] -> []
+ [x | rest] -> [x | unique(filter(rest, fn(y) { y != x }))]
}
}
fn merge_sort(a, b, compare) {
case a, b {
- | [], _ -> b
- | _, [] -> a
- | [ax | ar], [bx | br] ->
- case compare(ax, bx) {
- | order.Lt -> [ax | merge_sort(ar, b, compare)]
- | _ -> [bx | merge_sort(a, br, compare)]
- }
+ [], _ -> b
+ _, [] -> a
+ [ax | ar], [bx | br] ->
+ case compare(ax, bx) {
+ order.lt -> [ax | merge_sort(ar, b, compare)]
+ _ -> [bx | merge_sort(a, br, compare)]
+ }
}
}
fn do_sort(list, compare, list_length) {
case list_length < 2 {
- | True -> list
- | False ->
- let split_length = list_length / 2
- let a_list = take(list, split_length)
- let b_list = drop(list, split_length)
- merge_sort(
- do_sort(a_list, compare, split_length),
- do_sort(b_list, compare, list_length - split_length),
- compare,
- )
+ True -> list
+ False -> {
+ let split_length = list_length / 2
+ let a_list = take(list, split_length)
+ let b_list = drop(list, split_length)
+ merge_sort(
+ do_sort(a_list, compare, split_length),
+ do_sort(b_list, compare, list_length - split_length),
+ compare,
+ )
+ }
}
}
@@ -270,16 +273,16 @@ pub fn sort(list, sort_by compare) {
pub fn range(from start, to stop) {
case int.compare(start, stop) {
- | order.Eq -> []
- | order.Gt -> [start | range(start - 1, stop)]
- | order.Lt -> [start | range(start + 1, stop)]
+ order.Eq -> []
+ order.Gt -> [start | range(start - 1, stop)]
+ order.Lt -> [start | range(start + 1, stop)]
}
}
fn do_repeat(a, times, acc) {
case times <= 0 {
- | True -> acc
- | False -> do_repeat(a, times - 1, [a | acc])
+ True -> acc
+ False -> do_repeat(a, times - 1, [a | acc])
}
}
@@ -289,11 +292,11 @@ pub fn repeat(item a, times times) {
fn do_split(list, n, taken) {
case n <= 0 {
- | True -> Pair(reverse(taken), list)
- | False ->
+ True -> Pair(reverse(taken), list)
+ False ->
case list {
- | [] -> Pair(reverse(taken), [])
- | [x | xs] -> do_split(xs, n - 1, [x | taken])
+ [] -> Pair(reverse(taken), [])
+ [x | xs] -> do_split(xs, n - 1, [x | taken])
}
}
}
@@ -304,11 +307,11 @@ pub fn split(list list, on target) {
fn do_split_while(list, f, acc) {
case list {
- | [] -> Pair(reverse(acc), [])
- | [x | xs] ->
+ [] -> Pair(reverse(acc), [])
+ [x | xs] ->
case f(x) {
- | False -> Pair(reverse(acc), list)
- | _ -> do_split_while(xs, f, [x | acc])
+ False -> Pair(reverse(acc), list)
+ _ -> do_split_while(xs, f, [x | acc])
}
}
}
@@ -320,8 +323,8 @@ pub fn split_while(list list, while predicate) {
pub fn key_find(in haystack, find needle) {
find_map(haystack, fn(p) {
case pair.first(p) == needle {
- | True -> p |> pair.second |> Ok
- | False -> Error(Nil)
+ True -> p |> pair.second |> Ok
+ False -> Error(Nil)
}
})
}
diff --git a/src/gleam/map.gleam b/src/gleam/map.gleam
index 340fb64..49ca618 100644
--- a/src/gleam/map.gleam
+++ b/src/gleam/map.gleam
@@ -78,15 +78,15 @@ pub fn drop(from map, drop disallowed_keys) {
pub fn update(in map, update key, with fun) {
case get(map, key) {
- | Ok(value) -> insert(map, key, fun(Ok(value)))
- | Error(_) -> insert(map, key, fun(Error(Nil)))
+ Ok(value) -> insert(map, key, fun(Ok(value)))
+ Error(_) -> insert(map, key, fun(Error(Nil)))
}
}
fn do_fold(list, initial, fun) {
case list {
- | [] -> initial
- | [Pair(k, v) | tail] -> do_fold(tail, fun(k, v, initial), fun)
+ [] -> initial
+ [Pair(k, v) | tail] -> do_fold(tail, fun(k, v, initial), fun)
}
}
diff --git a/src/gleam/order.gleam b/src/gleam/order.gleam
index bc8ee06..cf3bc40 100644
--- a/src/gleam/order.gleam
+++ b/src/gleam/order.gleam
@@ -1,48 +1,48 @@
-pub enum Order =
- | Lt
- | Eq
- | Gt
-;
+pub enum Order {
+ Lt
+ Eq
+ Gt
+}
pub fn reverse(order) {
case order {
- | Lt -> Gt
- | Eq -> Eq
- | Gt -> Lt
+ Lt -> Gt
+ Eq -> Eq
+ Gt -> Lt
}
}
pub fn to_int(order) {
case order {
- | Lt -> -1
- | Eq -> 0
- | Gt -> 1
+ Lt -> -1
+ Eq -> 0
+ Gt -> 1
}
}
pub fn compare(a, b) {
case a, b {
- | Lt, Lt -> Eq
- | Lt, _ -> Lt
- | Eq, Eq -> Eq
- | Gt, Gt -> Eq
- | Eq, Gt -> Lt
- | _, _ -> Gt
+ Lt, Lt -> Eq
+ Lt, _ -> Lt
+ Eq, Eq -> Eq
+ Gt, Gt -> Eq
+ Eq, Gt -> Lt
+ _, _ -> Gt
}
}
pub fn max(a, b) {
case a, b {
- | Gt, _ -> Gt
- | Eq, Lt -> Eq
- | _, _ -> b
+ Gt, _ -> Gt
+ Eq, Lt -> Eq
+ _, _ -> b
}
}
pub fn min(a, b) {
case a, b {
- | Lt, _ -> Lt
- | Eq, Gt -> Eq
- | _, _ -> b
+ Lt, _ -> Lt
+ Eq, Gt -> Eq
+ _, _ -> b
}
}
diff --git a/src/gleam/result.gleam b/src/gleam/result.gleam
index cfde7ba..dce270c 100644
--- a/src/gleam/result.gleam
+++ b/src/gleam/result.gleam
@@ -3,49 +3,49 @@
pub fn is_ok(result) {
case result {
- | Error(_) -> False
- | Ok(_) -> True
+ Error(_) -> False
+ Ok(_) -> True
}
}
pub fn is_error(result) {
case result {
- | Ok(_) -> False
- | Error(_) -> True
+ Ok(_) -> False
+ Error(_) -> True
}
}
pub fn map(result, with fun) {
case result {
- | Ok(x) -> Ok(fun(x))
- | Error(e) -> Error(e)
+ Ok(x) -> Ok(fun(x))
+ Error(e) -> Error(e)
}
}
pub fn map_error(result, with fun) {
case result {
- | Ok(x) -> Ok(x)
- | Error(error) -> Error(fun(error))
+ Ok(x) -> Ok(x)
+ Error(error) -> Error(fun(error))
}
}
pub fn flatten(result) {
case result {
- | Ok(x) -> x
- | Error(error) -> Error(error)
+ Ok(x) -> x
+ Error(error) -> Error(error)
}
}
pub fn then(result, apply fun) {
case result {
- | Ok(x) -> fun(x)
- | Error(e) -> Error(e)
+ Ok(x) -> fun(x)
+ Error(e) -> Error(e)
}
}
pub fn unwrap(result, or default) {
case result {
- | Ok(v) -> v
- | Error(_) -> default
+ Ok(v) -> v
+ Error(_) -> default
}
}
diff --git a/test/gleam/list_test.gleam b/test/gleam/list_test.gleam
index 1cd0ff0..fcab9ce 100644
--- a/test/gleam/list_test.gleam
+++ b/test/gleam/list_test.gleam
@@ -85,8 +85,8 @@ pub fn map_test() {
pub fn traverse_test() {
let fun = fn(x) {
case x == 6 || x == 5 || x == 4 {
- | True -> Ok(x * 2)
- | False -> Error(x)
+ True -> Ok(x * 2)
+ False -> Error(x)
}
}
@@ -156,8 +156,8 @@ pub fn fold_right_test() {
pub fn find_map_test() {
let f = fn(x) {
case x {
- | 2 -> Ok(4)
- | _ -> Error(0)
+ 2 -> Ok(4)
+ _ -> Error(0)
}
}
diff --git a/test/gleam/map_test.gleam b/test/gleam/map_test.gleam
index fee689b..4c7b371 100644
--- a/test/gleam/map_test.gleam
+++ b/test/gleam/map_test.gleam
@@ -194,8 +194,8 @@ pub fn update_test() {
let inc_or_zero = fn(x) {
case x {
- | Ok(i) -> i + 1
- | Error(_) -> 0
+ Ok(i) -> i + 1
+ Error(_) -> 0
}
}