Is it possible to define collection type when using from_list?
Closed this issue · 2 comments
Hi there, apologies if I missed this in the documentation, but is it possible to define the collection type when creating a path via from_list?
e.g. I want to create something similar to path :alpha / (0 :: :list)
, but dynamically.
I attempted to do something like from_list([:alpha, 0 :: :list])
, however, I got an error saying that ::
was not a defined function.
The use case for this is that I want to use the above path to force_set
and create if it doesn't exist, but I want it to create a list rather than a map. From testing it seems that defining the collection type correctly results in a list being created for the 0
path.
Any assistance would be much appreciated!
Thanks!
You can't do this with from_list
, but you can achieve this behavior using other functions. Consider something like this
def my_from_list([head | tail]) do
Enum.reduce(tail, to_path(head), fn item, acc ->
acc ~> to_path(item)
end)
end
def my_from_list([]), do: matching(_)
defp to_path(int) when is_integer(int) do
path(int :: :list)
end
def to_path(other), do: path(other)
Oh I see, that is very nice! I am relatively new to Elixir so I am struggling a bit with these new patterns.
Appreciate the assistance and prompt response! Have a great day :)