[erlang-questions] More elegant way to
Nathaniel Waisbrot
nathaniel@REDACTED
Sun Jun 24 19:58:22 CEST 2018
> else if (erl_match(msg_pattern_b, emsg->msg)) {
> ETERM *term = erl_var_content(msg_pattern_b, "Term");
> response = erl_format("{ok, ~w}",erl_mk_string(floats_as_string()));
> erl_free_term(term);
> }
>
Your sample code looks either buggy or incomplete (you create `term` and then call `erl_free_term` on it without ever using it).
If understand your question, you want to return (e.g.) `{ok, [1.0, 1.1, 1.2, 2.0]}`. Right now you're returning something like `{ok, "[1.0, 1.1, 1.2, 2.0]"}` and then parsing the values out of the char-list in BEAM-land?
How about (untested)
```
ETERM* floats_to_list(float clist[], size_t length) {
ETERM* elist[length];
unsigned i;
for (i = 0; i < length; i++) {
elist[i] = erl_mk_float(clist[i]);
}
return erl_mk_list(elist, length);
}
void top_function() {
float *list_of_floats;
size_t size_of_list;
ETERM *response;
// ...
// somehow get these values loaded
// ...
response = floats_to_list(list_of_floats, size_of_list);
send_response(response);
// ...
}
```
More information about the erlang-questions
mailing list