From a974efd66ed4904ce7c9cabc38c3ec0563715b8f Mon Sep 17 00:00:00 2001 From: inoas Date: Wed, 10 Aug 2022 15:14:52 +0200 Subject: //erl[1,2|3] output for inspecting improper Erlang lists --- src/gleam_stdlib.erl | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index abb3b45..0d817fe 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -349,7 +349,10 @@ inspect(Binary) when is_binary(Binary) -> ["<<", lists:join(", ", Segments), ">>"] end; inspect(Elements) when is_list(Elements) -> - ["[", inspect_list(Elements), "]"]; + case is_proper_list(Elements) of + true -> ["[", inspect_proper_list(Elements), "]"]; + false -> ["//erl[", inspect_improper_list(Elements), "] %% improper list"] + end; inspect(Any) when is_tuple(Any) % Record constructors andalso is_atom(element(1, Any)) andalso element(1, Any) =/= false @@ -374,14 +377,27 @@ inspect(Any) when is_function(Any) -> inspect(Any) -> ["//erl(", io_lib:format("~p", [Any]), ")"]. -inspect_list([]) -> +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(_NthCall, []) -> []; -inspect_list([Head]) -> - [inspect(Head)]; -inspect_list([First | Rest]) -> - [inspect(First), <<", ">> | inspect_list(Rest)]; -inspect_list(ImproperTail) -> - [<<"..">>, inspect(ImproperTail)]. +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)]. float_to_string(Float) when is_float(Float) -> erlang:iolist_to_binary(io_lib_format:fwrite_g(Float)). -- cgit v1.2.3