#KT-1675 Fixed, renaming join() -> makeString() and adding an appendString() which reduces the possible number of appendables being created
This commit is contained in:
@@ -61,19 +61,3 @@ public inline fun <T> Array<T>.take(n: Int): List<T> {
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun <T> Array<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt join
|
||||
*/
|
||||
public inline fun <T> Array<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -61,19 +61,3 @@ public inline fun <T> Iterable<T>.take(n: Int): List<T> {
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt join
|
||||
*/
|
||||
public inline fun <T> Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -142,20 +142,3 @@ private class TakeWhileIterator<T>(val iterator: java.util.Iterator<T>, val pred
|
||||
done()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from the first *n (= limit)* elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt joinConcatenatesTheFirstNElementsAboveAThreshold
|
||||
*/
|
||||
public inline fun <T> java.util.Iterator<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int? = null) : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true; var count = 0
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
if (limit == null || ++count <= limit) buffer.append(element) else break
|
||||
}
|
||||
if (limit != null && count > limit) buffer.append("...")
|
||||
return buffer.append(postfix).toString().sure()
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,16 @@ package kotlin
|
||||
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if any elements match the given *predicate*
|
||||
*
|
||||
@@ -13,13 +23,22 @@ public inline fun <T> java.lang.Iterable<T>.any(predicate: (T) -> Boolean) : Boo
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns *true* if all elements match the given *predicate*
|
||||
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt all
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt makeString
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
|
||||
for (element in this) if (!predicate(element)) return false
|
||||
return true
|
||||
public inline fun <T> java.lang.Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): Unit {
|
||||
buffer.append(prefix)
|
||||
var count = 0
|
||||
for (element in this) {
|
||||
if (++count > 1) buffer.append(separator)
|
||||
if (limit < 0 || count <= limit) {
|
||||
val text = if (element == null) "null" else element.toString()
|
||||
buffer.append(text)
|
||||
} else break
|
||||
}
|
||||
if (limit >= 0 && count > limit) buffer.append("...")
|
||||
buffer.append(postfix)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,6 +148,17 @@ public inline fun <T, K> java.lang.Iterable<T>.groupBy(result: Map<K, List<T>> =
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt appendString
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1): String {
|
||||
val buffer = StringBuilder()
|
||||
appendString(buffer, separator, prefix, postfix, limit)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
/** Returns a list containing the first elements that satisfy the given *predicate* */
|
||||
public inline fun <T, L: List<in T>> java.lang.Iterable<T>.takeWhileTo(result: L, predicate: (T) -> Boolean) : L {
|
||||
for (element in this) if (predicate(element)) result.add(element) else break
|
||||
|
||||
@@ -58,19 +58,3 @@ public inline fun <T> java.lang.Iterable<T>.take(n: Int): List<T> {
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
|
||||
|
||||
/**
|
||||
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
|
||||
*
|
||||
* @includeFunctionBody ../../test/CollectionTest.kt join
|
||||
*/
|
||||
public inline fun <T> java.lang.Iterable<T>.join(separator: String = ", ", prefix: String = "", postfix: String = "") : String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
for (element in this) {
|
||||
if (first) first = false else buffer.append(separator)
|
||||
buffer.append(element)
|
||||
}
|
||||
buffer.append(postfix)
|
||||
return buffer.toString().sure()
|
||||
}
|
||||
|
||||
@@ -89,7 +89,7 @@ get() {
|
||||
return answer
|
||||
}
|
||||
set(value) {
|
||||
this.classes = value.join(" ")
|
||||
this.classes = value.makeString(" ")
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public fun Node.writeXmlString(writer: Writer, xmlDeclaration: Boolean): Unit {
|
||||
/** Converts the collection of nodes to an XML String */
|
||||
public fun nodesToXmlString(nodes: Iterable<Node>, xmlDeclaration: Boolean = false): String {
|
||||
// TODO this should work...
|
||||
// return this.map<Node,String>{it.toXmlString()}.join("")
|
||||
// return this.map<Node,String>{it.toXmlString()}.makeString("")
|
||||
val builder = StringBuilder()
|
||||
for (n in nodes) {
|
||||
builder.append(n.toXmlString(xmlDeclaration))
|
||||
|
||||
@@ -122,7 +122,7 @@ public inline fun <T, K> T?.groupBy(result: Map<K, List<T>> = HashMap<K, List<T>
|
||||
|
||||
|
||||
/** Creates a String from the nullable or item with the given prefix and postfix if supplied */
|
||||
public inline fun <T> T?.join(separator: String, prefix: String = "", postfix: String = ""): String {
|
||||
public inline fun <T> T?.makeString(separator: String = ", ", prefix: String = "", postfix: String = ""): String {
|
||||
val buffer = StringBuilder(prefix)
|
||||
var first = true
|
||||
if (this != null) {
|
||||
|
||||
@@ -8,6 +8,16 @@ import org.junit.Test
|
||||
|
||||
class CollectionTest {
|
||||
|
||||
Test fun all() {
|
||||
val data = arrayList("foo", "bar")
|
||||
assertTrue {
|
||||
data.all{it.length == 3}
|
||||
}
|
||||
assertNot {
|
||||
data.all{s -> s.startsWith("b")}
|
||||
}
|
||||
}
|
||||
|
||||
Test fun any() {
|
||||
val data = arrayList("foo", "bar")
|
||||
assertTrue {
|
||||
@@ -18,14 +28,12 @@ class CollectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
Test fun all() {
|
||||
|
||||
Test fun appendString() {
|
||||
val data = arrayList("foo", "bar")
|
||||
assertTrue {
|
||||
data.all{it.length == 3}
|
||||
}
|
||||
assertNot {
|
||||
data.all{s -> s.startsWith("b")}
|
||||
}
|
||||
val buffer = StringBuilder()
|
||||
val text = data.appendString(buffer, "-", "{", "}")
|
||||
assertEquals("{foo-bar}", buffer.toString())
|
||||
}
|
||||
|
||||
Test fun count() {
|
||||
@@ -157,7 +165,7 @@ class CollectionTest {
|
||||
val characters = data.flatMap<String,Character>{ it.toCharList() }
|
||||
println("Got list of characters ${characters}")
|
||||
assertEquals(7, characters.size())
|
||||
val text = characters.join("")
|
||||
val text = characters.makeString("")
|
||||
assertEquals("foobarx", text)
|
||||
}
|
||||
|
||||
@@ -223,9 +231,10 @@ class CollectionTest {
|
||||
|
||||
}
|
||||
|
||||
Test fun join() {
|
||||
|
||||
Test fun makeString() {
|
||||
val data = arrayList("foo", "bar")
|
||||
val text = data.join("-", "<", ">")
|
||||
val text = data.makeString("-", "<", ">")
|
||||
assertEquals("<foo-bar>", text)
|
||||
}
|
||||
|
||||
|
||||
@@ -48,12 +48,12 @@ class IteratorsTest {
|
||||
}
|
||||
|
||||
Test fun joinConcatenatesTheFirstNElementsAboveAThreshold() {
|
||||
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.join(separator = ", ", limit = 5))
|
||||
assertEquals("13, 21, 34, 55, 89, ...", fibonacci().filter { it > 10 }.makeString(separator = ", ", limit = 5))
|
||||
}
|
||||
|
||||
Test fun toStringJoinsNoMoreThanTheFirstTenElements() {
|
||||
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().join(limit = 10))
|
||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.join(limit = 10))
|
||||
assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.join())
|
||||
assertEquals("0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...", fibonacci().makeString(limit = 10))
|
||||
assertEquals("13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...", fibonacci().filter { it > 10 }.makeString(limit = 10))
|
||||
assertEquals("144, 233, 377, 610, 987", fibonacci().filter { it > 100 }.takeWhile { it < 1000 }.makeString())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ fun customerTemplate(customer: Customer) = """
|
||||
<body>
|
||||
<h1>Hello ${customer.name}</h1>
|
||||
<ul>
|
||||
${customer.products.map<Product,String>{ productSnippet(it) }.join("\n")}
|
||||
${customer.products.map<Product,String>{ productSnippet(it) }.makeString("\n")}
|
||||
</ul>
|
||||
<p>lets do some kool stuff</p>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user