vishal-h
Convert this python code into elixir
How would you write the accepted answer of https://stackoverflow.com/questions/19293481/how-to-elegantly-interleave-two-lists-of-uneven-length-in-python in Elixir?
More specifically, this code
from itertools import groupby
len_ab = len(a) + len(b)
groups = groupby(((a[len(a)*i//len_ab], b[len(b)*i//len_ab]) for i in range(len_ab)),
key=lambda x:x[0])
[j[i] for k,g in groups for i,j in enumerate(g)]
Most Liked
isaac-rstor
here’s a slightly more idiomatic version, although I think there might be some corner cases where it’s not as even as it could be:
defmodule M do
@doc """
interleaves a short list b, into a long list a, such that the
presence of the short list is evenly spaced in the long list.
iex> M.interleave([1,2,3,4,5,6,7],[:a, :b])
[1, 2, :a, 3, 4, 5, :b, 6, 7]
"""
def interleave(a, b) do
a_ct = Enum.count(a)
b_ct = Enum.count(b)
# calculates how many items are in the cycle of drawing from a vs b.
recurrence = div(a_ct, b_ct) + 1
# calculate how much we have to offset the recurrence by to center it.
offset = div(a_ct + b_ct - ((b_ct - 1) * recurrence), 2)
# bootstrap the initial condition
interleave(a, b, offset, recurrence, [])
end
# terminating condition: we've exhausted all the items; since we put values on the front
# in the tail calls, reverse the list.
def interleave([], [], _, _, rev_list), do: Enum.reverse(rev_list)
def interleave(a, [b_hd | b_tl], n, recurrence, list_so_far)
when rem(n, recurrence) == 0 do
# draw from the shorter list every <recurrence>. Put on front
# of the list we're building, then tail call in.
interleave(a, b_tl, n + 1, recurrence, [b_hd | list_so_far])
end
def interleave([a_hd | a_tl], b, n, recurrence, list_so_far) do
# draw from the longer list. Put on front of the list we're building,
# then tail call in.
interleave(a_tl, b, n + 1, recurrence, [a_hd | list_so_far])
end
end
if the enum.reverse thing is a bit confusing, you can also do this:
def interleave([], [], _, _, list), do: list
def interleave(a, [b_hd | b_tl], n, interval, list_so_far)
when rem(n, interval) == 0 do
interleave(a, b_tl, n + 1, interval, list_so_far ++ [b_hd])
end
def interleave([a_hd | a_tl], b, n, interval, list_so_far) do
interleave(a_tl, b, n + 1, interval, list_so_far ++ [a_hd])
end
1
Popular in Questions
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
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
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Hello,
I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these
buyer = %{
id: ...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
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
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
Other popular topics
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
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
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
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New







