Proposal: Enum.reject for Enum.compact

277 views
Skip to first unread message

Bruce Tate

unread,
Nov 17, 2016, 10:35:59 AM11/17/16
to elixir-l...@googlegroups.com
Every couple of months, we get a request for Enum.compact. So far, the main argument against it is that Enum.reject list, &is_nil/1 is sufficient. 

When you compare the difference in a pipe, you can see a significant difference in readability. 

It really is a pervasive use case. 

list
|> nil_or_result
|> compact

versus

list 
|> nil_or_result
|> reject(&isnil/1)



One possible solution is for Enum.reject to take a default function as an argument. I suggest a default of  fn(x) -> x end

That would make the above example: 

list
|> nil_or_result
|> reject


which reads just fine, I think. There is ample precedent for meaningful defaults in enum. is_nil/1 and &(&1) are meaningful defaults. 

Thoughts? 

-bt
--
Bruce Tate
President, RapidRed, LLC
Phone: 512.772.4312
Fax: 512 857-0415

Author of Seven Languages in Seven Weeks, Deploying Rails Applications, From Java to Ruby, Rails: Up and Running, Beyond Java, 6 others.

Tallak Tveide

unread,
Nov 17, 2016, 12:40:54 PM11/17/16
to elixir-lang-core
I assume you mean the default function should be

'''
fn x -> !x end
'''

To reject any falsey value. It does seem reasonable, but I dont think it is nearly as readable as 'compact', so I vote nay to this one...

In fact, isn't this a better option? (Or equally good)

'''
def filter(enum, fun \\ fn x -> x end)
'''

Actually I have also thought that a 'unity' or 'itself' function should be part of stdlib, to make this possible as:

'''
list
|> Enum.filter(&itself)
'''

Louis Pop

unread,
Nov 17, 2016, 12:48:20 PM11/17/16
to elixir-lang-core

Hiya

I'd like an identity function in Kernel, that's for sure.

I think an identity function being the default argument to filter would be quite nice. I find reject a bit strange, I don't set why it exists when we have filter.

Cheers,
Louis


--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/06bc7738-3ed9-4a21-9fce-b7eb3fecd600%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

OvermindDL1

unread,
Nov 17, 2016, 1:33:51 PM11/17/16
to elixir-lang-core
`identity : 'a -> 'a` is the most common function declaration like that in almost every functional language, would be nice to have in kernel.

Drew Olson

unread,
Nov 17, 2016, 2:08:12 PM11/17/16
to elixir-l...@googlegroups.com
I've also often wished for an identity/1 in Kernel, especially because you cannot easily represent it with the short function syntax.

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/07571247-7dba-4a21-948d-89f7ac335f92%40googlegroups.com.

Drew Olson

unread,
Nov 17, 2016, 2:12:11 PM11/17/16
to elixir-l...@googlegroups.com
While we're wishing for things, I've also wanted constant/1 a few times:

constant(3).()
# => 3

Especially for things like creation of Agents.

Drew Olson

unread,
Nov 17, 2016, 2:14:39 PM11/17/16
to elixir-l...@googlegroups.com
Ignore my comment about not being about to use the shorter function syntax. I was confused about the precedence of &.

On Thu, Nov 17, 2016 at 1:08 PM, Drew Olson <dr...@drewolson.org> wrote:

OvermindDL1

unread,
Nov 17, 2016, 5:13:35 PM11/17/16
to elixir-lang-core, dr...@drewolson.org
Yeah you could do `&(&1)` or so for an inline identity, though definitely not as clear.  :-)

Constant is not as succinct though:  `&(fn->&1 end)`
And still not clear.  ^.^

Bruce Tate

unread,
Nov 18, 2016, 4:01:17 PM11/18/16
to elixir-l...@googlegroups.com
The identify function is effectively already the default for Enum.all? and any?, and a few others as well. 

Why not make it the default for Enum.reject too?

-bt

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/8c4a990c-77be-4d00-857b-9a26c59715f9%40googlegroups.com.

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



--

Peter Hamilton

unread,
Nov 18, 2016, 4:04:55 PM11/18/16
to elixir-l...@googlegroups.com
Bruce: As pointed out previously, It's a bad default for Enum.reject. It would leave you with just the nil values. It might be an ok default for Enum.filter though.

--
Bruce Tate
President, RapidRed, LLC
Phone: 512.772.4312
Fax: 512 857-0415

Author of Seven Languages in Seven Weeks, Deploying Rails Applications, From Java to Ruby, Rails: Up and Running, Beyond Java, 6 others.

--
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.

Chris Keathley

unread,
Nov 19, 2016, 2:24:28 PM11/19/16
to elixir-lang-core
It seems like an identity function would be a better default for both Enum.filter and Enum.reject. Then instead of using Enum.reject(&is_nil/1) you would use Enum.filter. I'm not sure if defaulting to identity could return surprising results. But it seems like it might be OK since thats what Enum.any? and Enum.all? already do.

Viza-

unread,
Jan 27, 2017, 6:35:38 PM1/27/17
to elixir-lang-core

Yes, would be really handy to have these features for .filter and .reject


четверг, 17 ноября 2016 г., 18:35:59 UTC+3 пользователь Bruce Tate написал:
Reply all
Reply to author
Forward
0 new messages