1

I'm trying to take an array of Pairs(StartTime:Int, EndTime:Int) and reduce them to the accumulation of time for each session.

fun main() {
    val sessionsInSecond = listOf<Pair<Int,Int>>(Pair(10,12), Pair(10,15))

    val timeSpan: Int = sessionsInSecond.reduce{acc, it -> acc + (it.second - it.first) }
    println(timeSpan)
}

This gives me the following error:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: @InlineOnly public inline operator fun BigDecimal.plus(other: BigDecimal): BigDecimal defined in kotlin @InlineOnly public inline operator fun BigInteger.plus(other: BigInteger): BigInteger defined in kotlin public operator fun Array.plus(elements: Array): Array defined in kotlin.collections public operator fun Array.plus(elements: Collection): Array defined in kotlin.collections public operator fun Array.plus(element: Int): Array defined in kotlin.collections public operator fun BooleanArray.plus(element: Boolean): BooleanArray defined in kotlin.collections public operator fun BooleanArray.plus(elements: BooleanArray): BooleanArray defined in kotlin.collections public operator fun BooleanArray.plus(elements: Collection): BooleanArray defined in kotlin.collections public operator fun ByteArray.plus(element: Byte): ByteArray defined in kotlin.collections public operator fun ByteArray.plus(elements: ByteArray): ByteArray defined in kotlin.collections public operator fun ByteArray.plus(elements: Collection): ByteArray defined in kotlin.collections @InlineOnly public inline operator fun Char.plus(other: String): String defined in kotlin.text public operator fun CharArray.plus(element: Char): CharArray defined in kotlin.collections public operator fun CharArray.plus(elements: CharArray): CharArray defined in kotlin.collections public operator fun CharArray.plus(elements: Collection): CharArray defined in kotlin.collections public operator fun DoubleArray.plus(element: Double): DoubleArray defined in kotlin.collections public operator fun DoubleArray.plus(elements: DoubleArray): DoubleArray defined in kotlin.collections public operator fun DoubleArray.plus(elements: Collection): DoubleArray defined in kotlin.collections public operator fun FloatArray.plus(element: Float): FloatArray defined in kotlin.collections public operator fun FloatArray.plus(elements: FloatArray): FloatArray defined in kotlin.collections public operator fun FloatArray.plus(elements: Collection): FloatArray defined in kotlin.collections public operator fun IntArray.plus(element: Int): IntArray defined in kotlin.collections public operator fun IntArray.plus(elements: IntArray): IntArray defined in kotlin.collections public operator fun IntArray.plus(elements: Collection): IntArray defined in kotlin.collections public operator fun LongArray.plus(element: Long): LongArray defined in kotlin.collections public operator fun LongArray.plus(elements: LongArray): LongArray defined in kotlin.collections public operator fun LongArray.plus(elements: Collection): LongArray defined in kotlin.collections public operator fun ShortArray.plus(element: Short): ShortArray defined in kotlin.collections public operator fun ShortArray.plus(elements: ShortArray): ShortArray defined in kotlin.collections public operator fun ShortArray.plus(elements: Collection): ShortArray defined in kotlin.collections public operator fun String?.plus(other: Any?): String defined in kotlin public operator fun String?.plus(other: Any?): String defined in kotlin public operator fun Collection.plus(elements: Array): List defined in kotlin.collections public operator fun Collection.plus(elements: Iterable): List defined in kotlin.collections public operator fun Collection.plus(elements: Sequence): List defined in kotlin.collections public operator fun Collection.plus(element: Int): List defined in kotlin.collections public operator fun Iterable.plus(elements: Array): List defined in kotlin.collections public operator fun Iterable.plus(elements: Iterable): List defined in kotlin.collections public operator fun Iterable.plus(elements: Sequence): List defined in kotlin.collections public operator fun Iterable.plus(element: Int): List defined in kotlin.collections public operator fun Map.plus(pairs: Array>): Map defined in kotlin.collections public operator fun Map.plus(pair: Pair): Map defined in kotlin.collections public operator fun Map.plus(pairs: Iterable>): Map defined in kotlin.collections public operator fun Map.plus(map: Map): Map defined in kotlin.collections public operator fun Map.plus(pairs: Sequence>): Map defined in kotlin.collections public operator fun Set.plus(elements: Array): Set defined in kotlin.collections public operator fun Set.plus(elements: Iterable): Set defined in kotlin.collections public operator fun Set.plus(elements: Sequence): Set defined in kotlin.collections public operator fun Set.plus(element: Int): Set defined in kotlin.collections public operator fun Sequence.plus(elements: Array): Sequence defined in kotlin.sequences public operator fun Sequence.plus(elements: Iterable): Sequence defined in kotlin.sequences public operator fun Sequence.plus(elements: Sequence): Sequence defined in kotlin.sequences public operator fun Sequence.plus(element: Int): Sequence defined in kotlin.sequences

Any suggestions how to solve this? ps. It's an exercise in reduce so i don't want to just add the values in a forEach loop.

Joel Broström
  • 3,530
  • 1
  • 34
  • 61

3 Answers3

2

I guess your result should be an Int here? You can change this to fold to make it work with little effort:

val timeSpan: Int = sessionsInSecond.fold(0) { acc, it -> 
    acc + (it.second - it.first) 
}

You start with 0 and continue adding to it until all values have been visited. The problem in your code is that acc is of type Pair<Int,Int> rather than Int

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
1

acc in reduce is Pair<Int, Int> in your case. You could either use fold or something like that

    val timeSpan = sessionsInSecond.reduce { acc, it ->
        acc.first + (it.second - it.first)
        acc
    }.first
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

You could define an extension function on Pair<Int, Int> too. Giving it a typealias makes the code even more readable.

typealias SecondsInterval = Pair<Int, Int> 

fun SecondsInterval.diff() = second - first

val timeSpan = sessionsInSecond.sumBy(SecondsInterval::diff)

This becomes especially handy, if you want to retrieve the difference of a single SecondsInterval somewhere else.

val interval: SecondsInterval = 5 to 12
interval.diff() // 7
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121