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

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: wolf4earth
Understanding Partial Moves in Rust. Partial moves are an interesting but often misunderstood feature of Rust. However, with the right ...
New
AstonJ
If you’re interested in Rust this is worth a read :smiley: Technology from the past come to save the future from itself Hi I have be...
New
First poster: bot
What’s Next for Teal, the typed dialect of Lua - FOSDEM 2021. This is my talk about the latest updates on the Teal programming language,...
New
First poster: bot
Go is not an easy programming language. It is simple in many ways: the syntax is simple, most of the semantics are simple. But a language...
/go
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
axelson
I describe how we use Hot Reloading with Webpack to develop faster and show how to integrate Webpack 5, webpack-dev-server, and Phoenix f...
New
elbrujohalcon
Another week, another oldies-but-goldies post… This one about Test Driven Development.
New
StuntProgrammer
In building lofi.limo, media storage and distribution naturally came up. I have songs, announcements, and background image loops which I ...
New

Other popular topics Top

AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
AstonJ
If you want a quick and easy way to block any website on your Mac using Little Snitch simply… File > New Rule: And select Deny, O...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
PragmaticBookshelf
Author Spotlight: Karl Stolley @karlstolley Logic! Rhetoric! Prag! Wow, what a combination. In this spotlight, we sit down with Karl ...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
PragmaticBookshelf
Develop, deploy, and debug BEAM applications using BEAMOps: a new paradigm that focuses on scalability, fault tolerance, and owning each ...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New