aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parent2a7d9061e4ec9c763053b078db2d98400bd08dd5 (diff)
downloadgleam_stdlib-80a8083b143eaa24218ec5fda16f947658fb823d.tar.gz
gleam_stdlib-80a8083b143eaa24218ec5fda16f947658fb823d.zip
Compile map module
Diffstat (limited to 'src')
-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
6 files changed, 91 insertions, 118 deletions
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) ->