Warn when using take_random/2 with a stream

34 views
Skip to first unread message

Devon Estes

unread,
Sep 24, 2020, 8:54:20 AM9/24/20
to elixir-l...@googlegroups.com
I realized today that Enum.take_random/2 can enter an infinite loop if someone calls it with an unbounded Stream as the enum. I also see that there‘s a warning of sorts (although nothing expressly mentioning a Stream as an enum), but I‘m not sure this is going to address user confusion by this behavior.

Would it make sense to emit a warning when using a Stream as an argument to take_random/2? Clearly since it‘s possible for this to _not_ get stuck in an infinite loop when using a steam we shouldn’t error, but a warning might at least help folks call something that converts the stream to a list before calling take_random/2.

Thoughts?
--

_________________
Devon Estes
+49 176 2356 4717
www.devonestes.com

José Valim

unread,
Sep 24, 2020, 9:14:46 AM9/24/20
to elixir-l...@googlegroups.com
I don't think it makes sense to explicitly check on Stream because:

1. Not all streams are infinite
2. Not all infinite streams are %Stream{}

So we would have both false positives and false negatives. Also note this problem happens with reduce, sum, count, etc.

--
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/CAGowJci%2BZ9hbg%3DVheXOqemF_PJ2YDQOn9aua0w-ejpYPHGnPwg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages