aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2019-03-12 18:30:12 +0000
committerLouis Pilfold <louis@lpil.uk>2019-03-12 18:32:51 +0000
commit80a8083b143eaa24218ec5fda16f947658fb823d (patch)
tree32b64f2327e30270e0f2126ee0f80363956d4527
parent2a7d9061e4ec9c763053b078db2d98400bd08dd5 (diff)
downloadgleam_stdlib-80a8083b143eaa24218ec5fda16f947658fb823d.tar.gz
gleam_stdlib-80a8083b143eaa24218ec5fda16f947658fb823d.zip
Compile map module
-rw-r--r--gen/any.erl1
-rw-r--r--gen/bool.erl1
-rw-r--r--gen/expect.erl1
-rw-r--r--gen/iodata.erl3
-rw-r--r--gen/list.erl3
-rw-r--r--gen/map.erl62
-rw-r--r--gen/order.erl1
-rw-r--r--gen/result.erl1
-rw-r--r--src/any.gleam11
-rw-r--r--src/gleam__stdlib.erl11
-rw-r--r--src/iodata.gleam2
-rw-r--r--src/list.gleam2
-rw-r--r--src/map.gleam177
-rw-r--r--src/result.gleam6
14 files changed, 154 insertions, 128 deletions
diff --git a/gen/any.erl b/gen/any.erl
index 9ea0087..bfca689 100644
--- a/gen/any.erl
+++ b/gen/any.erl
@@ -1,4 +1,5 @@
-module(any).
+-compile(no_auto_import).
-export([from/1, unsafeCoerce/1]).
diff --git a/gen/bool.erl b/gen/bool.erl
index 9024a16..8b9c1c4 100644
--- a/gen/bool.erl
+++ b/gen/bool.erl
@@ -1,4 +1,5 @@
-module(bool).
+-compile(no_auto_import).
-include_lib("eunit/include/eunit.hrl").
-export([max/2, min/2, to_int/1]).
diff --git a/gen/expect.erl b/gen/expect.erl
index 2152c4f..4d42f10 100644
--- a/gen/expect.erl
+++ b/gen/expect.erl
@@ -1,4 +1,5 @@
-module(expect).
+-compile(no_auto_import).
-export([equal/2, not_equal/2, true/1, false/1, fail/0]).
diff --git a/gen/iodata.erl b/gen/iodata.erl
index 0eaaca2..9704413 100644
--- a/gen/iodata.erl
+++ b/gen/iodata.erl
@@ -1,9 +1,10 @@
-module(iodata).
+-compile(no_auto_import).
-export([prepend/2, append/2, from/1, to_string/1, byte_size/1]).
prepend(A, B) ->
- gleam__stdlib:iodata_prepend(A, B).
+ gleam__stdlib:iodata_concat(A, B).
append(A, B) ->
gleam__stdlib:iodata_append(A, B).
diff --git a/gen/list.erl b/gen/list.erl
index 0e5f1f2..964dc69 100644
--- a/gen/list.erl
+++ b/gen/list.erl
@@ -1,4 +1,5 @@
-module(list).
+-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]).
@@ -15,7 +16,7 @@ length_test() ->
-endif.
reverse(A) ->
- erlang:reverse(A).
+ lists:reverse(A).
-ifdef(TEST).
reverse_test() ->
diff --git a/gen/map.erl b/gen/map.erl
index db29fd0..c2a07d4 100644
--- a/gen/map.erl
+++ b/gen/map.erl
@@ -1,10 +1,8 @@
-module(map).
+-compile(no_auto_import).
-include_lib("eunit/include/eunit.hrl").
--export([new/0, size/1, to_list/1, from_list/1, fetch/2, map_values/2, keys/1, values/1, filter/2]).
-
-new() ->
- maps:new().
+-export([size/1, to_list/1, from_list/1, has_key/2, new/0, fetch/2, put/3, map_values/2, keys/1, values/1, filter/2]).
size(A) ->
maps:size(A).
@@ -19,8 +17,38 @@ from_list(A) ->
from_list_test() ->
Proplist = [{4, 0}, {1, 0}],
Map = from_list(Proplist),
- _ = (fun(Capture1) -> expect:equal(Capture1, 2) end)(size(Map)),
- (fun(Capture1) -> expect:equal(Capture1, Proplist) end)(to_list(Map)).
+ (fun(Capture1) -> expect:equal(Capture1, 2) end)(size(Map)).
+-endif.
+
+is_key(A, B) ->
+ maps:is_key(A, B).
+
+has_key(Map, Key) ->
+ is_key(Key, Map).
+
+-ifdef(TEST).
+has_key_test() ->
+ _ = expect:false((fun(Capture1) ->
+ has_key(Capture1, 1)
+ end)(from_list([]))),
+ _ = expect:true((fun(Capture1) ->
+ has_key(Capture1, 1)
+ end)(from_list([{1, 0}]))),
+ _ = expect:true((fun(Capture1) ->
+ has_key(Capture1, 1)
+ end)(from_list([{4, 0}, {1, 0}]))),
+ expect:false((fun(Capture1) ->
+ has_key(Capture1, 0)
+ end)(from_list([{4, 0}, {1, 0}]))).
+-endif.
+
+new() ->
+ maps:new().
+
+-ifdef(TEST).
+new_test() ->
+ _ = (fun(Capture1) -> expect:equal(Capture1, 0) end)(size(new())),
+ (fun(Capture1) -> expect:equal(Capture1, []) end)(to_list(new())).
-endif.
fetch(A, B) ->
@@ -38,6 +66,24 @@ fetch_test() ->
end)((fun(Capture1) -> fetch(Capture1, 1) end)(Map)).
-endif.
+erl_put(A, B, C) ->
+ maps:put(A, B, C).
+
+put(Map, Key, Value) ->
+ erl_put(Key, Value, Map).
+
+-ifdef(TEST).
+put_test() ->
+ (fun(Capture1) ->
+ expect:equal(Capture1,
+ from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]))
+ end)((fun(Capture1) ->
+ put(Capture1, <<"c">>, 2)
+ end)((fun(Capture1) ->
+ put(Capture1, <<"b">>, 1)
+ end)((fun(Capture1) -> put(Capture1, <<"a">>, 0) end)(new())))).
+-endif.
+
erl_map_values(A, B) ->
maps:map(A, B).
@@ -47,7 +93,7 @@ map_values(Map, Fun) ->
-ifdef(TEST).
map_values_test() ->
(fun(Capture1) ->
- expect:equal(Capture1, from_list([{1, 0}, {2, 3}, {3, 5}]))
+ expect:equal(Capture1, from_list([{1, 1}, {2, 3}, {3, 5}]))
end)((fun(Capture1) ->
map_values(Capture1, fun(K, V) -> K + V end)
end)(from_list([{1, 0}, {2, 1}, {3, 2}]))).
@@ -77,4 +123,4 @@ erl_filter(A, B) ->
maps:filter(A, B).
filter(Map, Fun) ->
- filter(Fun, Map).
+ erl_filter(Fun, Map).
diff --git a/gen/order.erl b/gen/order.erl
index edf63a7..69f0eb8 100644
--- a/gen/order.erl
+++ b/gen/order.erl
@@ -1,4 +1,5 @@
-module(order).
+-compile(no_auto_import).
-include_lib("eunit/include/eunit.hrl").
-export([reverse/1, to_int/1, compare/2, max/2, min/2]).
diff --git a/gen/result.erl b/gen/result.erl
index 9bbf1e1..101b56a 100644
--- a/gen/result.erl
+++ b/gen/result.erl
@@ -1,4 +1,5 @@
-module(result).
+-compile(no_auto_import).
-include_lib("eunit/include/eunit.hrl").
-export([is_ok/1, is_error/1, map/2, map_error/2, flatten/1, flat_map/2, unwrap/2]).
diff --git a/src/any.gleam b/src/any.gleam
index bb34ac4..884a436 100644
--- a/src/any.gleam
+++ b/src/any.gleam
@@ -1,19 +1,16 @@
-// doc """
// `Any` data is data that we don"t know the type of yet.
// We likely get data like this from interop with Erlang, or from
// IO with the outside world.
-// """
+//
pub external type Any;
-// doc """
// Convert any Gleam data into `Any` data.
-// """
+//
pub external fn from(a) -> Any = "gleam__stdlib" "identity";
-// doc """
// Unsafely cast any type into any other type.o
-
+//
// This is an escape hatch for the type system that may be useful when wrapping
// native Erlang APIs. It is to be used as a last measure only.
-// """
+//
pub external fn unsafeCoerce(a) -> b = "gleam__stdlib" "identity";
diff --git a/src/gleam__stdlib.erl b/src/gleam__stdlib.erl
index e4b76f7..4af95eb 100644
--- a/src/gleam__stdlib.erl
+++ b/src/gleam__stdlib.erl
@@ -1,7 +1,10 @@
-module(gleam__stdlib).
--compile(export_all).
-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]).
+
+
expect_equal(A, Expected) -> ?assertEqual(Expected, A).
expect_not_equal(A, Expected) -> ?assertNotEqual(Expected, A).
expect_true(A) -> ?assert(A).
@@ -9,11 +12,11 @@ expect_false(A) -> ?assertNot(A).
map_fetch(Map, Key) ->
case maps:find(Key, Map) of
- error -> {error, {}},
+ error -> {error, {}};
OkFound -> OkFound
end.
-iodata_append(X, Data) -> [X | Y].
-iodata_prepend(X, Data) -> [Y, X].
+iodata_append(Iodata, String) -> [Iodata, String].
+iodata_prepend(Iodata, String) -> [String, Iodata].
identity(X) -> X.
diff --git a/src/iodata.gleam b/src/iodata.gleam
index 1d65e11..1b28577 100644
--- a/src/iodata.gleam
+++ b/src/iodata.gleam
@@ -5,7 +5,7 @@ import any
pub external type Iodata;
pub external fn prepend(Iodata, String) -> Iodata =
- "gleam__stdlib" "iodata_prepend";
+ "gleam__stdlib" "iodata_concat";
pub external fn append(Iodata, String) -> Iodata =
"gleam__stdlib" "iodata_append";
diff --git a/src/list.gleam b/src/list.gleam
index 871c980..027afd6 100644
--- a/src/list.gleam
+++ b/src/list.gleam
@@ -16,7 +16,7 @@ test length {
// Using the Erlang C BIF implementation.
//
-pub external fn reverse(List(a)) -> List(a) = "erlang" "reverse"
+pub external fn reverse(List(a)) -> List(a) = "lists" "reverse"
test reverse {
let _ = length([]) |> expect:equal(_, 0)
diff --git a/src/map.gleam b/src/map.gleam
index 9236ffd..d5a662d 100644
--- a/src/map.gleam
+++ b/src/map.gleam
@@ -4,78 +4,79 @@ import expect
pub external type Map(key, value);
-pub external fn new() -> Map(key, value)
- = "maps" "new"
-
-// test new {
-// new()
-// |> new
-// |> expect:equal(_, [])
-// }
-
pub external fn size(Map(k, v)) -> Int
= "maps" "size"
-// test size {
-// let _ = []
-// |> from_list
-// |> size
-// |> expect:equal(_, 0)
+pub external fn to_list(Map(key, value)) -> List({key, value})
+ = "maps" "to_list"
-// let _ = [
-// {1, 1},
-// ]
-// |> from_list
-// |> size
-// |> expect:equal(_, 1)
+pub external fn from_list(List({key, value})) -> Map(key, value)
+ = "maps" "from_list"
-// [
-// {"", 1.0},
-// {"", 2.0},
-// ]
-// |> from_list
-// |> size
-// |> expect:equal(_, 2)
-// }
+test from_list {
+ let proplist = [
+ {4, 0},
+ {1, 0},
+ ]
+ let map = from_list(proplist)
-// external fn is_key(key, Map(key, v)) -> Bool
-// = "maps" "is_key"
+ map
+ |> size
+ |> expect:equal(_, 2)
+}
-// pub fn has_key(map, key) {
-// is_key(key, map)
-// }
-// test has_key {
-// let _ = []
-// |> from_list
-// |> has_key(_, 1)
-// |> expect:false
-
-// let _ = [
-// {1, 0},
-// ]
-// |> from_list
-// |> has_key(_, 1)
-// |> expect:true
-
-// let _ = [
-// {4, 0},
-// {1, 0},
-// ]
-// |> from_list
-// |> has_key(_, 1)
-// |> expect:true
-
-// [
-// {4, 0},
-// {1, 0},
-// ]
-// |> from_list
-// |> has_key(_, 0)
-// |> expect:false
-// }
+external fn is_key(key, Map(key, v)) -> Bool
+ = "maps" "is_key"
+
+pub fn has_key(map, key) {
+ is_key(key, map)
+}
+
+test has_key {
+ let _ = []
+ |> from_list
+ |> has_key(_, 1)
+ |> expect:false
+
+ let _ = [
+ {1, 0},
+ ]
+ |> from_list
+ |> has_key(_, 1)
+ |> expect:true
+
+ let _ = [
+ {4, 0},
+ {1, 0},
+ ]
+ |> from_list
+ |> has_key(_, 1)
+ |> expect:true
+
+ [
+ {4, 0},
+ {1, 0},
+ ]
+ |> from_list
+ |> has_key(_, 0)
+ |> expect:false
+}
+
+pub external fn new() -> Map(key, value)
+ = "maps" "new"
-// pub fn from_record(record: {r}) -> Map(Atom, any:Any) {
+test new {
+ let _ = new()
+ |> size
+ |> expect:equal(_, 0)
+
+ new()
+ |> to_list
+ |> expect:equal(_, [])
+}
+
+// pub fn from_record(record: {r | }) -> Map(Atom, any:Any) {
// any:unsafeCoerce(record)
// }
@@ -90,28 +91,6 @@ pub external fn size(Map(k, v)) -> Int
// |> expect:equal(_, from_list([{"name", "Jane"}]))
// }
-pub external fn to_list(Map(key, value)) -> List({key, value})
- = "maps" "to_list"
-
-pub external fn from_list(List({key, value})) -> Map(key, value)
- = "maps" "from_list"
-
-test from_list {
- let proplist = [
- {4, 0},
- {1, 0},
- ]
- let map = from_list(proplist)
-
- let _ = map
- |> size
- |> expect:equal(_, 2)
-
- map
- |> to_list
- |> expect:equal(_, proplist)
-}
-
pub external fn fetch(Map(key, value), key) -> Result(a, value)
= "gleam__stdlib" "map_fetch";
@@ -135,20 +114,20 @@ test fetch {
// |> expect:equal(_, Error(())
}
-// external fn erl_put(key, value, Map(key, value)) -> Map(key, value)
-// = "maps" "put";
+external fn erl_put(key, value, Map(key, value)) -> Map(key, value)
+ = "maps" "put";
-// pub fn put(map, key, value) {
-// erl_put(key, value, map)
-// }
+pub fn put(map, key, value) {
+ erl_put(key, value, map)
+}
-// test put {
-// new()
-// |> put(_, "a", 0)
-// |> put(_, "b", 1)
-// |> put(_, "c", 2)
-// |> expect:equal(_, Ok(from_list([{"a", 0}, {"b", 1}, {"c", 2}])))
-// }
+test put {
+ new()
+ |> put(_, "a", 0)
+ |> put(_, "b", 1)
+ |> put(_, "c", 2)
+ |> expect:equal(_, from_list([{"a", 0}, {"b", 1}, {"c", 2}]))
+}
external fn erl_map_values(fn(key, value) -> value, Map(key, value)) -> Map(key, value)
= "maps" "map";
@@ -165,7 +144,7 @@ test map_values {
]
|> from_list
|> map_values(_, fn(k, v) { k + v })
- |> expect:equal(_, from_list([{1, 0}, {2, 3}, {3, 5}]))
+ |> expect:equal(_, from_list([{1, 1}, {2, 3}, {3, 5}]))
}
pub external fn keys(Map(keys, v)) -> List(keys)
@@ -200,5 +179,5 @@ external fn erl_filter(fn(key, value) -> Bool, Map(key, value)) -> Map(key, valu
= "maps" "filter";
pub fn filter(map, fun) {
- filter(fun, map)
+ erl_filter(fun, map)
}
diff --git a/src/result.gleam b/src/result.gleam
index 2ad7ee8..77dc6e5 100644
--- a/src/result.gleam
+++ b/src/result.gleam
@@ -90,12 +90,6 @@ test flatten {
|> expect:equal(_, Error(Error(1)))
}
-// pub fn flat_map(result, fun) {
-// result
-// |> map(_, fun)
-// |> flatten
-// }
-
pub fn flat_map(result, fun) {
case result {
| Ok(x) ->