Skip to content

Instantly share code, notes, and snippets.

@poetix
poetix / example.java
Last active December 21, 2015 05:49
Lenses and magic records with spinoza / hume
package com.codepoetics.hume;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import org.junit.Test;
import com.codepoetics.hume.api.Property;
import com.codepoetics.hume.api.Record;
import com.codepoetics.spinoza.Compose;
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
@poetix
poetix / Table.kt
Last active June 2, 2016 09:51
SQL -> Data Class mapping without reflection
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
@poetix
poetix / Poly.purs
Last active July 4, 2017 16:35
Find and print all 35 Hexominoes
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)

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 / structural_typing.ts
Last active September 25, 2017 10:46
Unit testing an endpoint class with a mocked service class
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,
@poetix
poetix / structural_typing2.ts
Created September 25, 2017 11:08
Now without the interface...
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_typing3.ts
Created September 25, 2017 12:39
Now with mocking
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 / pyramid.idr
Last active October 26, 2017 22:07
module Main
forLoop : List a -> (a -> IO ()) -> IO ()
forLoop [] f = return ()
forLoop (x :: xs) f = do f x
forLoop xs f
syntax for {x} "in" [xs] ":" [body] = forLoop xs (\x => body)
spaces : Nat -> String
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")