frellus

frellus

Enum.drop_every - make it make sense

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 the enumerable dropped, starting with the first element.
The first element is always dropped, unless nth is 0.
The second argument specifying every nth 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.

Most Liked

al2o3cr

al2o3cr

Enum.drop_every was specifically added to extend the take -> drop relationship pattern to take_every:

https://github.com/elixir-lang/elixir/pull/4698

Odds would be Enum.take_every(list, 2).

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

A practical use case where Enum.drop_every is the right function to use does not readily come to my mind. From a “makes sense” perspective though I don’t know that a concrete use case is required. The idea of removing every nth item, starting with the first, is pretty cogent / well defined on its own.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

This bit isn’t right. Odd numbers are also every other element, just offset by 1.

I think you understand how the function works, I just think you’re confused about whether it’s supposed to be a function you’d use to do even or odd numbers.

I wouldn’t try to solve the problem of doing the even or odd numbers with drop_every, I’d solve that with filter or reject where you keep or reject numbers that divide by 2.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement