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

dimitarvp
Apparently he decided to live-stream how he’s going to create a semver library.
New
Scorpil
I dabbled in Phoenix for a while now, but never really got my hands dirty with it right up until now. Apart from the whole framework bein...
New
First poster: AstonJ
They expect you to make a onepage application (SPA) The polaris design system officially only supports react Integration with the s...
New
First poster: bot
Creation vs. Evolution Consider the history of Elixir: first you take Erlang, which was invented by Joe Armstrong and team to solve the ...
New
First poster: bot
Just a small test with lists in cython. Considering echosystem, multithreading and ease of use, Julia is a clear winner here.
New
CommunityNews
I don’t like reading thick O’Reilly books when I start learning new programming languages. Rather, I like starting by writing small and d...
New
CommunityNews
Functional programming is an increasing popular programming paradigm with many languages building or already supporting it. Go already su...
New
elbrujohalcon
Erlang is famous for its introspecting powers. You can get a lot of information about the processes running in your nodes without any ext...
New
First poster: bot
I wrote Python for the last 10 years, and I always tend to write code in a “functional” way - map, filter, lambda and so on, it makes me ...
New
brainlid
In episode 81 of Thinking Elixir, we talk with Digit and Quinn Wilton about the Burrito project. It wraps up Elixir to a single binary, e...
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’...
1036 18873 386
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
DevotionGeo
I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New