feat: reverse argument order of List.insert to index-first convention
The previous signature `insert(element, index)` was counter-intuitive and inconsistent with every major language's list API. This commit swaps the parameters to `insert(index, element)`, matching the convention used by Ruby, JavaScript, C++, Lua, C#, Java, and Python. All documentation, C implementation, and test files are updated to reflect the new order.
This commit is contained in:
@@ -17,7 +17,7 @@ Removes all items from the list.
|
||||
|
||||
The number of items in the list.
|
||||
|
||||
### **insert**(item, index)
|
||||
### **insert**(index, item)
|
||||
|
||||
**TODO**
|
||||
|
||||
|
||||
@@ -71,10 +71,10 @@ use `add` to append a single item to the end:
|
||||
You can insert a new element at a specific position using `insert`:
|
||||
|
||||
:::dart
|
||||
hirsute.insert("soul patch", 2)
|
||||
hirsute.insert(2, "soul patch")
|
||||
|
||||
The first argument is the value to insert, and the second is the index to
|
||||
insert it at. All elements following the inserted one will be pushed down to
|
||||
The first argument is the index to insert at, and the second is the value to
|
||||
insert. All elements following the inserted one will be pushed down to
|
||||
make room for it.
|
||||
|
||||
It's valid to "insert" after the last element in the list, but only *right*
|
||||
@@ -83,9 +83,9 @@ back. Doing so counts back from the size of the list *after* it's grown by one:
|
||||
|
||||
:::dart
|
||||
var letters = ["a", "b", "c"]
|
||||
letters.insert("d", 3) // OK: inserts at end.
|
||||
letters.insert(3, "d") // OK: inserts at end.
|
||||
IO.print(letters) // ["a", "b", "c", "d"]
|
||||
letters.insert("e", -2) // Counts back from size after insert.
|
||||
letters.insert(-2, "e") // Counts back from size after insert.
|
||||
IO.print(letters) // ["a", "b", "c", "e", "d"]
|
||||
|
||||
## Removing elements
|
||||
|
||||
Reference in New Issue
Block a user