Skip to content

Instantly share code, notes, and snippets.

View MapValueRetrievalExample.java
public String ofNullableExample(Map<String, String> map) {
return Optional.ofNullable(map.get(key))
.orElseThrow(() -> new ItemNotFoundException(key));
}
public String nullCheckingExample(Map<String, String> map) {
String valueAtKey = map.get(key);
if (valueAtKey == null) {
throw new ItemNotFoundException(key);
}
View DefensiveNullIntolerance.java
public final class Person {
// Publicly accessible, all parameters validated.
public static Person with(String name, int age, String favouriteColour) {
checkArgument(age > 0, "age must be greater than 0");
return new Person(
checkNotNull(name, "name must not be null"),
age,
checkNotNull(favouriteColour, "favouriteColour must not be null")
View structural_typing3.ts
import { expect } from 'chai';
import 'mocha';
import { mock, instance, when, anyString, verify } from "ts-mockito";
class HelloEndpoint {
constructor(private service: HelloService) {}
async handle(evt: any): Promise<any> {
const greeting = await this.service.sayHello(evt.queryStringParameters.name);
return {
@poetix
poetix / structural_typing2.ts
Created Sep 25, 2017
Now without the interface...
View structural_typing2.ts
import { expect } from 'chai';
import 'mocha';
class HelloEndpoint {
constructor(private service: HelloService) {}
async handle(evt: any): Promise<any> {
const greeting = await this.service.sayHello(evt.queryStringParameters.name);
return {
body: greeting
@poetix
poetix / structural_typing.ts
Last active Sep 25, 2017
Unit testing an endpoint class with a mocked service class
View structural_typing.ts
import { expect } from 'chai';
import 'mocha';
class HelloEndpoint {
constructor(private service: HelloService) {}
async handle(evt: any): Promise<any> {
const greeting = await this.service.sayHello(evt.queryStringParameters.name);
return {
statusCode: 200,
View keybase.md

Keybase proof

I hereby claim:

  • I am poetix on github.
  • I am poetix (https://keybase.io/poetix) on keybase.
  • I have a public key ASDdtm9fCHu0-fJArrQzHByuiG-rDxBbi1TeELZ3YLElBwo

To claim this, I am signing this object:

@poetix
poetix / Poly.purs
Last active Jul 4, 2017
Find and print all 35 Hexominoes
View Poly.purs
module Main where
import Prelude
import Control.Plus (empty)
import Data.Array as A
import Data.Set as S
import Control.Monad.Eff.Console (log)
import Data.Foldable (minimum, maximum)
import Data.Maybe (fromMaybe)
import Data.String (joinWith, fromCharArray)
@poetix
poetix / Table.kt
Last active Jun 2, 2016
SQL -> Data Class mapping without reflection
View Table.kt
package com.codepoetics.kontinuous
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
data class Foo(val a: String, val b: Int, val c: Double)
interface RowN {
val selectSql: String
View paths.hs
import qualified Data.Map as Map
type SubPaths = Map.Map String Node
data Node = Content SubPaths | NoContent SubPaths deriving (Show)
content :: [(String, Node)] -> Node
content kvs = Content $ Map.fromList kvs
nocontent :: [(String, Node)] -> Node
nocontent kvs = NoContent $ Map.fromList kvs
View Tunnel.java
import java.util.function.*;
public final class Tunnel<E extends Exception> {
public static <E extends Exception, R> R call(Class<? extends E> exceptionClass, Function<Tunnel<E>, R> f) throws E {
try {
return f.apply(new Tunnel<>());
} catch (TunnelledException e) {
throw exceptionClass.cast(e.getCause());
}