diff options
author | inoas <mail@inoas.com> | 2022-08-10 15:14:52 +0200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-08-11 09:56:08 +0100 |
commit | a974efd66ed4904ce7c9cabc38c3ec0563715b8f (patch) | |
tree | f8c9b868ae82fd532e713876f28a313063b44b39 /src/gleam_stdlib.erl | |
parent | 81c7284468739bc64a65527d126f54fd8dff4120 (diff) | |
download | gleam_stdlib-a974efd66ed4904ce7c9cabc38c3ec0563715b8f.tar.gz gleam_stdlib-a974efd66ed4904ce7c9cabc38c3ec0563715b8f.zip |
//erl[1,2|3] output for inspecting improper Erlang lists
Diffstat (limited to 'src/gleam_stdlib.erl')
-rw-r--r-- | src/gleam_stdlib.erl | 32 |
1 files changed, 24 insertions, 8 deletions
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)). |