#Idiomatic Gleam way to handle Erlang Function Pattern Matching

1 messages · Page 1 of 1 (latest)

lunar jay
#

I have some erlang functions that utilize guards.
I don't /have/ to expose all of these, but I would like to.

i2s(I) ->
    integer_to_list(I).

a2s(A) ->
    atom_to_list(A).

n2s(A) when is_float(A)   -> f2s(A);
n2s(A) when is_integer(A) -> i2s(A);
n2s([])                   -> [];
n2s([H|T])                -> [n2s(H)," "|n2s(T)].

f2s(I) when is_integer(I) ->
    i2s(I);
f2s(F) ->    
    remove_leading_blanks(flatten(io_lib:format("~8.2f", [F]))).

remove_leading_blanks([$\s|T]) -> remove_leading_blanks(T);
remove_leading_blanks(X)       -> X.

flatten(L) ->
    binary_to_list(list_to_binary(L)).
#

I would assume it would be something like

@external("erlang", "eg_pdf_op", "n2s")
pub fn n2s_float(input: Float) -> Charlist

@external("erlang", "eg_pdf_op", "n2s")
pub fn n2s_int(input: Int) -> Charlist

...