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

Popular Backend topics Top

PragmaticBookshelf
Classroom-tested by tens of thousands of students, this new edition of the bestselling intro to programming book is for anyone who wants ...
New
PragmaticBookshelf
Ruby, Io, Prolog, Scala, Erlang, Clojure, Haskell. With Seven Languages in Seven Weeks, by Bruce A. Tate, you’ll go beyond the syntax—and...
New
AstonJ
Currently a hot topic in the BEAM world, let’s start a thread for it (as suggested by @crowdhailer here) :smiley: What are your current...
New
bot
Kotlin v1.4.0 has been released. Link: Release Kotlin 1.4.0 · JetBrains/kotlin · GitHub
New
ErlangSolutions
Hi this week it’s Code Mesh V when London’s leading alternative tech conference goes virtual for the first time. Talks are set for both U...
New
First poster: bot
Ruby on Rails v6.1.0 has been released. Link: Release 6.1.0 · rails/rails · GitHub
New
joshi
Hey everybody! I’m working on the project that includes import of Oracle data to PostgreSQL. That data comes as Oracle export (expdp) fi...
New
GermaVinsmoke
Reading Programming Elixir 1.6 book, I’ve completed part 1 of the book. Now I’m thinking of reading Elixir in Action. What do you all sug...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
mafinar
Hello folks! We had a pretty fun thread here around the same time last year - talking about Advent of Code problems. That also happened t...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
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
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
AstonJ
Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face: https://on...
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
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
AstonJ
Chris Seaton, the creator of TruffleRuby has died. It appears from suicide :cry: He left this note on Twitter on the weekend: And one...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New