Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is lists:zf/2 (Erlang)?

Tags:

erlang

Why is lists:zf/2 an alias for filtermap/2 in Erlang?

It's an undocumented but exported function in the lists: module. Here is the implementation:

zf(F, L) ->
    filtermap(F, L).

What I'm baffled by is:

  • what's it for?
  • what does "zf" stand for? "Zermelo-Fraenkel"?
like image 299
Max Heiber Avatar asked Nov 13 '20 12:11

Max Heiber


People also ask

Is Erlang a list?

In Erlang, Lists are created by enclosing the values in square brackets. Following is a simple example of creating a list of numbers in Erlang.

How do I find the length of a list in Erlang?

You can use length() to find the length of a list, and can use list comprehensions to filter your list. num(L) -> length([X || X <- L, X < 1]).


2 Answers

As Robert Virding, a co-inventor of Erlang, wrote in 2016:

The lists:zf/2 function came before list comprehensions and is a combination of map and filter. Like describing a set from a set of rules. Hence the Zermelo-Fraenkel based name. It is also called lists:filtermap/2 but where’s the fun in that?

So, your guess is absolutely correct.

like image 123
bereal Avatar answered Oct 11 '22 16:10

bereal


My old answer is perfectly correct, it does stand for Zermelo-Fraenkel. The name was chosen to be a bit of a joke. We could do that in those days.

It is similar to why we use 'reductions' in process info as a measure of how much work a process has done, basically the number of function calls. Originally, way back when, Erlang was implemented in Prolog and there they had reductions not function calls and we decided to keep it as a bit of a joke. Any suggestions that it has something to do with the theory of functional languages have got it wrong.

like image 39
rvirding Avatar answered Oct 11 '22 16:10

rvirding