So Enum.drop makes perfect sense to me - it drops the element at the exact position I think it should and returns a list.
Enum.drop_every has me tearing my hair out, so there’s something I’m meant to understand which I’m just not getting, and looking at the documentation for it isn’t helping at all.
Enum.drop_every docs says it
Returns a list of every
nth
element in theenumerable
dropped, starting with the first element.
The first element is always dropped, unlessnth
is 0.
The second argument specifying everynth
element must be a non-negative integer.
Ok, that makes sense. So If I take a list of 10 elements and I say drop every 2, I’d expect it to return all the even numbers … and it does!:
iex(1)> Enum.drop_every([1,2,3,4,5,6,7,8,9,10], 2)
[2, 4, 6, 8, 10]
So I lost the elements at position 1, 3, 5, 7, 9 … yea! This makes perfect sense.
Ok, so now I want to drop all the odds, or every 3rd element.
iex(2)> Enum.drop_every([1,2,3,4,5,6,7,8,9,10], 3)
[2, 3, 5, 6, 8, 9]
So I lose elements at positions 1, 4, 7, 10 … hm, ok maybe it makes sense, so I would have expected it to keep 1, but it did say that the first element is always droped. Fine, ok so if I count over from ‘4’ 3 places it’ll drop the 7, then plus 3 again the 10 gets dropped… alright. So how do I just drop the odds? The counting starts after the first element is dropped, because I was thinking it should have returned [3,5,7,9].
Hmm, what if I say drop every 4th element?
iex(3)> Enum.drop_every([1,2,3,4,5,6,7,8,9,10], 4)
[2, 3, 4, 6, 7, 8, 10]
So we lost elements at position 1, 5, 9. So If I assume the first element is always dropped then count over by 4 positions and drop the remainder list is what’s left.
In what cases is this useful to me as an Elixir programmer? Maybe I’m not thinking logically, but if I wanted to just get a list of the odd positions in a list, I’m sure there’s a better way to do it now, but I’m struggling to understand why i would ever use Enum.drop_every unless the behavior of not always dropping the first element was true. Because then if I did this:
iex(4)> Enum.drop_every([1,2,3,4,5,6,7,8,9,10], 3)
I would happily get:
[3, 6, 9]
So now I can think of Enum.drop_every as a divisor of a list of elements. If I have a list of 100 elements and I drop every 2, then I would get all the even positions back, or divide the list by 10, I should get a list of 10 elements back.
Help me change my thinking here. I can’t believe something so basic is giving me a hard time, but I’m obviously not thinking about it the right way.