Rename Stream<T> to Sequence<T> and provide migration path via deprecated types and functions.

This commit is contained in:
Ilya Ryzhenkov
2015-03-10 18:10:57 +03:00
parent 9684d217b3
commit e448f40756
47 changed files with 1906 additions and 556 deletions
@@ -172,10 +172,10 @@ class ArraysTest {
expect(1) { array("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
expect(2) { array("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
expect(-1) { streamOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
expect(0) { streamOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
expect(1) { streamOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
expect(2) { streamOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
expect(-1) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
expect(0) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
expect(1) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
expect(2) { sequenceOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
}
test fun lastIndexOf() {
@@ -191,11 +191,11 @@ class ArraysTest {
expect(2) { array("cat", "dog", "bird").indexOfLast { it.endsWith('d') } }
expect(3) { array("cat", "dog", "bird", "red").indexOfLast { it.endsWith('d') } }
expect(-1) { streamOf("cat", "dog", "bird").indexOfLast { it.contains("p") } }
expect(0) { streamOf("cat", "dog", "bird").indexOfLast { it.startsWith('c') } }
expect(2) { streamOf("cat", "dog", "cap", "bird").indexOfLast { it.startsWith('c') } }
expect(2) { streamOf("cat", "dog", "bird").indexOfLast { it.endsWith('d') } }
expect(3) { streamOf("cat", "dog", "bird", "red").indexOfLast { it.endsWith('d') } }
expect(-1) { sequenceOf("cat", "dog", "bird").indexOfLast { it.contains("p") } }
expect(0) { sequenceOf("cat", "dog", "bird").indexOfLast { it.startsWith('c') } }
expect(2) { sequenceOf("cat", "dog", "cap", "bird").indexOfLast { it.startsWith('c') } }
expect(2) { sequenceOf("cat", "dog", "bird").indexOfLast { it.endsWith('d') } }
expect(3) { sequenceOf("cat", "dog", "bird", "red").indexOfLast { it.endsWith('d') } }
}
test fun plus() {
@@ -376,8 +376,8 @@ class CollectionTest {
expect(2000000000000, { listOf(3000000000000, 2000000000000).min() })
expect('a', { listOf('a', 'b').min() })
expect("a", { listOf("a", "b").min() })
expect(null, { listOf<Int>().stream().min() })
expect(2, { listOf(2, 3).stream().min() })
expect(null, { listOf<Int>().sequence().min<Int>() })
expect(2, { listOf(2, 3).sequence().min<Int>() })
}
test fun max() {
@@ -387,8 +387,8 @@ class CollectionTest {
expect(3000000000000, { listOf(3000000000000, 2000000000000).max() })
expect('b', { listOf('a', 'b').max() })
expect("b", { listOf("a", "b").max() })
expect(null, { listOf<Int>().stream().max() })
expect(3, { listOf(2, 3).stream().max() })
expect(null, { listOf<Int>().sequence().max<Int>() })
expect(3, { listOf(2, 3).sequence().max<Int>() })
}
test fun minBy() {
@@ -397,8 +397,8 @@ class CollectionTest {
expect(3, { listOf(2, 3).minBy { -it } })
expect('a', { listOf('a', 'b').minBy { "x$it" } })
expect("b", { listOf("b", "abc").minBy { it.length() } })
expect(null, { listOf<Int>().stream().minBy { it } })
expect(3, { listOf(2, 3).stream().minBy { -it } })
expect(null, { listOf<Int>().sequence().minBy<Int, Int> { it } })
expect(3, { listOf(2, 3).sequence().minBy<Int, Int> { -it } })
}
test fun maxBy() {
@@ -407,8 +407,8 @@ class CollectionTest {
expect(2, { listOf(2, 3).maxBy { -it } })
expect('b', { listOf('a', 'b').maxBy { "x$it" } })
expect("abc", { listOf("b", "abc").maxBy { it.length() } })
expect(null, { listOf<Int>().stream().maxBy { it } })
expect(2, { listOf(2, 3).stream().maxBy { -it } })
expect(null, { listOf<Int>().sequence().maxBy<Int, Int> { it } })
expect(2, { listOf(2, 3).sequence().maxBy<Int, Int> { -it } })
}
test fun minByEvaluateOnce() {
@@ -416,7 +416,7 @@ class CollectionTest {
expect(1, { listOf(5, 4, 3, 2, 1).minBy { c++; it * it } })
assertEquals(5, c)
c = 0
expect(1, { listOf(5, 4, 3, 2, 1).stream().minBy { c++; it * it } })
expect(1, { listOf(5, 4, 3, 2, 1).sequence().minBy<Int, Int> { c++; it * it } })
assertEquals(5, c)
}
@@ -425,7 +425,7 @@ class CollectionTest {
expect(5, { listOf(5, 4, 3, 2, 1).maxBy { c++; it * it } })
assertEquals(5, c)
c = 0
expect(5, { listOf(5, 4, 3, 2, 1).stream().maxBy { c++; it * it } })
expect(5, { listOf(5, 4, 3, 2, 1).sequence().maxBy<Int, Int> { c++; it * it } })
assertEquals(5, c)
}
@@ -435,7 +435,7 @@ class CollectionTest {
expect(3.0) { listOf(1.0, 2.0).sum() }
expect(3000000000000) { arrayListOf<Long>(1000000000000, 2000000000000).sum() }
expect(3.0.toFloat()) { arrayListOf<Float>(1.0.toFloat(), 2.0.toFloat()).sum() }
expect(3.0.toFloat()) { streamOf<Float>(1.0.toFloat(), 2.0.toFloat()).sum() }
expect(3.0.toFloat()) { sequenceOf<Float>(1.0.toFloat(), 2.0.toFloat()).sum() }
}
test fun takeReturnsFirstNElements() {
+1 -1
View File
@@ -68,7 +68,7 @@ class MapTest {
test fun stream() {
val map = mapOf("beverage" to "beer", "location" to "Mells", "name" to "James")
val named = map.stream().filter { it.key == "name" }.single()
val named = map.sequence().filter { it.key == "name" }.single()
assertEquals("James", named.value)
}
@@ -21,7 +21,7 @@ class MutableCollectionTest {
val list = listOf("foo", "bar")
val collection = ArrayList<String>()
collection.addAll(list.stream())
collection.addAll(list.sequence())
assertEquals(list, collection)
}
@@ -0,0 +1,26 @@
package test.collections
import org.junit.Test as test
import kotlin.test.assertEquals
class SequenceJVMTest {
test fun filterIsInstance() {
val src: Sequence<Any> = listOf(1, 2, 3.toDouble(), "abc", "cde").sequence()
val intValues: Sequence<Int> = src.filterIsInstance<Int>()
assertEquals(listOf(1, 2), intValues.toArrayList())
val doubleValues: Sequence<Double> = src.filterIsInstance<Double>()
assertEquals(listOf(3.0), doubleValues.toArrayList())
val stringValues: Sequence<String> = src.filterIsInstance<String>()
assertEquals(listOf("abc", "cde"), stringValues.toArrayList())
val anyValues: Sequence<Any> = src.filterIsInstance<Any>()
assertEquals(src.toList(), anyValues.toArrayList())
val charValues: Sequence<Char> = src.filterIsInstance<Char>()
assertEquals(0, charValues.toArrayList().size())
}
}
@@ -4,12 +4,12 @@ import org.junit.Test as test
import kotlin.test.*
import java.util.*
fun fibonacci(): Stream<Int> {
fun fibonacci(): Sequence<Int> {
// fibonacci terms
var index = 0;
var a = 0;
var b = 1
return stream {
return sequence {
when (index++) { 0 -> a; 1 -> b; else -> {
val result = a + b; a = b; b = result; result
}
@@ -17,26 +17,26 @@ fun fibonacci(): Stream<Int> {
}
}
public class StreamTest {
public class SequenceTest {
test fun filterEmptyStream() {
val stream = streamOf<String>()
test fun filterEmptySequence() {
val stream = sequenceOf<String>()
assertEquals(0, stream.filter { false }.count())
assertEquals(0, stream.filter { true }.count())
}
test fun mapEmptyStream() {
val stream = streamOf<String>()
val stream = sequenceOf<String>()
assertEquals(0, stream.map { false }.count())
assertEquals(0, stream.map { true }.count())
}
test fun requireNoNulls() {
val stream = streamOf<String?>("foo", "bar")
val stream = sequenceOf<String?>("foo", "bar")
val notNull = stream.requireNoNulls()
assertEquals(listOf("foo", "bar"), notNull.toList())
val streamWithNulls = streamOf("foo", null, "bar")
val streamWithNulls = sequenceOf("foo", null, "bar")
val notNull2 = streamWithNulls.requireNoNulls() // shouldn't fail yet
fails {
// should throw an exception as we have a null
@@ -45,35 +45,35 @@ public class StreamTest {
}
test fun filterNullable() {
val data = streamOf(null, "foo", null, "bar")
val data = sequenceOf(null, "foo", null, "bar")
val filtered = data.filter { it == null || it == "foo" }
assertEquals(listOf(null, "foo", null), filtered.toList())
}
test fun filterNot() {
val data = streamOf(null, "foo", null, "bar")
val data = sequenceOf(null, "foo", null, "bar")
val filtered = data.filterNot { it == null }
assertEquals(listOf("foo", "bar"), filtered.toList())
}
test fun filterNotNull() {
val data = streamOf(null, "foo", null, "bar")
val data = sequenceOf(null, "foo", null, "bar")
val filtered = data.filterNotNull()
assertEquals(listOf("foo", "bar"), filtered.toList())
}
test fun mapNotNull() {
val data = streamOf(null, "foo", null, "bar")
val data = sequenceOf(null, "foo", null, "bar")
val foo = data.mapNotNull { it.length() }
assertEquals(listOf(3, 3), foo.toList())
assertTrue {
foo is Stream<Int>
foo is Sequence<Int>
}
}
test fun withIndex() {
val data = streamOf("foo", "bar")
val data = sequenceOf("foo", "bar")
val indexed = data.withIndex().map { it.value.substring(0..it.index) }.toList()
assertEquals(2, indexed.size())
assertEquals(listOf("f", "ba"), indexed)
@@ -112,12 +112,12 @@ public class StreamTest {
test fun dropWhile() {
assertEquals("233, 377, 610", fibonacci().dropWhile { it < 200 }.take(3).joinToString(limit = 10))
assertEquals("", streamOf(1).dropWhile { it < 200 }.joinToString(limit = 10))
assertEquals("", sequenceOf(1).dropWhile { it < 200 }.joinToString(limit = 10))
}
test fun merge() {
expect(listOf("ab", "bc", "cd")) {
streamOf("a", "b", "c").merge(streamOf("b", "c", "d")) { a, b -> a + b }.toList()
sequenceOf("a", "b", "c").merge(sequenceOf("b", "c", "d")) { a, b -> a + b }.toList()
}
}
@@ -128,24 +128,24 @@ public class StreamTest {
}
test fun plus() {
val stream = streamOf("foo", "bar")
val stream = sequenceOf("foo", "bar")
val streamCheese = stream + "cheese"
assertEquals(listOf("foo", "bar", "cheese"), streamCheese.toList())
// lets use a mutable variable
var mi = streamOf("a", "b")
var mi = sequenceOf("a", "b")
mi += "c"
assertEquals(listOf("a", "b", "c"), mi.toList())
}
test fun plusCollection() {
val a = streamOf("foo", "bar")
val a = sequenceOf("foo", "bar")
val b = listOf("cheese", "wine")
val stream = a + b
assertEquals(listOf("foo", "bar", "cheese", "wine"), stream.toList())
// lets use a mutable variable
var ml = streamOf("a")
var ml = sequenceOf("a")
ml += a
ml += "beer"
ml += b
@@ -156,7 +156,7 @@ public class StreamTest {
test fun iterationOverStream() {
var s = ""
for (i in streamOf(0, 1, 2, 3, 4, 5)) {
for (i in sequenceOf(0, 1, 2, 3, 4, 5)) {
s = s + i.toString()
}
assertEquals("012345", s)
@@ -165,7 +165,7 @@ public class StreamTest {
test fun streamFromFunction() {
var count = 3
val stream = stream {
val stream = sequence {
count--
if (count >= 0) count else null
}
@@ -175,18 +175,18 @@ public class StreamTest {
}
test fun streamFromFunctionWithInitialValue() {
val values = stream(3) { n -> if (n > 0) n - 1 else null }
val values = sequence(3) { n -> if (n > 0) n - 1 else null }
assertEquals(listOf(3, 2, 1, 0), values.toList())
}
private fun <T, C : MutableCollection<in T>> Stream<T>.takeWhileTo(result: C, predicate: (T) -> Boolean): C {
private fun <T, C : MutableCollection<in T>> Sequence<T>.takeWhileTo(result: C, predicate: (T) -> Boolean): C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
test fun streamExtensions() {
val d = ArrayList<Int>()
streamOf(0, 1, 2, 3, 4, 5).takeWhileTo(d, { i -> i < 4 })
sequenceOf(0, 1, 2, 3, 4, 5).takeWhileTo(d, { i -> i < 4 })
assertEquals(4, d.size())
}
@@ -201,27 +201,27 @@ public class StreamTest {
'5' // fibonacci(10) = 55
)
assertEquals(expected, fibonacci().drop(4).flatMap { it.toString().stream() }.take(10).toList())
assertEquals(expected, fibonacci().drop(4).flatMap { it.toString().sequence() }.take(10).toList())
}
test fun flatMap() {
val result = streamOf(1, 2).flatMap { streamOf(0..it) }
val result = sequenceOf(1, 2).flatMap { sequenceOf(0..it) }
assertEquals(listOf(0, 1, 0, 1, 2), result.toList())
}
test fun flatMapOnEmpty() {
val result = streamOf<Int>().flatMap { streamOf(0..it) }
val result = sequenceOf<Int>().flatMap { sequenceOf(0..it) }
assertTrue(result.none())
}
test fun flatMapWithEmptyItems() {
val result = streamOf(1, 2, 4).flatMap { if (it == 2) streamOf<Int>() else streamOf(it - 1..it) }
val result = sequenceOf(1, 2, 4).flatMap { if (it == 2) sequenceOf<Int>() else sequenceOf(it - 1..it) }
assertEquals(listOf(0, 1, 3, 4), result.toList())
}
test
fun flatten() {
val data = streamOf(1, 2).map { streamOf(0..it) }
val data = sequenceOf(1, 2).map { sequenceOf(0..it) }
assertEquals(listOf(0, 1, 0, 1, 2), data.flatten().toList())
}
@@ -1,26 +0,0 @@
package test.collections
import org.junit.Test as test
import kotlin.test.assertEquals
class StreamJVMTest {
test fun filterIsInstance() {
val src: Stream<Any> = listOf(1, 2, 3.toDouble(), "abc", "cde").stream()
val intValues: Stream<Int> = src.filterIsInstance<Int>()
assertEquals(listOf(1, 2), intValues.toArrayList())
val doubleValues: Stream<Double> = src.filterIsInstance<Double>()
assertEquals(listOf(3.0), doubleValues.toArrayList())
val stringValues: Stream<String> = src.filterIsInstance<String>()
assertEquals(listOf("abc", "cde"), stringValues.toArrayList())
val anyValues: Stream<Any> = src.filterIsInstance<Any>()
assertEquals(src.toList(), anyValues.toArrayList())
val charValues: Stream<Char> = src.filterIsInstance<Char>()
assertEquals(0, charValues.toArrayList().size())
}
}
@@ -10,7 +10,7 @@ class FunctionIteratorTest {
Test fun iterateOverFunction() {
var count = 3
val iter = stream<Int> {
val iter = sequence<Int> {
count--
if (count >= 0) count else null
}
@@ -20,7 +20,7 @@ class FunctionIteratorTest {
}
Test fun iterateOverFunction2() {
val values = stream<Int>(3) { n -> if (n > 0) n - 1 else null }
val values = sequence<Int>(3) { n -> if (n > 0) n - 1 else null }
assertEquals(arrayListOf(3, 2, 1, 0), values.toList())
}
}
@@ -11,7 +11,7 @@ class IteratorsJVMTest {
fun intToBinaryDigits() = { (i: Int) ->
val binary = Integer.toBinaryString(i)!!
var index = 0
stream<Char> { if (index < binary.length()) binary.get(index++) else null }
sequence<Char> { if (index < binary.length()) binary.get(index++) else null }
}
val expected = arrayListOf(
@@ -27,7 +27,7 @@ class IteratorsJVMTest {
}
test fun flatMapOnIterator() {
val result = streamOf(1, 2).flatMap { i -> (0..i).stream()}
val result = sequenceOf(1, 2).flatMap { i -> (0..i).sequence()}
assertEquals(listOf(0, 1, 0, 1, 2), result.toList())
}
}
@@ -5,10 +5,10 @@ import org.junit.Test as test
import kotlin.test.fails
import java.util.ArrayList
fun fibonacci(): Stream<Int> {
fun fibonacci(): Sequence<Int> {
// fibonacci terms
var index = 0; var a = 0; var b = 1
return stream<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
return sequence<Int> { when (index++) { 0 -> a; 1 -> b; else -> { val result = a + b; a = b; b = result; result } } }
}
class IteratorsTest {
@@ -39,12 +39,12 @@ class IteratorsTest {
}
test fun plus() {
val iter = arrayListOf("foo", "bar").stream()
val iter = arrayListOf("foo", "bar").sequence()
val iter2 = iter + "cheese"
assertEquals(arrayListOf("foo", "bar", "cheese"), iter2.toList())
// lets use a mutable variable
var mi = streamOf("a", "b")
var mi = sequenceOf("a", "b")
mi += "c"
assertEquals(arrayListOf("a", "b", "c"), mi.toList())
}
@@ -52,12 +52,12 @@ class IteratorsTest {
test fun plusCollection() {
val a = arrayListOf("foo", "bar")
val b = arrayListOf("cheese", "wine")
val iter = a.stream() + b.stream()
val iter = a.sequence() + b.sequence()
assertEquals(arrayListOf("foo", "bar", "cheese", "wine"), iter.toList())
// lets use a mutable variable
var ml = arrayListOf("a").stream()
ml += a.stream()
var ml = arrayListOf("a").sequence()
ml += a.sequence()
ml += "beer"
ml += b
ml += "z"
@@ -65,11 +65,11 @@ class IteratorsTest {
}
test fun requireNoNulls() {
val iter = arrayListOf<String?>("foo", "bar").stream()
val iter = arrayListOf<String?>("foo", "bar").sequence()
val notNull = iter.requireNoNulls()
assertEquals(arrayListOf("foo", "bar"), notNull.toList())
val iterWithNulls = arrayListOf("foo", null, "bar").stream()
val iterWithNulls = arrayListOf("foo", null, "bar").sequence()
val notNull2 = iterWithNulls.requireNoNulls()
fails {
// should throw an exception as we have a null