does kotlin provide any list implementation that can do efficient (constant-time) cons/cdr, for use when designing tail recursive functions that operate on lists?
r
Ryan
05/24/2020, 11:12 PM
you can write an extension for linkedlist.
m
myaa
05/24/2020, 11:25 PM
if you mean java's LinkedList i believe that's doubly linked, so efficient (immutable) cons/cdr isn't possible
j
Jakub Gwóźdź
05/25/2020, 9:21 AM
there is kotlinx.collections.immutable project that may be of help to you, but I'm not sure if that's exactly what you're looking for.
k
Kroppeb
05/25/2020, 10:08 AM
Do you mean a stack?
You want to push and pop data no?
Technically you can just use a list and add an element or remove one at the end, or use deque if you want a nicer interface and want to push and pop on both sides
m
myaa
05/25/2020, 5:22 PM
in the end i just went with a simple custom definition of a linked list:
class Node<T>(val head: T, val tail: Node<T>?)
. i guess i was just a bit too used to languages like scala/haskell where this is the default list type and constant-time cons is a very fundamental operator
it also looks like the
Sequence
type (immutable, stateless) might be relevant for functional idioms