Reverting the pull-request #29 due to the upcoming redesign of the lazy collections API
This commit is contained in:
@@ -1,105 +0,0 @@
|
||||
package kotlin.sequences
|
||||
|
||||
/**
|
||||
* A sequence of items of type *T* that are lazily produced by the given closure
|
||||
*
|
||||
* @param next a closure yielding the next element in this sequence
|
||||
*
|
||||
* @author [Franck Rasolo](http://www.linkedin.com/in/franckrasolo)
|
||||
* @since 1.0
|
||||
*/
|
||||
class Sequence<in T>(val next: () -> T?) : Iterable<T> {
|
||||
|
||||
override fun iterator(): Iterator<T> = YieldingIterator { (next)() }
|
||||
|
||||
/**
|
||||
* Retains only elements that satisfy the given predicate
|
||||
*
|
||||
* @param predicate the predicate evaluated against objects of type *T*
|
||||
*/
|
||||
fun filter(predicate: (T) -> Boolean): Sequence<T> {
|
||||
val iterator = iterator()
|
||||
|
||||
fun next(): T? {
|
||||
while (iterator.hasNext) {
|
||||
val item = iterator.next()
|
||||
if ((predicate)(item)) return item
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return Sequence<T> { next() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Reduces this sequence using the binary operator, from left to right.
|
||||
* Only finite sequences can be reduced.
|
||||
*
|
||||
* @param seed an initial value, typically the left-identity of the operator
|
||||
* @param operator a binary operator
|
||||
*/
|
||||
fun fold(seed: T, operator: (T, T) -> T): T {
|
||||
var result = seed
|
||||
for (item in this) result = (operator)(result, item)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces a sequence obtained by applying *transform* to each element of this sequence
|
||||
*
|
||||
* @param transform the function transforming an object of type *T* into an object of type *R*
|
||||
*/
|
||||
fun <in R> map(transform: (T) -> R): Sequence<R> {
|
||||
val iterator = iterator()
|
||||
fun next(): R? = if (iterator.hasNext) (transform)(iterator.next()) else null
|
||||
return Sequence<R> { next() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the prefix of this sequence as a finite sequence of length *n*
|
||||
*
|
||||
* @param n the number of elements of the extracted sequence
|
||||
*/
|
||||
fun take(n: Int): Sequence<T> {
|
||||
fun countTo(n: Int): (T) -> Boolean {
|
||||
var count = 0
|
||||
return { ++count; count <= n }
|
||||
}
|
||||
return takeWhile(countTo(n))
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the longest prefix, possibly empty, of this sequence where each element satisfies *predicate*
|
||||
*
|
||||
* @param predicate the predicate evaluated against objects of type *T*
|
||||
*/
|
||||
fun takeWhile(predicate: (T) -> Boolean): Sequence<T> {
|
||||
val iterator = iterator()
|
||||
|
||||
fun next(): T? {
|
||||
if (iterator.hasNext) {
|
||||
val item = iterator.next()
|
||||
if ((predicate)(item)) return item
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return Sequence<T> { next() }
|
||||
}
|
||||
}
|
||||
|
||||
private class YieldingIterator<T>(val yield: () -> T?) : Iterator<T> {
|
||||
var current : T? = (yield)()
|
||||
|
||||
override val hasNext: Boolean
|
||||
get() = current != null
|
||||
|
||||
override fun next(): T {
|
||||
val next = current
|
||||
if (next != null) {
|
||||
current = (yield)()
|
||||
return next
|
||||
}
|
||||
throw java.util.NoSuchElementException()
|
||||
}
|
||||
}
|
||||
@@ -1,74 +0,0 @@
|
||||
package kotlin.sequences
|
||||
|
||||
/** Creates a sequence from the given list of *elements* */
|
||||
inline fun <T> sequence(vararg elements: T): Sequence<T> = asSequence(elements.iterator())
|
||||
|
||||
/** Creates a sequence of elements lazily obtained from this Kotlin [[jet.Iterable]] */
|
||||
inline fun <T> Iterable<T>.asSequence(): Sequence<T> = asSequence(iterator())
|
||||
|
||||
/** Creates a sequence of elements lazily obtained from this Java [[java.lang.Iterable]] */
|
||||
inline fun <T> java.lang.Iterable<T>.asSequence(): Sequence<T> = asSequence(iterator().sure() as java.util.Iterator<T>)
|
||||
|
||||
private fun <T> asSequence(iterator: Iterator<T>) = Sequence<T> { if (iterator.hasNext) iterator.next() else null }
|
||||
private fun <T> asSequence(iterator: java.util.Iterator<T>) = Sequence<T> { if (iterator.hasNext()) iterator.next() else null }
|
||||
|
||||
/**
|
||||
* Produces the [cartesian product](http://en.wikipedia.org/wiki/Cartesian_product#n-ary_product) as a sequence of ordered pairs of elements lazily obtained
|
||||
* from two [[Iterable]] instances
|
||||
*/
|
||||
fun <T> Iterable<T>.times(other: Iterable<T>): Sequence<#(T, T)> {
|
||||
val first = iterator(); var second = other.iterator(); var a: T? = null
|
||||
|
||||
fun nextPair(): #(T, T)? {
|
||||
if (a == null && first.hasNext) a = first.next()
|
||||
if (second.hasNext) return #(a.sure(), second.next())
|
||||
if (first.hasNext) {
|
||||
a = first.next(); second = other.iterator()
|
||||
return #(a.sure(), second.next())
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return Sequence<#(T, T)> { nextPair() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Partitions this string into groups of fixed size strings
|
||||
*
|
||||
* @param size the number of characters per group
|
||||
*
|
||||
* @return a sequence of strings of size *size*, except the last will be truncated if the elements do not divide evenly
|
||||
*/
|
||||
fun String.grouped(size: Int, iterator: CharIterator = iterator()): Sequence<String> {
|
||||
fun nextGroup(): String? {
|
||||
if (iterator.hasNext) {
|
||||
val window = StringBuilder()
|
||||
for (i in 1..size) if (iterator.hasNext) window.append(iterator.next())
|
||||
return window.toString()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
return Sequence<String> { nextGroup() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Groups elements in fixed size blocks by passing a *sliding window* over them, as opposed to partitioning them, as is done in `String.grouped`
|
||||
*
|
||||
* @param size the number of characters per group
|
||||
*
|
||||
* @return a sequence of strings of size *size*, except the last and the only element will be truncated if there are fewer characters than *size*
|
||||
*/
|
||||
fun String.sliding(size: Int, iterator: CharIterator = iterator()): Sequence<String> {
|
||||
val window = StringBuilder()
|
||||
|
||||
fun nextWindow(): String? {
|
||||
if (window.length() == 0) {
|
||||
for (i in 1..size) if (iterator.hasNext) window.append(iterator.next())
|
||||
return window.toString()
|
||||
}
|
||||
return if (iterator.hasNext) window.deleteCharAt(0)?.append(iterator.next()).toString() else null
|
||||
}
|
||||
|
||||
return Sequence<String> { nextWindow() }
|
||||
}
|
||||
Reference in New Issue
Block a user