*On Thursday, June 16, 2016 at 9:18:27 AM UTC-7, Eric Meadows-Jönsson wrote:* > > *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.* >
Nope...it isn't bad (in this case). In fact, that's how I implemented (and have done so all over the place in various recursive function calls prior). But it also isn't technically what I'm trying to accomplish, and I've had the same thought every time I've done it, so I decided to go did a little bit to find out the reason why, and then ask the question this time. 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 needed to conditionally add elements to that list in sequence. I accomplished this in about 30 seconds, so this isn't an issue with being unable to accomplish the task (though I rather would have done it with a single operator or expression that illustrated exactly what I was intending). I both create and consume a lot of APIs, and there can be a subtle philosophy that creeps into API design that what is efficient to implement is synonymous with what logical needs are --- they aren't synonymous. 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. It wouldn't be a good idea to eliminate the "push" operation because (hypothetically) language X made concatenating the entire contents of the existing stack to the element to be pushed a more efficient operation. Sure, the underlying implementation can take that approach if it is most efficient, but that should be a black-box implementation detail, not a requirement placed on the user of a stack -- the stack data structure should expose "push". The general idea is maintaining consistent logical metaphors and data structures regardless of implementation approach. Just a suggestion. At the very least I think it improves clarity of the language and resulting code. Cheers, Brad -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/e99166d2-ebc1-4a4b-93f0-c0075a15f576%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
