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)   
gregvaughn

gregvaughn

I had fun turning the Luhn checksum into a simple Elixir pipeline a couple of years ago. The algorithm as stated is of a very imperative/mutable mindset, but you can do it with immutable pipelines.

https://github.com/gvaughn/elixir_kata/blob/master/luhn/luhn.exs

davydog187

davydog187

If you want to get “fancy”, then you could simply use Enum.with_index and Enum.map. Bonus points if you use the Stream module

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|> Enum.with_index()
|> Enum.map(fn {num, index} ->
  cond do
    index < 4 -> num
    rem(num, 2) == 0 -> num * 2
    true -> num * 3
  end
end)

[1, 2, 3, 4, 15, 12, 21, 16, 27, 20]

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
New
pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
praveenperera
How We Replaced React with Phoenix By: Thought Bot
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
ejpcmac
I have discovered Nix last month and I am currently on my way to migrating to it—both on macOS at home and the full NixOS distrubution at...
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
griffinbyatt
Sobelow Sobelow is a security-focused static analysis tool for the Phoenix framework. For security researchers, it is a useful tool for g...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New

Other popular topics Top

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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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