Skip to content

Instantly share code, notes, and snippets.

View wafisher's full-sized avatar

Will Fisher wafisher

View GitHub Profile
@chourobin
chourobin / 0-bridging-react-native-cheatsheet.md
Last active June 28, 2024 20:49
React Native Bridging Cheatsheet
@matklad
matklad / takeWhileInclusive.kt
Created December 24, 2016 21:03
An "inclusive" version of Kotlin's takeWhile
fun <T> Sequence<T>.takeWhileInclusive(pred: (T) -> Boolean): Sequence<T> {
var shouldContinue = true
return takeWhile {
val result = shouldContinue
shouldContinue = pred(it)
result
}
}
@hastebrot
hastebrot / partition.md
Last active April 12, 2022 10:22
Algorithm: Fixed and Sliding Windows in Kotlin
  • partitionRanges.kt: partition() implements a function for fixed and sliding windows suited for lists (not sequences) using indices/ranges.

  • partitionRecursion.kt: partitionRec() and partitionTailrec() 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 a StackOverflowException.

  • partitionIterator.kt: batch() and slide() implement such functions suited for lists and sequences (similar to the prototype implementation). Small "problem" here is that source.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 with sequenceOf(...).constrainOnce():

@ghiermann
ghiermann / PolylineUtils.kt
Created August 23, 2016 08:20
Kotlin PolylineUtils - encoding/decoding of polylines, simplification with Ramer-Douglas–Peucker algorithm
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
@mikehearn
mikehearn / Kryo Kotlin class serialiser.kt
Created December 17, 2015 10:53
A Kryo serialiser that lets you serialise immutable classes like "class Foo(val a: Int, val b: Bar)" without bypassing the c'tor.
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<*> })