Getting a Pair from Collections


#1

Hi,
I was just wondering would it be feasible to have Collectons extension that can convert given array to pair by applying some custom transformation.
An example in can give is reading String (“1,2” )and then getting a Pair(1,2) out of it?
I know i can write a custom function but was just wondering if many of us do it frequently, can we have an extension for this?
It could look like this
val list = readLine()!!.split(" ").collectAsPair(someTransformation)


#2

This feature is present in Kotlin 1.2-M2. Your interested features are chunked and pairwise. You could see these APIs here.
If you are using Kotlin 1.1.x versions, you could use a library like kommon. Look at this test method of the library.


#3

That’s great. Thanks


#4

Incidentally, the pairwise() function has now be renamed to zipWithNext().

This makes it clear that each element is zipped with the next element in the collection which wasn’t particularly clear from the previous name.