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")
@poetix
poetix / iterators.js
Created December 27, 2011 22:28
Lazy map, filter and reduce in Javascript
Iterators = (function() {
var each, map, filter, reduce, toArray, toObject;
function _map(iter, f) {
return {
hasNext: iter.hasNext,
next: function() { return f(iter.next()); }
};
}
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());
}
@poetix
poetix / RecorderSpec.scala
Created January 14, 2013 17:20
A monad which captures dynamic method invocations against multiple targets (for use in implementing object builders / matchers)
package com.youdevise.lofty
import org.specs2.mutable._
import scala.language.dynamics
trait State[S, A] {
val runState: Function[S, (S, A)]
def flatMap[B](f: A => Function[S, (S, B)]):Function[S, (S, B)] = (state:S) => {
val (newState, value) = runState(state)
f(value)(newState)
@poetix
poetix / json_to_csv.py
Created August 29, 2012 09:58
Short script to fetch JSON from MongoDB, flatten to dictionary of leaf nodes, and write everything out in CSV format
#!/usr/bin/python
import urllib2
import argparse
import json
parser = argparse.ArgumentParser(description="Fetch some JSON")
parser.add_argument("url")
args = parser.parse_args()
@poetix
poetix / Nonchalantly.java
Last active December 21, 2017 17:11
A Java 8 class for when you just don't give a fuck about that checked exception
public interface Nonchalantly<T> {
static <T, E extends Throwable> T invoke(Nonchalantly<T> f) throws E {
try {
return f.run();
} catch (Throwable e) {
throw (E) e;
}
}
T run() throws Throwable;
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);
}