From e76d8e0774c2e3fe4a194e81403790d2850f4193 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 12 Apr 2023 11:40:27 +0300 Subject: [PATCH] fix zipWithNextCircular on single element --- .../kotlin/space/kscience/kmath/misc/collections.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/collections.kt b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/collections.kt index afa76d2a9..f630055fa 100644 --- a/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/collections.kt +++ b/kmath-core/src/commonMain/kotlin/space/kscience/kmath/misc/collections.kt @@ -9,7 +9,7 @@ package space.kscience.kmath.misc * The same as [zipWithNext], but includes link between last and first element */ public inline fun List.zipWithNextCircular(transform: (a: T, b: T) -> R): List { - if (isEmpty()) return emptyList() + if (size < 2) return emptyList() return indices.map { i -> if (i == size - 1) { transform(last(), first()) @@ -19,4 +19,4 @@ public inline fun List.zipWithNextCircular(transform: (a: T, b: T) -> } } -public fun List.zipWithNextCircular(): List> = zipWithNextCircular { l, r -> l to r } \ No newline at end of file +public fun List.zipWithNextCircular(): List> = zipWithNextCircular { l, r -> l to r } \ No newline at end of file