aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-08-10 20:24:11 +0200
committerLouis Pilfold <louis@lpil.uk>2022-08-11 09:56:08 +0100
commitaa6a32fdb1d8f43db1c9957c9966f4c46b898229 (patch)
tree09bd311405fb1315ce2ca5f60426ab33fd52d26c
parent4959c80c687689dfb5383ddc7c2d1e707cd4dd7e (diff)
downloadgleam_stdlib-aa6a32fdb1d8f43db1c9957c9966f4c46b898229.tar.gz
gleam_stdlib-aa6a32fdb1d8f43db1c9957c9966f4c46b898229.zip
single pass proper/improper list inspection
-rw-r--r--src/gleam_stdlib.erl39
-rw-r--r--test/gleam/string_test.gleam4
2 files changed, 17 insertions, 26 deletions
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 8417e7c..c06854a 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -348,10 +348,10 @@ inspect(Binary) when is_binary(Binary) ->
Segments = [erlang:integer_to_list(X) || <<X>> <= Binary],
["<<", lists:join(", ", Segments), ">>"]
end;
-inspect(Elements) when is_list(Elements) ->
- case is_proper_list(Elements) of
- true -> ["[", inspect_proper_list(Elements), "]"];
- false -> ["//erl[", inspect_improper_list(Elements), "] %% improper list"]
+inspect(List) when is_list(List) ->
+ case inspect_list(firstCall, properList, List) of
+ {properList, Elements} -> ["[", Elements, "]"];
+ {improperList, Elements} -> ["//erl[", Elements, "] %% improper list"]
end;
inspect(Any) when is_tuple(Any) % Record constructors
andalso is_atom(element(1, Any))
@@ -377,25 +377,18 @@ inspect(Any) when is_function(Any) ->
inspect(Any) ->
["//erl(", io_lib:format("~p", [Any]), ")"].
-is_proper_list([]) ->
- true;
-is_proper_list([_Head | Tail]) ->
- is_proper_list(Tail);
-is_proper_list(ImproperTail) when not is_list(ImproperTail) ->
- false.
-
-inspect_proper_list(List) ->
- lists:join(<<", ">>, lists:map(fun inspect/1, List)).
-
-inspect_improper_list(Elements) ->
- do_inspect_improper_list(firstCall, Elements).
-
-do_inspect_improper_list(firstCall, [First | Rest]) ->
- [inspect(First) | do_inspect_improper_list(nthCall, Rest)];
-do_inspect_improper_list(nthCall, [First | Rest]) ->
- [ <<",">>, inspect(First) | do_inspect_improper_list(nthCall, Rest)];
-do_inspect_improper_list(nthCall, ImproperTail) ->
- [<<"|">>, inspect(ImproperTail)].
+inspect_list(_recursionCount, properList, []) ->
+ {properList, []};
+inspect_list(firstCall, properList, [Head]) ->
+ {properList, [inspect(Head)]};
+inspect_list(firstCall, properList, [First | Rest]) ->
+ {ListKind, Elements} = inspect_list(nthCall, properList, Rest),
+ {ListKind, [inspect(First) | Elements]};
+inspect_list(nthCall, properList, [First | Rest]) ->
+ {ListKind, Elements} = inspect_list(nthCall, properList, Rest),
+ {ListKind, [<<", ">>, inspect(First) | Elements]};
+inspect_list(nthCall, properList, ImproperTail) ->
+ {improperList, [<<" | ">>, 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 b025f5c..d817a35 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -791,12 +791,10 @@ if erlang {
|> string.inspect()
|> should.equal("\"abc\"")
}
-}
-if erlang {
pub fn improper_list_inspect_test() {
let list = improper_list_append(1, 2, 3)
- assert "//erl[1,2|3] %% improper list" = string.inspect(list)
+ assert "//erl[1, 2 | 3] %% improper list" = string.inspect(list)
}
// Warning: The type of this function is incorrect