fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> { | |
var shouldContinue = true | |
return takeWhile { | |
val result = shouldContinue | |
shouldContinue = pred(it) | |
result | |
} | |
} |
-
partitionRanges.kt:
partition()
implements a function for fixed and sliding windows suited for lists (not sequences) using indices/ranges. -
partitionRecursion.kt:
partitionRec()
andpartitionTailrec()
implement such functions suited for lists using recursion and tail recursion (very inefficient). The concatenation of a lot of sequences using.plus()
(or.flatten()
) results in aStackOverflowException
. -
partitionIterator.kt:
batch()
andslide()
implement such functions suited for lists and sequences (similar to the prototype implementation). Small "problem" here is thatsource.iterator()
introduces mutual state. Does not use RingBuffer or LinkedList, instead replaces the whole List. -
partitionZipDrop.kt: A sliding window for pairs with an offset of one, can be implemented ad hoc with this simple variant which uses
zip()
. This however does not work withsequenceOf(...).constrainOnce()
:
data class Coordinate(val longitude: Double, val latitude: Double) | |
/** | |
* Encodes a polyline using Google's polyline algorithm | |
* (See http://code.google.com/apis/maps/documentation/polylinealgorithm.html for more information). | |
* | |
* code derived from : https://gist.github.com/signed0/2031157 | |
* | |
* @param (x,y)-Coordinates |
interface SerializeableWithKryo | |
class ImmutableClassSerializer<T : SerializeableWithKryo>(val klass: KClass<T>) : Serializer<T>() { | |
val props = klass.memberProperties.sortedBy { it.name } | |
val propsByName = props.toMapBy { it.name } | |
val constructor = klass.primaryConstructor!! | |
init { | |
// Verify that this class is immutable (all properties are final) | |
assert(props.none { it is KMutableProperty<*> }) |