aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-08-10 08:58:21 +0100
committerLouis Pilfold <louis@lpil.uk>2022-08-10 08:58:21 +0100
commit3fba785183210dd935a99632a653302e6f832ba4 (patch)
treeaf69fffb72709f87b9fbc4909867432db56f283d
parent95425ffdc5407e9ec60efed9b8d4fa7c1fdfb764 (diff)
downloadgleam_stdlib-3fba785183210dd935a99632a653302e6f832ba4.tar.gz
gleam_stdlib-3fba785183210dd935a99632a653302e6f832ba4.zip
Use different syntax for improper lists
-rw-r--r--src/gleam_stdlib.erl19
-rw-r--r--test/gleam/string_test.gleam9
-rw-r--r--test/gleam_stdlib_test_ffi.erl5
3 files changed, 23 insertions, 10 deletions
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 9fc1c4a..b2aa135 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -348,9 +348,8 @@ inspect(Binary) when is_binary(Binary) ->
Segments = [erlang:integer_to_list(X) || <<X>> <= Binary],
["<<", lists:join(", ", Segments), ">>"]
end;
-inspect(List) when is_list(List) ->
- Elements = lists:join(<<", ">>, do_map_listish(fun inspect/1, List)),
- ["[", Elements, "]"];
+inspect(Elements) when is_list(Elements) ->
+ ["[", inspect_list(Elements), "]"];
inspect(Any) when is_tuple(Any) % Record constructors
andalso is_atom(element(1, Any))
andalso element(1, Any) =/= false
@@ -375,13 +374,15 @@ inspect(Any) when is_function(Any) ->
inspect(Any) ->
["//erl(", io_lib:format("~p", [Any]), ")"].
-do_map_listish(_Fn, []) ->
+
+inspect_list([]) ->
[];
-do_map_listish(Fn, List) when is_list(List) ->
- [Head | Tail] = List,
- [Fn(Head) | do_map_listish(Fn, Tail)];
-do_map_listish(Fn, Tail) when not is_list(Tail) ->
- [Fn(Tail)].
+inspect_list([Head]) ->
+ [inspect(Head)];
+inspect_list([First | Rest]) ->
+ [inspect(First), <<", ">> | inspect_list(Rest)];
+inspect_list(ImproperTail) ->
+ [<<"...">>, inspect(ImproperTail)].
float_to_string(Float) when is_float(Float) ->
erlang:iolist_to_binary(io_lib_format:fwrite_g(Float)).
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index 46b5e91..acc00d7 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -792,3 +792,12 @@ if erlang {
|> should.equal("\"abc\"")
}
}
+
+pub fn improper_list_inspect_test() {
+ let list = improper_list_append(1, 2)
+ assert "[1, ...2]" = string.inspect(list)
+}
+
+// Warning: The type of this function is incorrect
+external fn improper_list_append(anything1, anything2) -> List(anything) =
+ "gleam_stdlib_test_ffi" "improper_list_append"
diff --git a/test/gleam_stdlib_test_ffi.erl b/test/gleam_stdlib_test_ffi.erl
index 6be4cb6..c26a879 100644
--- a/test/gleam_stdlib_test_ffi.erl
+++ b/test/gleam_stdlib_test_ffi.erl
@@ -2,7 +2,7 @@
-export([
main/0, should_equal/2, should_not_equal/2, should_be_ok/1,
- should_be_error/1
+ should_be_error/1, improper_list_append/2
]).
-include_lib("eunit/include/eunit.hrl").
@@ -38,3 +38,6 @@ should_be_ok(A) ->
should_be_error(A) ->
?assertMatch({error, _}, A),
nil.
+
+improper_list_append(X, Y) ->
+ [X | Y].