Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Clojure
- C++
- C++
- C#
- C#
- D
- Dart
- Elixir
- Fortran
- Go
- Go
- Go
- Go
- Haskell
- Haskell
- JS
- JS
- Java
- Lisp
- Lua
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Ruby
- Rust
- Scala
- VB
- VB
items = items.Prepend(x).ToList();
items implements IEnumerable<T>.
Omitting ToList() results in a wrapper IEnumerable<T> with the element prepended.
Omitting ToList() results in a wrapper IEnumerable<T> with the element prepended.
items.Insert(0, x);
items implements IList<T>.
Complexity depends on list implementation.
Complexity depends on list implementation.
items = [x, ...items];
items2 = [x | items]
items = [x, items]
This assumes that items is an allocatable array.
items = append([]T{x}, items...)
items has type []T.
This implementation always allocates a full new slice.
This implementation always allocates a full new slice.
func prepend[S ~[]T, T any](items *S, x ...T) {
*items = append(*items, x...)
copy((*items)[len(x):], *items)
copy(*items, x)
}
This generic func does not always allocate (depending on the capacity of items).
The variadic argument x accepts one or more values to insert at the beginning.
The variadic argument x accepts one or more values to insert at the beginning.
func prepend[S ~[]T, T any](items *S, x ...T) {
*items = append(x, *items...)
}
This generic func always allocates a full new slice.
The variadic argument x accepts one or more values to be inserted at the beginning.
The variadic argument x accepts one or more values to be inserted at the beginning.
items = append(items, x)
copy(items[1:], items)
items[0] = x
This implementation is verbose, but it will often not allocate, when items has enough capacity.
f(x:xs)= x:xs
items2 = x : items
This creates a new list items2
items = [x, ...items];
items.unshift(x);
items.add(0, x);
items implements List<E>. Complexity depends on list implementation.
(push x items)
table.insert(items, 1, x)
items.insert(0,x);
unshift @items, $x
items.insert(0, x)
items = [x] + items
items.prepend(x)
prepend is an alias for unshift
items.unshift(x)
val newList = x :: items
Scala uses an implementation of a linked list for its List type, so prepending x is guaranteed to be O(1) time.
The :: operator is pronounced "Cons"
The :: operator is pronounced "Cons"
items = items.Prepend(x).ToList()
items implements IEnumerable<T>.
Omitting ToList() results in a wrapper IEnumerable<T> with the element prepended.
Omitting ToList() results in a wrapper IEnumerable<T> with the element prepended.
items.Insert(0, x)
items implements IList<T>.
Complexity depends on list implementation.
Complexity depends on list implementation.