add function for generating sequence of pairs (value, previousValue)

This commit is contained in:
Daniil Ovchinnikov
2017-09-06 18:03:51 +03:00
parent 0df4e5368a
commit 111ed09e3d
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.util
typealias LookbackValue<T> = Pair<T, T?>
/**
* @return sequence of `Pair(value, previousValue)`. First element of sequence has `null` previous value.
*/
fun <T> Sequence<T>.withPrevious(): Sequence<LookbackValue<T>> = LookbackSequence(this)
private class LookbackSequence<T>(private val sequence: Sequence<T>) : Sequence<LookbackValue<T>> {
override fun iterator(): Iterator<LookbackValue<T>> = LookbackIterator(sequence.iterator())
}
private class LookbackIterator<T>(private val iterator: Iterator<T>) : Iterator<LookbackValue<T>> {
private var previous: T? = null
override fun hasNext() = iterator.hasNext()
override fun next(): LookbackValue<T> {
val next = iterator.next()
val result = LookbackValue(next, previous)
previous = next
return result
}
}