Updating a list is fast as long as we are prepending elements:
iex> [0 | list] [0, 1, 2, 3]
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/8b4cb74d-06ff-4840-9698-9091c3c672c5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
It's not made clear in the elixir docs but building the list backwards and reversing it is the usual way. It's more clearly laid out in erlang's docs: http://erlang.org/doc/efficiency_guide/listHandling.html
Thanks for the reply -- that definitely gives me a better understanding behind the way it ought to be implemented. However, the erlang doc even says:"Looking at how lists:append/1 or ++ would be implemented in plain Erlang..."Why isn't there a Lists.append/1 or an operator in Elixir? Is everyone writing their own implementation every time they need to append to a list? I mean "++" isn't painful, of course, but having to convert elements to a list prior to concatenation semantically expresses a slightly different relationship, which can lend itself to less clarity in a heavily-nested, recursive processing situation. I'm just curious, it seemed an odd omission to leave out an append operation (or any reference to how to do so) when the language acknowledges list element deletion, replacing, prepending, concatenation, and subtraction.
I'm just learning Elixir but I'm getting the definite impression that
prepending is much more "Elixirish" than appending....
That said I believe ++ (http://elixir-lang.org/docs/stable/elixir/Kernel.html#++/2) is the recommended way to append to a list if it's needed.
There is no way of appending an element to a list in Erlang either. In Elixir and Erlang we use `list ++ [elem]` to append elements. Is that really so much worse than `list ++ elem`? It is uncommon to append an element to a list since, as others have said, it is inefficient. In the majority of cases you would design your data structures and APIs so that you only prepend to lists and then at some point reverse the list.
Typically, you should be using Enum.map to preserve the conceptual ordering (which behind the scenes does a prepend, then reverse at the end) or some other higher order function, or a for comprehension. There are much better options if you abstract your operation a bit.
This is a pretty fundamental approach for immutable data structures. I'm against an api and an operator for appending to lists. Doing things in a bad way should have a syntactical disadvantage IMHO. oldlist ++ [newelement] is good enough when needed.
Enough people have commented already about how it typically isn't done.
If however you do need a "list" you can append and prepend to you can reach for the erlang :queue module.
Thanks,
Red
OK, Brad, based upon this email, perhaps the better advice is to make sure that you're using the correct data structure for your problem. In functional programming a "list" is a specific data structure, much more constrained than the vernacular use of the word "list". It inherently has this O(n) behavior for appending.
--
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-talk/BR7wPpG9tzw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/ab9767e6-1998-439f-af6b-1f72b237ad06%40googlegroups.com.
children =if add_conditional_child?(opts) dochildren ++ [new_child] # Or List.appendelsechildrenend
default_children() ++ conditional_children(opts) ++ other_conditional_children(opts)
children = [
{:ok, child1},
{:ok, child2},
conditional_child(opts), # {:ok, child} | :errorother_conditional_child(opts), # {:ok, child} | :error
]
for {:ok, child} <- children, do: child
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAP%3DvNq-o1z9R5P35oGNb-F5XX6qxs1gmc_zDkh5p%2BuLdkKY13w%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAGnRm4Ksb-xEG7yCoDsjYctz0gHSMDWnA49Ag%2BAm2R9-8QkOvQ%40mail.gmail.com.
The reason there isn't such function in List is because we already have ++ in Kernel. Some could argue that List.append would be useful for developers that are not familiar with ++ but I would rather make sure we teach developers about ++ because it is more useful than List.append (as ++ is allowed in patterns).
> The thing that got me to thinking about it was populating the "children" list of workers in the startup of a Phoenix web app.I would just like to use this example to mention that solving this with an append operation is a very imperative way of thinking about the problem.
That reminds me more of https://www.youtube.com/watch?v=6LR6eS2NWf4
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/CAHxKQijhhSWdU5r3rGOgwasThzRJO8uq0XxfdvwP6G_7x-6wMg%40mail.gmail.com.
I would also argue that a singly-linked list of immutable nodes does not have a logical append operation. Perhaps we should rename the List module to ImmutableSinglyLinkedList (I jest). It does however have a logical pure concatenate operation. So while a generic List data structure should provide an append operation, we don't actually have a generic List. We only have an ImmutableSinglyLinkedList, and therefore should limit our abstraction.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/c34c393e-04cd-4a53-8058-101ad39c5b8b%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/B4AFFE81-3689-4A79-96F4-3C2596E37D47%40cfcl.com.
On 16 Jun 2016, at 18:43, Brad O'Hearne <brad.o...@gmail.com> wrote:Users will always have the need to prepend, append, insert, replace, delete, clear, copy, etc. elements of a list. Those are logical needs of a list, a fundamental data structure, same as queues, stacks, etc. have their own needs. Take a stack for instance -- that is a fundamental data structure with canonical operations.
A lot of this confusion is due to people using the term 'list' when
what they really have is something a lot more akin to a 'stack'.
> I find it completely absurd to have an ordered data structure API which allows mutability but doesn’t formally allow and express adding elements in order.
Lists are not mutable. Adding things to the end of a list is very possible, but doesn't need a special operator or function because it's not something you should be doing all that much. The concatenation operator should do fine. Use it.
Bradley,
You said:
"I find it completely absurd to have an ordered data structure API which allows mutability but doesn’t formally allow and express adding elements in order."
You explicitly said mutable. A clarification that there are no mutable data-structures on the beam seems an appropriate clarification.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/2217968F-EF17-4688-BCB9-9179E986E55B%40bighillsoftware.com.