Baby steps with a nif
Sunday, 14th August, 2011
simple_nif is an erlang NIF which takes a list of integers and returns a record, called params, containing the sum, the mean, and the quartiles of the input list:
1> simple:get_params([1,2,3,4,5,6,7]). {params,28,4.0,{2.0,4.0,6.0}}
The main point of the exercise was the C interface between erlang and the C functions. The file simple_nif.c shows how to parse the input list from erlang into a C array, and how to assemble the results into an erlang tuple to return.
More details in the README.
I am releasing the code under the ISC license.
References
- Erlang NIF documentation
- Erlang NIF tutorial
- pytherl_utils.c: usage of enif_get_list_cell
- Erlang and OTP in Action: Chapter 12 has a section on NIFs