Hi folks!
I'd like to propose a List.rotate/4 function (and maybe some convenience wrappers for it as well).
Rotate is one of those algorithms that, once I learned about it, I started seeing it everywhere. It's somewhat complicated to grasp (it takes 4 parameters, after all), but in situations where you need it, it's still *much* simpler than the equivalent imperative version.
The classic use case is this: Suppose you have a list of to-do items, which the user has ordered by priority:
A "rotate" or "slide" occurs when the user selects some number of elements and drags them to a new place in the list. Let's say they selected items 3 & 4 from the preceding and dragged them above item 2. When they release the mouse, the new order should be:
Doing this without the named algorithm requires 3 splits (one at the insertion point, one at the start of the selected range, and one at the end of the selected range). It's easy to get the index math wrong, and it's even harder for readers of your code to grasp what's going on. Adding this as a "vocabulary" algorithm would help a lot, I feel.
A number of other languages have a rotate algorithm, though it's still somewhat uncommon. I found Dave Abrahams' comments valuable when this was discussed for inclusion in Swift.
I've put together a first draft of an implementation, plus some basic tests.
If this is something folks decide we want, it might also be worth considering a few variants (also implemented in the file linked above):
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/01c63660-6a11-4e69-bdcb-6659579ef683n%40googlegroups.com.
Playing devil's advocate here for a second:
We are talking about a situation where you have a collection of
items, and you want to add elements to the middle/remove elements
from the middle/reorder elements into the middle.
I do not think that lists are the right tool for this job here,
as these operations will slow down significantly when the list
grows in size.
As such, maybe adding such an operation to the `List` module might
not be such a good idea, as we will be giving new developers a
tool which they may hurt themselves with,
rather than choosing a data structure (like another array
collection, such as map-based arrays or `:array`) that fits the
requirements better.
~Marten/Qqwy
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/3467969a-8289-42fa-9b4e-741a26da3e1en%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/990c93ec-0f9f-0d8a-4d51-731821ebf205%40resilia.nl.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/7657048f-da6d-475e-8c43-cb22f22e2e0cn%40googlegroups.com.
It could definitely be implemented as a Enum or a Stream function, but the level of complexity is definitely much higher, and reduce would force the implementation to be linear anyway. So I am honestly not sure if it is worth it.
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/d54e1e48-a32c-4d9c-8307-d5cb7926050cn%40googlegroups.com.
You received this message because you are subscribed to a topic in the Google Groups "elixir-lang-core" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-lang-core/LYmkUopaWN4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/8903e169-dadc-48bf-98ea-f860f1bc0891n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/dff0982d-3d24-4279-a013-e7f08fb2e5cdn%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/cce43976-8c96-4098-b926-06133be5ec24n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/ab1d8450-7a6d-4c9b-a6f0-e839fa9bcc00n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/995ee2f1-b011-44c9-811e-a6da70f92528n%40googlegroups.com.