Skip to content

Instantly share code, notes, and snippets.

@poetix
poetix / heap.ts
Created September 25, 2023 14:51
Heap and Priority Queue in Typescript
class Heap<T> {
selectMax: (l: T, r: T) => boolean;
data: T[];
constructor(selectMax: (l: T, r: T) => boolean) {
this.selectMax = selectMax;
this.data = [];
}
length(): number {
@poetix
poetix / bft.ts
Created September 25, 2023 12:16
Breadth-first graph traversal in Typescript
/**
* Given a start node and a function which provides an Iterable of adjacent nodes for any node,
* perform a breadth-first traversal of the graph and yield each visited node together with
* the minimum number of steps needed to reach that node from the start node.
*/
function *bft<T>(start: T, adjacent: (node: T) => Iterable<T>): IterableIterator<[T, number]> {
const visited: Set<T> = new Set();
const queue: T[] = [start];
visited.add(start);
var depth = 1;
@poetix
poetix / pangolins.hs
Created October 10, 2022 09:48
Pangolins in Haskell
module Lib
( playRepeatedly,
firstStep
) where
import System.IO
data GameStep = Guess { animalName :: String }
| Question {
question :: String,
@poetix
poetix / wordle_functions.py
Last active March 1, 2022 17:01
Python functions for investigation Wordle
import itertools
from urllib import request
import os
if not os.path.isfile('count_1w.txt'):
request.urlretrieve(
"https://norvig.com/ngrams/count_1w.txt",
"count_1w.txt")
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);
}
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")
@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 / 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_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,

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: