Skip to content

Instantly share code, notes, and snippets.

@npryce
npryce / add-to-string-pitfall.md
Last active Oct 17, 2016
Another Kotlin string pitfalls
View add-to-string-pitfall.md

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 Jul 30, 2017
Kotlin/Java interop method overloading pitfall
View kotlin-java-interop-overloading-pitfall.md

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 Sep 18, 2016
A Kotlin pitfall w.r.t. nulls and toString
View kotlin-null-tostring-pitfall.md
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 Mar 8, 2016
Using Krouton (typesafe routing & reverse routing) with Wasabi (Sinatra-like web server library for Kotlin)
View WasabiKrouton.kt
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 Oct 14, 2015
Metaphor Bibliography
View metaphor.bib
@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 Sep 10, 2015
Testing react components with Mocha and NodeJS
View react-for-mocha.js
// 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 Aug 29, 2015
Refactoring to Asynchronous in JavaScript
View js-sync-to-async-cps-transform.md

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 Aug 29, 2015
Makefile to bootstrap and build a Purescript project
View Makefile
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 Aug 29, 2015
How to create a Log4J Logger for unit testing Java code that logs
View LoggerTest.java
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 Aug 29, 2015
merchant-of-venice
View MerchantOfVenice.java
public interface Prickable {
void prick();
boolean isBleeding();
}
public interface Ticklable {
void tickle();
boolean isLaughing();
}