Thoughts on `Enum.compact/1`

3,898 views
Skip to first unread message

Patrick Van Stee

unread,
Aug 26, 2013, 6:40:33 PM8/26/13
to elixir-l...@googlegroups.com
Would implementing an `Enum.compact/1` be useful to anyone? It would basically just be the same as calling `Enum.reject` with `&nil?/1`.

Here's an example:

iex> Enum.compact([1, nil, 3, nil, 5])  
[1, 3, 5]

Gustavo Brunoro

unread,
Aug 26, 2013, 9:20:25 PM8/26/13
to elixir-l...@googlegroups.com
In my opinion it's just noise, at it's a really trivial function.
Speaking of that, what's the point of having both Enum.reject and Enum.filter?


2013/8/26 Patrick Van Stee <van...@bignerdranch.com>

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Peter Minten

unread,
Aug 27, 2013, 4:32:34 AM8/27/13
to elixir-l...@googlegroups.com
Probably that reject is quite common and the rather simple function for
it helps clearly express the function of the code while filter with an
inverted test requires more thinking.

I agree that it's a bit trivial.

Steve Downey

unread,
Aug 28, 2013, 12:49:33 PM8/28/13
to elixir-l...@googlegroups.com
This is probably my Ruby experience/bias exposing itself but:

- removing nil elements from a collection is a common pattern
- Enum.compact(list) is more intention-revealing than Enum.reject(list, &(nil?(&1)))

Sam Davies

unread,
May 17, 2017, 6:28:30 AM5/17/17
to elixir-lang-core
+1 for this

OvermindDL1

unread,
May 17, 2017, 11:14:56 AM5/17/17
to elixir-lang-core
If you use an erlang nil `[]` (the empty list) instead of a Elixir's `nil` (which is an atom, which is weird) as nil's then you can just use `:lists.flatten`.

Paul Schoenfelder

unread,
May 17, 2017, 11:29:15 AM5/17/17
to elixir-l...@googlegroups.com
This is probably my Ruby experience/bias exposing itself but:

- removing nil elements from a collection is a common pattern
- Enum.compact(list) is more intention-revealing than Enum.reject(list, &(nil?(&1)))

I really don't agree that `Enum.compact(list)` is somehow more revealing than `Enum.reject(list, &is_nil/1)` - it is clear what the latter will do, it is not clear what the former will do. I think there is a lot more of the Ruby bias in this statement than you think ;). I'm not sold on the idea of adding a function to the standard library which is so trivially written with functions that already exist, at the sacrifice of clarity.

Paul

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/0bcae60f-2dca-466a-8d62-e03d75028eee%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

binaryseed

unread,
May 17, 2017, 12:46:55 PM5/17/17
to elixir-lang-core
It's even clearer written like this:

Enum.reject(list, &is_nil/1)



On Wednesday, May 17, 2017 at 8:29:15 AM UTC-7, Paul Schoenfelder wrote:
This is probably my Ruby experience/bias exposing itself but:

- removing nil elements from a collection is a common pattern
- Enum.compact(list) is more intention-revealing than Enum.reject(list, &(nil?(&1)))

I really don't agree that `Enum.compact(list)` is somehow more revealing than `Enum.reject(list, &is_nil/1)` - it is clear what the latter will do, it is not clear what the former will do. I think there is a lot more of the Ruby bias in this statement than you think ;). I'm not sold on the idea of adding a function to the standard library which is so trivially written with functions that already exist, at the sacrifice of clarity.

Paul
On Wed, May 17, 2017 at 10:14 AM, OvermindDL1 <overm...@gmail.com> wrote:
If you use an erlang nil `[]` (the empty list) instead of a Elixir's `nil` (which is an atom, which is weird) as nil's then you can just use `:lists.flatten`.


On Wednesday, May 17, 2017 at 4:28:30 AM UTC-6, Sam Davies wrote:
+1 for this

On Wednesday, August 28, 2013 at 5:49:33 PM UTC+1, Steve Downey wrote:
This is probably my Ruby experience/bias exposing itself but:

- removing nil elements from a collection is a common pattern
- Enum.compact(list) is more intention-revealing than Enum.reject(list, &(nil?(&1)))


On Monday, August 26, 2013 3:40:33 PM UTC-7, Patrick Van Stee wrote:
Would implementing an `Enum.compact/1` be useful to anyone? It would basically just be the same as calling `Enum.reject` with `&nil?/1`.

Here's an example:

iex> Enum.compact([1, nil, 3, nil, 5])  
[1, 3, 5]

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages