Reputation: 581
Are functional collection types available in Kotlin? Something like immutable Queue or Stack in Scala? If not, is there any good library out there which provides functional collection types to Kotlin (based on Kotlin's Any) ?
Upvotes: 0
Views: 727
Reputation: 581
So solution wasn't actually that hard. A persistent Stack can be easily implememted using a linked list which results with O(1) complexity for both push() and pop(). As for the Queue it seems there is no much choice but to accept O(n) complexity of either enqueue or dequeue. In such case I can simply use one stack as an input (enqueue) and another one as an optput (dequeue). It will be just a matter of reversing the input stack and swapping with the output when the output gets empty - probably the best idea is to do in on the dequeue(). This may be not the most elegant approach, I may need to re-think this for larger number of elements (maybe add a queue/tree of Stacks to limit reversing) but for my current needs this should be enough. Thanks to all of you for your suggestions.
Persistent Stack implementation - https://github.com/nekomatic/types/blob/graph/src/main/kotlin/com/nekomatic/types/Stack.kt
Persistent Queue implementation - https://github.com/nekomatic/types/blob/graph/src/main/kotlin/com/nekomatic/types/Queue.kt
Upvotes: 0
Reputation: 1474
Arrow Library
Λrrow is a library for Typed Functional Programming in Kotlin.
Upvotes: 1
Reputation: 421
val myarray: Array<out Any> = arrayOfNull(100)
https://kotlinlang.org/docs/reference/basic-types.html#arrays
Assigning <Any>
to <String>
is wrong, though the opposite is not.
Upvotes: 0