mackeyja92

mackeyja92

`Enum.map_every/3` but with start offset

There is sometimes cases where you want to map every nth item starting on the nth item. Eg map every 2nd item starting on the 2nd one. Currently the map_every/3 always gives back the first item in the enumerable and then every nth item. I propose to add an optional parameter to the function for the offset for the first item. Another option would be to add a parameter to start on the nth item instead but that’s probably less flexible than the offset. What’s your thoughts? Is there another way to do this with the existing APIs?

Most Liked

tomekowal

tomekowal

I believe that is a perfectly fine solution. If you need to perform different operations on even and odd elements you could also try chunking the list.

E.g.

[1,2,3,4,5,6]
|> Enum.chunk_every(2)
# [[1,2], [3,4], [5,6]]
|> Enum.map(fn [first, second] -> [first*2, second*3] end)
|> List.flatten
# [2, 6, 6, 12, 10, 18]

That doubles every odd element and triples every even one.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@mackeyja92 I’d probably use Enum.drop.
eg: “Map every 4th thing starting on the 2nd thing”

list |> Enum.drop(2) |> Enum.map_every(4, fun)
mackeyja92

mackeyja92

The problem with drop is it removes the items from the list. If you only need to do a certain operation on nth items but need to do another operation on the whole list after you’re stuck with removing items and storing in a temporary list and then pushing them back on the front after the map_every.

A real world example is the Luhn checksum.

# Reverse them first                                                                                                                                                                                                                                                                        
numbers = Enum.reverse(numbers)                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                            
# Pop the first one off because elixir always does the first one when using map_every                                                                                                                                                                                                       
{first, numbers} = List.pop_at(numbers, 0)                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                            
numbers                                                                                                                                                                                                                                                                                     
|> Enum.map_every(2, fn num ->                                                                                                                                                                                                                                                              
  num = num * 2                                                                                                                                                                                                                                                                             
  # If the number after doubling is greater than 9 then subtract 9                                                                                                                                                                                                                          
  if num > 9 do                                                                                                                                                                                                                                                                             
    num - 9                                                                                                                                                                                                                                                                                 
  else                                                                                                                                                                                                                                                                                      
    num                                                                                                                                                                                                                                                                                     
  end                                                                                                                                                                                                                                                                                       
end)                                                                                                                                                                                                                                                                                        
# Put the first one back                                                                                                                                                                                                                                                                    
|> List.insert_at(0, first)                                                                                                                                                                                                                                                                 
# Sum them all                                                                                                                                                                                                                                                                              
|> Enum.sum()                                                                                                                                                                                                                                                                               
# If the modulo of 10 is 0 then it is a valid checksum                                                                                                                                                                                                                                      
|> Kernel.rem(10)                                                                                                                                                                                                                                                                           
|> Kernel.==(0)   

Last Post!

shjeon0730

shjeon0730

found a simple solution:
basic concept is, adding a dummy value at the beginning, and choose tail later.

list=[1,2,3,4,5]
Enum.map_every(list, 2, & &1+10)
[11, 2, 13, 4, 15]
[99 | list] |> Enum.map_every(2, & &1+10) |> tl
[1, 12, 3, 14, 5]

for more offset, comprehension would work.

for i <- 1..5 do i end ++ list |> Enum.map_every(7, & &1+10) |> Enum.drop(5)
[1, 2, 13, 4, 5]

if it is just alternate pattern and need to choose offset 0 or 1, it might be useful

Where Next?

Popular in Discussions Top

AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20070 166
New
sergio
There’s a new TIOBE index report that came out that shows Elixir is still not in the top 50 used languages. It also goes on to call Elix...
New
rms.mrcs
A couple of days ago I was discussing with a friend about different approaches to write microservices. He said that if he was going to w...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14362 124
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement