Skip to content

Instantly share code, notes, and snippets.

@npryce
npryce / merge-to-subdir
Last active February 22, 2024 17:23
Merge history from one Git repository as the history of a subdirectory of another Git repository
#!/bin/bash
# Usage: merge-to-subdir source-repo destination-repo subdir
#
# Merges the history of source-repo into destination-repo as the
# history of the subdirectory subdir.
#
# source-repo can be local or remote.
# destination-repo must be local to the machine.
# subdir can be a relative path, in which case intermediate
@npryce
npryce / property-based-testing-tools.md
Last active August 14, 2022 20:34
Property-Based Testing Tools

If you're coming to the Property-Based TDD As If You Meant It Workshop, you will need to bring a laptop with your favourite programming environment, a property-based testing library and, depending on the language, a test framework to run the property-based-tests.

Any other languages or suggestions? Comment below.

.NET (C#, F#, VB)

Python:

@npryce
npryce / string-char-to-int-pitfall.md
Created January 26, 2019 23:33
string-char-to-int-pitfall

In Kotlin String.toInt() and Char.toInt() do very different things.

>>> "9".toInt()
9
>>> '9'.toInt()
57
>>> 
@npryce
npryce / layout.kt
Created December 6, 2016 11:44
kotlin-layout-hack
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
val <T> T.` `: T get() = this
@npryce
npryce / wrapping-with-after-before.kt
Last active October 31, 2018 09:46
minutest-hooks
fun TestContext<*>.withTestTiming() {
var start: Long? = null
before {
start = System.currentTimeMillis()
}
after {
start?.let {
val timeTaken = System.currentTimeMillis() - it
@npryce
npryce / fixture-with-descriptor.kt
Created October 31, 2018 09:37
Minutest Fixtures with Descriptors
class Fixture {
val tempFile: File
}
fixture { testDescriptor ->
Fixture(System.createTempFile(testDescriptor.fullName.joinToString("/"), ".json"))
}
test {
doSomethingThatWritesTo(tempFile)
@npryce
npryce / reference-to-extension-method-of-nullable-type-via-nonnullable-type-pitfall.md
Created January 31, 2018 15:42
Taking a reference to an extension of a nullable type is possible through the non-nullable type, but probably not what you want!

E.g. the stdlib defines the following function:

fun CharSequence?.isNullOrBlank(): Boolean

You can take a reference to this extension function like this:

val isNullOrBlankFn = CharSequence?::isNullOrBlank
@npryce
npryce / speech_synthesis.kt
Created December 12, 2017 15:34
Kotlin definitions for the browser speech synthesis API
package browser
import org.w3c.dom.events.Event
import org.w3c.dom.events.EventTarget
typealias EventHandler<E> = (E) -> Unit
external val speechSynthesis: SpeechSynthesis
external class SpeechSynthesis : EventTarget {
@npryce
npryce / touch.kt
Created December 12, 2017 15:34
Kotlin definitions for browser touch API
package browser
import org.w3c.dom.Element
import org.w3c.dom.events.UIEvent
typealias TouchId = Int
external interface Touch {
val identifier: TouchId
val target: Element
@npryce
npryce / kotlin-java-interop-overloading-pitfall.md
Last active July 30, 2017 13:28
Kotlin/Java interop method overloading pitfall

Given a Java library that defines overloaded methods for primitive and boxed types:

class X {
    public void m(int value) { ... }
    public void m(Integer value) { ... }
    ...
}