One common problem we face in constructing lists is that there is (AFAIK) no support for conditionally inserting members into list declarations.
You can’t write:
allow_even? = false
[
1,
if allow_even? do 2 end,
3
]
because the result would be [1, nil, 3], though syntactically a lot of code would be easier and more readable for us if you could do that.
What if returning a very specific value, like :ignore_list_member would lead to a list being constructed like this:
[1, :ignore_list_member, 3] ==> [1, 3]
Functions could return specifically this value if there’s nothing to insert.
def foo(x), do: if rem(x, 2) != 0 do x else :ignore_list_member end
[
foo(1),
foo(2),
foo(3)
]
===> [1, 3]
Right now we typically do something like piping a whole list like this:
allow_even? = false
[
1,
if allow_even? do 2 end,
3
]
|> Enum.reject(&is_nil/1)
which is fine, but I wonder if this could be solved more conveniently and generic without the need to walk the list a second time after constructing it - just once when it’s “constructed” with the declarative syntax.
If it’s not possible with small effort, I still thank you for reading this. ![]()






















