overcomeoj
Filling the tail of a list upto a length with default values?
Is there a more idiomatic way to fill the tail of a list with default values?
That is, given a list, return a list that is at least n elements long, with a default value inserted if needed:
x = [1, 2, 3]
fill(x, 5, :default) # => [1, 2, 3, :default, :default]
This is what I came up with, you could also do it with zip, if the first arg was the correct length filled with default values, but that seems needlessly “allocate-y”. Also you could do some recursive function.
# This seems the most simple to understand, but could be expensive if the lists are big?
fill_concat = fn list, len, default ->
# Stream.repeatedly (or cycle) probably best if count is large enough
# to make [:default, :default ...] problematic if just writing this
# directly in another function.
list
|> Enum.concat(Stream.repeatedly(fn -> default end) |> Enum.take(len))
|> Enum.take(len)
end
fill_stream =
fn list, len, default ->
# *probably* faster, at least when the size is under the cost of creating a stream etc.
Stream.unfold({list, len}, fn
{_, 0} -> nil
{[x | xs], count} -> {x, {xs, count - 1}}
{[], count} -> {default, {[], count - 1}}
end)
|> Enum.to_list()
end
x = [1, 2]
fill_concat.(x, 5, :concat)
|> IO.inspect()
fill_stream.(x, 5, :stream)
|> IO.inspect()
Maybe this is the way to do it, but it feels a bit over done?
Most Liked
LostKobrakai
list
|> Stream.concat(Stream.repeatedly(fn -> default end))
|> Enum.take(len)
This should be pretty sane as it’ll iterate list only once, touching Stream.repeatedly(fn -> default end) only if needed.
1
dimitarvp
I changed your post to “Questions”, now you can select a solution. You originally marked your post as “Chat” and there are no marked solutions there.
1
Popular in Questions
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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
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
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
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
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Other popular topics
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
can someone please explain to me how Enum.reduce works with maps
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this
"1000"
What is the ...
New
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
Hey :wave:t3: Elixir community,
I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New







