Skip to content

Instantly share code, notes, and snippets.

@npryce
npryce / add-to-string-pitfall.md
Last active October 17, 2016 09:56
Another Kotlin string pitfalls

Don't write an extension method that implement the plus operator with String arguments. If you forget to import it, the code will compile, but use string append instead.

public final class String {
  ... public final operator fun plus(other: kotlin.Any?): kotlin.String { /* compiled code */ }
}

So, always write a conversion function from String to your own type, then implement + on that type.

@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) { ... }
    ...
}
@npryce
npryce / kotlin-null-tostring-pitfall.md
Created September 18, 2016 21:14
A Kotlin pitfall w.r.t. nulls and toString
val x : SomeType? = ... // Note: nullable

val s1 : String? = x?.toString()

val s2 : String? = x.toString()

The expressions s1 and s2 are subtly different. When x is null, s1 will evaluate to null, while s2 will evaluate to the string "null".

@npryce
npryce / WasabiKrouton.kt
Last active March 8, 2016 11:22
Using Krouton (typesafe routing & reverse routing) with Wasabi (Sinatra-like web server library for Kotlin)
import com.natpryce.krouton.*
import io.netty.handler.codec.http.HttpMethod
import io.netty.handler.codec.http.HttpMethod.GET
import org.wasabi.app.AppServer
import org.wasabi.http.Request
import org.wasabi.http.Response
import org.wasabi.http.StatusCodes
import org.wasabi.interceptors.Interceptor
import org.wasabi.routing.RouteHandler
@npryce
npryce / metaphor.bib
Created October 14, 2015 10:06
Metaphor Bibliography
@article{Deignan2005,
abstract = {With the benefit of hindsight, it is now possible to see that one of the most important themes in the study of language to emerge in the 20 th century was developed, not by linguists, but primarily by philosophers of language such as Wittgenstein and Grice and anthropologists such as Malinowski and Rosch. This theme involves, among other things, rejection of sharply defined category boundaries and adoption instead of systems of categories built by analogy around prototypes. Central and typical examples of linguistic categories are usually easy to identify, but boundaries between categories are fuzzy grey areas on a cline, rather than sharp divisions. Metaphor is the most prototypical example of linguistic analogy, so a corpus-based study of metaphor will be a theme of central interest. In the linguistic paradigm shift just mentioned, Aristotelian-Leibnizian theories of meaning requiring the satisfaction of necessary and sufficient conditions were rejected. Instead, meaning in
@npryce
npryce / react-for-mocha.js
Last active September 10, 2015 13:31
Testing react components with Mocha and NodeJS
// React decides if it has a DOM on loading, so if we always need to
// initialise JSDOM before loading it.
var testdom = require('testdom');
testdom('<html><body></body></html>');
// But React hates you even more than that... it caches the rendered component
// in the DOM and if the same type of component with the same key is rendered at
// into the same element, then it uses the cached instance, not the just rendered
// instance. Even if the props are different. So the instance being tested will
// not be exercised.
@npryce
npryce / js-sync-to-async-cps-transform.md
Last active August 29, 2015 14:26
Refactoring to Asynchronous in JavaScript

Note: the latest version of this document is at http://natpryce.com/articles/000812.html

Here's some code that gets and uses a value from a synchronous call or built in data structure:

function to_be_refactored() {
    var x;
    ...
 x = get_x();
@npryce
npryce / Makefile
Last active August 29, 2015 14:22
Makefile to bootstrap and build a Purescript project
npm_bin:=$(shell npm bin)
psc=$(npm_bin)/psc
bower=$(npm_bin)/bower
outdir=target
all: $(outdir)/js/HelloWorld.js
$(outdir)/js/HelloWorld.js: src/HelloWorld.purs
@npryce
npryce / LoggerTest.java
Last active August 29, 2015 14:17
How to create a Log4J Logger for unit testing Java code that logs
LoggingEvent loggedEvent = null;
private Logger capturingLogger() {
return new Logger("testing") {
{
level = Level.ALL;
repository = mock(LoggerRepository.class);
when(repository.isDisabled(anyInt())).thenReturn(false);
}
@npryce
npryce / MerchantOfVenice.java
Last active August 29, 2015 14:09
merchant-of-venice
public interface Prickable {
void prick();
boolean isBleeding();
}
public interface Ticklable {
void tickle();
boolean isLaughing();
}