Kotlin input element

I am looking for an alternative to Kotlin for:
(cons 1 '(2 3)) in lisp or
1 : [2, 3] in haskell or
1 :: List(2, 3) in scala,
(which all lead to sth, like [1, 2, 3])
so I can add an item to the List<T> (or any other list you can offer).

It will also be good if you can provide O (1) head and tail Kotlin options (I found only first() )

+11
source share
7 answers

Any class that implements Deque is right for you, like LinkedList :

 val linkedList = LinkedList(listOf(2, 3)) linkedList.push(1) println(linkedList) // [1, 2, 3] 

LinkedList(listOf(2, 3)) through the LinkedList(listOf(2, 3)) constructor LinkedList(listOf(2, 3)) can be annoying in many places, so feel free to write a factory method:

 fun <T> linkedListOf(vararg elements: T): LinkedList<T> { return LinkedList<T>(elements.toList()) } // Usage: val list = linkedListOf(2, 3) list.push(1) println(list) // [1, 2, 3] 
+8
source

I think the easiest way to write is:

 var list = listOf(2,3) println(list) // [2, 3] list = listOf(1) + list println(list) // [1, 2, 3] 

There is no concrete implementation of tail , but you can call .drop (1) to get the same. You can make this head\tail more general by writing these extension properties:

 val <T> List<T>.tail: List<T> get() = drop(1) val <T> List<T>.head: T get() = first() 

Then:

 val list = listOf(1, 2, 3) val head = list.head val tail = list.tail 

Additional Information: Kotlin List Tail Function

+9
source

Simple, just wrap the item to add to the List and then use the + (or List.plus() ) operator to combine the two Lists :

 val list1 = listOf(2, 3) // [2, 3] val list2 = listOf(1) + list1 // [1, 2, 3] 

For your second question, in Kotlin 1.2 there is:

 List.first() List.last() 

Both O (1)

+2
source

To get as close to Lisp as possible, use an immutable linked list.

You can use pcollections

 val list = ConsPStack.from(listOf(2, 3)) val newList = list + 1 println(list) // [2, 3] println(newList) // [1, 2, 3] 

Head:

 list.first() // 1 list[0] // 1 

(unfortunately this thing needs one distribution)

Tail:

 list - 0 // [2, 3] list.subList(1) // [2, 3] 

It looks pretty ugly.

We hope that we get the best API when kotlinx.collections.immutable is ready. This is an attempt to create Kotlin standard immutable collections (and not just the readable ones we have). At the moment, this project is still at a very early stage (I could not find a structure supporting an effective preend / head / tail)

0
source

If for some reason you often do this in your code, consider adding an extension operator method, such as:

 operator fun <T> T.plus(tail: List<T>): List<T> { val list = ArrayList<T>(1 + tail.size) list.add(this) list.addAll(tail) return list } 

Then your code can work like Scala: 1 + listOf(2, 3)

Another way to achieve the same behavior, in short, but sacrificing memory:

 operator fun <T> T.plus(tail: List<T>): List<T> { return mutableListOf(this).apply { addAll(tail) } } 
0
source

This can be easily done with the advanced features as shown below.

Predetermining element

 fun <T> MutableList<T>.prepend(element: T) { add(0, element) } 

Preliminary list

 fun <T> MutableList<T>.prependAll(elements: List<T>) { addAll(0, elements) } 
0
source

I'm not quite sure what you want to do, so please try one of the following.

Mutating List:

 val list = mutableListOf(3, 2) list.add(1) 

Using an immutable list:

 var list = listOf(3, 2) list = list + 1 
-3
source

Source: https://habr.com/ru/post/1265682/


All Articles