From 7e93494e2fdddc7da99c4a8491ec84c19a3dbb11 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Sun, 18 Dec 2011 08:15:39 +0000 Subject: [PATCH] renamed the sort() methods on Iterable which create a new List and sort that as "toSortedList()" instead which is more accurate. --- stdlib/ktSrc/JavaUtil.kt | 37 ++++++++++++++++++------------ testlib/test/CollectionApiCheck.kt | 1 + 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/stdlib/ktSrc/JavaUtil.kt b/stdlib/ktSrc/JavaUtil.kt index 3a6761e9a26..f0faab3ab2f 100644 --- a/stdlib/ktSrc/JavaUtil.kt +++ b/stdlib/ktSrc/JavaUtil.kt @@ -141,13 +141,13 @@ inline fun java.util.Collection.map(transform : fun(T) : R) : Collecti return result } -inline fun > java.lang.Iterable.sort() : List { +inline fun > java.lang.Iterable.toSortedList() : List { val answer = this.toList() answer.sort() return answer } -inline fun > java.lang.Iterable.sort(comparator: java.util.Comparator) : List { +inline fun > java.lang.Iterable.toSortedList(comparator: java.util.Comparator) : List { val answer = this.toList() answer.sort(comparator) return answer @@ -155,20 +155,9 @@ inline fun > java.lang.Iterable.sort(comparator /** TODO figure out necessary variance/generics ninja stuff... :) -inline fun java.lang.Iterable.sort(transform: fun(T) : java.lang.Comparable<*>) : List { +inline fun java.lang.Iterable.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { val answer = this.toList() - val comparator = java.util.Comparator() { - fun compare(o1: T, o2: T): Int { - val v1 = transform(o1) - val v2 = transform(o2) - if (v1 == v2) { - return 0 - } else { - return v1.compareTo(v2) - } - } - } - answer.sort(comparator) + answer.sort(transform) return answer } */ @@ -207,6 +196,24 @@ inline fun > List.sort(comparator: java.util.Co Collections.sort(this, comparator) } +/** + TODO figure out necessary variance/generics ninja stuff... :) +inline fun List.sort(transform: fun(T) : java.lang.Comparable<*>) : List { + val comparator = java.util.Comparator() { + fun compare(o1: T, o2: T): Int { + val v1 = transform(o1) + val v2 = transform(o2) + if (v1 == v2) { + return 0 + } else { + return v1.compareTo(v2) + } + } + } + answer.sort(comparator) +} +*/ + val List.head : T? get() = this.get(0) diff --git a/testlib/test/CollectionApiCheck.kt b/testlib/test/CollectionApiCheck.kt index 0b97d4cb974..4396cc2617d 100644 --- a/testlib/test/CollectionApiCheck.kt +++ b/testlib/test/CollectionApiCheck.kt @@ -1,5 +1,6 @@ namespace test.apicheck +import std.util.* import java.util.* trait Traversable {