diff options
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)). |