cgrothaus

cgrothaus

3 different ways to build a list with conditional elements in Elixir (and what that has to do with application startup)

I wrote a blog post about 3 different ways to build a list with conditional elements in Elixir (and what that has to do with application startup).

Most Liked

Eiji

Eiji

There are other ways to achieve such results for example using recursive functions.

defmodule Example do
  def sample(list) do
    List.foldr(list, [], fn
      {false, _child_spec}, acc -> acc
      {true, child_spec}, acc -> [child_spec | acc]
      child_spec, acc -> [child_spec | acc]
    end)
  end

  def sample2([]), do: []
  def sample2([{false, _child_spec} | rest]), do: sample2(rest)
  def sample2([{true, child_spec} | rest]), do: sample2([child_spec | rest])
  def sample2([child_spec | rest]), do: [child_spec | sample2(rest)]
end

input = [:not_tuple, {false, :skip_me}, {true, :no_skip}]

Example.sample(input)
Example.sample2(input)

Helpful resources:

  1. List.foldr/3
  2. Patterns and Guards

Where Next?

Popular Backend topics Top

tomekzawada
Greetings from Membrane Framework team! Check out our case study based on our latest projects at Software Mansion. https://blog.swmansi...
New
New
First poster: bot
Rust vs Go — Bitfield Consulting. Which is better, Rust or Go? Which language should you choose for your next project, and why? How do t...
New
First poster: bot
It’s not legacy code — it’s PHP. Vimeo has been using PHP in production for over 15 years. Find out how we keep a million lines of PHP i...
New
First poster: bot
We all know how to teach recursion. We’ve done it for decades. We pick some honored, time-tested examples—Fibonacci numbers and factorial...
New
First poster: AstonJ
Ten years without Elixir. I never got into Elixir, largely because it looked like Ruby. I was a Rubyist for a good while, spent time and...
New
First poster: bot
Ruby on Rails is a web framework that contains many libraries you’d need to create and deploy a successful web application. We often take...
New
First poster: bot
Once a year, I look back at the recent developments in the PHP world, and also look forward to what’s to come. And just like in 2020 and ...
New
First poster: bot
At Grammarly, the foundation of our business, our core grammar engine, is written in Common Lisp. It currently processes more than a thou...
New
First poster: bot
Episode 299 - 10 Tips and Tricks. I don’t get around to doing these too often, but they are always a lot of fun. In this episode, we’ll ...
New

Other popular topics Top

Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1030 17300 381
New
AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
Exadra37
On modern versions of macOS, you simply can’t power on your computer, launch a text editor or eBook reader, and write or read, without a ...
New
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
Fl4m3Ph03n1x
Background Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New