From ae00b0bb15a709d07b05b4f11342955b42e083d2 Mon Sep 17 00:00:00 2001 From: Alexander Zolotov Date: Fri, 15 Jun 2012 13:22:47 +0400 Subject: [PATCH] Fix foldRight implementation for iterables --- js/js.libraries/src/core/dom.kt | 12 ++++++------ .../src/generated/ArraysFromJLangIterables.kt | 2 +- .../generated/BooleanArraysFromJLangIterables.kt | 2 +- .../generated/ByteArraysFromJLangIterables.kt | 2 +- .../generated/CharArraysFromJLangIterables.kt | 2 +- .../generated/DoubleArraysFromJLangIterables.kt | 2 +- .../generated/FloatArraysFromJLangIterables.kt | 2 +- .../src/generated/IntArraysFromJLangIterables.kt | 2 +- .../JUtilIteratorsFromJLangIterables.kt | 2 +- .../generated/LongArraysFromJLangIterables.kt | 2 +- .../generated/ShortArraysFromJLangIterables.kt | 2 +- .../src/generated/StandardFromJLangIterables.kt | 2 +- libraries/stdlib/src/kotlin/JLangIterables.kt | 2 +- .../stdlib/src/kotlin/nullable/Nullables.kt | 4 ++-- libraries/stdlib/test/CollectionTest.kt | 16 +++++++++++++++- 15 files changed, 35 insertions(+), 21 deletions(-) diff --git a/js/js.libraries/src/core/dom.kt b/js/js.libraries/src/core/dom.kt index 6117b5cdc76..84c3a3b1aeb 100644 --- a/js/js.libraries/src/core/dom.kt +++ b/js/js.libraries/src/core/dom.kt @@ -49,17 +49,17 @@ native public trait Document: Node { public var xmlVersion: String public fun createElement(arg1: String): Element = js.noImpl public fun createComment(arg1: String): Comment = js.noImpl - public fun getElementsByTagName(arg1: String): NodeList = js.noImpl - public fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl public fun createDocumentFragment(): DocumentFragment = js.noImpl public fun createTextNode(arg1: String): Text = js.noImpl public fun createCDATASection(arg1: String): CDATASection = js.noImpl public fun createProcessingInstruction(arg1: String, arg2: String): ProcessingInstruction = js.noImpl public fun createAttribute(arg1: String): Attr = js.noImpl public fun createEntityReference(arg1: String): EntityReference = js.noImpl + public fun getElementsByTagName(arg1: String): NodeList = js.noImpl public fun importNode(arg1: Node, arg2: Boolean): Node = js.noImpl public fun createElementNS(arg1: String, arg2: String): Element = js.noImpl public fun createAttributeNS(arg1: String, arg2: String): Attr = js.noImpl + public fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl public fun getElementById(arg1: String): Element = js.noImpl public fun adoptNode(arg1: Node): Node = js.noImpl public fun normalizeDocument(): Unit = js.noImpl @@ -136,15 +136,15 @@ native public trait Element: Node { public val tagName: String public fun getAttribute(arg1: String): String = js.noImpl public fun setAttribute(arg1: String, arg2: String): Unit = js.noImpl + public fun getElementsByTagName(arg1: String): NodeList = js.noImpl + public fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl public fun removeAttribute(arg1: String): Unit = js.noImpl public fun getAttributeNode(arg1: String): Attr = js.noImpl public fun removeAttributeNode(arg1: Attr): Attr = js.noImpl - public fun getElementsByTagName(arg1: String): NodeList = js.noImpl public fun getAttributeNS(arg1: String, arg2: String): String = js.noImpl public fun setAttributeNS(arg1: String, arg2: String, arg3: String): Unit = js.noImpl public fun removeAttributeNS(arg1: String, arg2: String): Unit = js.noImpl public fun getAttributeNodeNS(arg1: String, arg2: String): Attr = js.noImpl - public fun getElementsByTagNameNS(arg1: String, arg2: String): NodeList = js.noImpl public fun hasAttribute(arg1: String): Boolean = js.noImpl public fun hasAttributeNS(arg1: String, arg2: String): Boolean = js.noImpl public fun setIdAttribute(arg1: String, arg2: Boolean): Unit = js.noImpl @@ -204,11 +204,10 @@ native public trait Node { public var textContent: String public fun normalize(): Unit = js.noImpl public fun isSupported(arg1: String, arg2: String): Boolean = js.noImpl - public fun getUserData(arg1: String): Any = js.noImpl - public fun setUserData(arg1: String, arg2: Any, arg3: UserDataHandler): Any = js.noImpl public fun getFeature(arg1: String, arg2: String): Any = js.noImpl public fun hasAttributes(): Boolean = js.noImpl public fun removeChild(arg1: Node): Node = js.noImpl + public fun getUserData(arg1: String): Any = js.noImpl public fun replaceChild(arg1: Node, arg2: Node): Node = js.noImpl public fun insertBefore(arg1: Node, arg2: Node): Node = js.noImpl public fun appendChild(arg1: Node): Node = js.noImpl @@ -220,6 +219,7 @@ native public trait Node { public fun isDefaultNamespace(arg1: String): Boolean = js.noImpl public fun lookupNamespaceURI(arg1: String): String = js.noImpl public fun isEqualNode(arg1: Node): Boolean = js.noImpl + public fun setUserData(arg1: String, arg2: Any, arg3: UserDataHandler): Any = js.noImpl class object { public val ELEMENT_NODE: Short = 1 diff --git a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt index ac101f92b9c..b56ee9bc28b 100644 --- a/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun Array.fold(initial: T, operation: (T, T) -> T): T { * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun Array.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) +public inline fun Array.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt index 7e2cb2fb71e..aeffc797788 100644 --- a/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun BooleanArray.fold(initial: Boolean, operation: (Boolean, Boole * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun BooleanArray.foldRight(initial: Boolean, operation: (Boolean, Boolean) -> Boolean): Boolean = reverse().fold(initial, operation) +public inline fun BooleanArray.foldRight(initial: Boolean, operation: (Boolean, Boolean) -> Boolean): Boolean = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt index 0eeaceb55ca..1a86fbd6654 100644 --- a/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun ByteArray.fold(initial: Byte, operation: (Byte, Byte) -> Byte) * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun ByteArray.foldRight(initial: Byte, operation: (Byte, Byte) -> Byte): Byte = reverse().fold(initial, operation) +public inline fun ByteArray.foldRight(initial: Byte, operation: (Byte, Byte) -> Byte): Byte = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt index ea34a59d9b3..5530fbe5838 100644 --- a/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun CharArray.fold(initial: Char, operation: (Char, Char) -> Char) * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun CharArray.foldRight(initial: Char, operation: (Char, Char) -> Char): Char = reverse().fold(initial, operation) +public inline fun CharArray.foldRight(initial: Char, operation: (Char, Char) -> Char): Char = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt index 81fee5ce19d..ee7c9bd630a 100644 --- a/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun DoubleArray.fold(initial: Double, operation: (Double, Double) * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun DoubleArray.foldRight(initial: Double, operation: (Double, Double) -> Double): Double = reverse().fold(initial, operation) +public inline fun DoubleArray.foldRight(initial: Double, operation: (Double, Double) -> Double): Double = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt index f31f4adbd4c..a7ff1d03b6d 100644 --- a/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun FloatArray.fold(initial: Float, operation: (Float, Float) -> F * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun FloatArray.foldRight(initial: Float, operation: (Float, Float) -> Float): Float = reverse().fold(initial, operation) +public inline fun FloatArray.foldRight(initial: Float, operation: (Float, Float) -> Float): Float = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt index c0c52167b8f..bfb4b65d3b0 100644 --- a/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun IntArray.fold(initial: Int, operation: (Int, Int) -> Int): Int * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun IntArray.foldRight(initial: Int, operation: (Int, Int) -> Int): Int = reverse().fold(initial, operation) +public inline fun IntArray.foldRight(initial: Int, operation: (Int, Int) -> Int): Int = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt b/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt index fc53daa5470..2530c797bc2 100644 --- a/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/JUtilIteratorsFromJLangIterables.kt @@ -143,7 +143,7 @@ public inline fun java.util.Iterator.fold(initial: T, operation: (T, T) - * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun java.util.Iterator.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) +public inline fun java.util.Iterator.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt index 2db65b7edf7..c7981e3e972 100644 --- a/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun LongArray.fold(initial: Long, operation: (Long, Long) -> Long) * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun LongArray.foldRight(initial: Long, operation: (Long, Long) -> Long): Long = reverse().fold(initial, operation) +public inline fun LongArray.foldRight(initial: Long, operation: (Long, Long) -> Long): Long = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt index 640a753f120..6edc1aa6642 100644 --- a/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun ShortArray.fold(initial: Short, operation: (Short, Short) -> S * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun ShortArray.foldRight(initial: Short, operation: (Short, Short) -> Short): Short = reverse().fold(initial, operation) +public inline fun ShortArray.foldRight(initial: Short, operation: (Short, Short) -> Short): Short = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt index ee597cfc5fd..fdf63cb1cff 100644 --- a/libraries/stdlib/src/generated/StandardFromJLangIterables.kt +++ b/libraries/stdlib/src/generated/StandardFromJLangIterables.kt @@ -145,7 +145,7 @@ public inline fun Iterable.fold(initial: T, operation: (T, T) -> T): T { * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun Iterable.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) +public inline fun Iterable.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/kotlin/JLangIterables.kt b/libraries/stdlib/src/kotlin/JLangIterables.kt index fb663987878..647e6926151 100644 --- a/libraries/stdlib/src/kotlin/JLangIterables.kt +++ b/libraries/stdlib/src/kotlin/JLangIterables.kt @@ -135,7 +135,7 @@ public inline fun java.lang.Iterable.fold(initial: T, operation: (T, T) - * * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun java.lang.Iterable.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, operation) +public inline fun java.lang.Iterable.foldRight(initial: T, operation: (T, T) -> T): T = reverse().fold(initial, {x, y -> operation(y, x)}) /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by diff --git a/libraries/stdlib/src/kotlin/nullable/Nullables.kt b/libraries/stdlib/src/kotlin/nullable/Nullables.kt index 7066dcbf329..13d3a9af5a1 100644 --- a/libraries/stdlib/src/kotlin/nullable/Nullables.kt +++ b/libraries/stdlib/src/kotlin/nullable/Nullables.kt @@ -103,8 +103,8 @@ public inline fun T?.fold(initial: T, operation: (it: T, it2: T) -> T): T { * Folds all the values from right to left with the initial value to perform the operation on sequential pairs of values */ public inline fun T?.foldRight(initial: T, operation: (it: T, it2: T) -> T): T { - // maximum size is 1 so it makes no difference :) - return fold(initial, operation) + // maximum size is 1 so reverse is not needed + return fold(initial, {x, y -> operation(y, x)}) } /** diff --git a/libraries/stdlib/test/CollectionTest.kt b/libraries/stdlib/test/CollectionTest.kt index ae29da0b2f0..d6024a2560a 100644 --- a/libraries/stdlib/test/CollectionTest.kt +++ b/libraries/stdlib/test/CollectionTest.kt @@ -195,13 +195,27 @@ class CollectionTest { } } + test fun foldWithNonCommutativeOperation() { + expect(1) { + val numbers = arrayList(1, 2, 3) + numbers.fold(7) {a, b -> a - b} + } + } + test fun foldRight() { - expect("4321") { + expect("1234") { val numbers = arrayList(1, 2, 3, 4) numbers.map{it.toString()}.foldRight(""){ a, b -> a + b} } } + test fun foldRightWithNonCommutativeOperation() { + expect(-5) { + val numbers = arrayList(1, 2, 3) + numbers.foldRight(7) {a, b -> a - b} + } + } + test fun groupBy() { val words = arrayList("a", "ab", "abc", "def", "abcd") val byLength = words.groupBy{ it.length }