Skip to content

Instantly share code, notes, and snippets.

@alkhe
alkhe / BudgetBreaker.sol
Last active November 6, 2021 15:40
Holmstrom (1982) budget breaker for eliminating moral hazard in joint production.
pragma solidity ^0.8.0;
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
contract BudgetBreaker {
enum ContractState { NOT_EXECUTED, EXECUTORY, DISBURSED }
enum MemberState { DNE, NOT_SIGNED, SIGNED }
ContractState contractState = ContractState.NOT_EXECUTED;
mapping (address => MemberState) memberStates;
@alkhe
alkhe / coroutine.ts
Created September 17, 2021 19:41
Coroutines in Typescript
type GeneratorFunction<T> = () => Generator<any, T, any>
export default function<T>(g: GeneratorFunction<T>): Promise<T> {
return new Promise((res, rej) => {
// create generator
const r = g()
function step(green: boolean, fulfilled: any) {
let result: IteratorResult<any, T> | null
@alkhe
alkhe / Usage.kt
Created September 12, 2021 09:37
incorrect but well formed
class Usage {
data class UsageEventDetails(val type: Int, val time: Long)
private fun computeUsages(begin: Long, end: Long): Map<String, Long> {
val usageStatsManager = getSystemService(USAGE_STATS_SERVICE) as UsageStatsManager
val usageEvents = usageStatsManager.queryEvents(begin, end)
val usageEventsByPackage: MutableMap<String, MutableList<UsageEventDetails>> = HashMap()
val usageEvent = UsageEvents.Event()
@alkhe
alkhe / mini-redux.js
Created January 7, 2021 05:55
Mini Redux
const store = (reducer, state) => {
return {
receive: action => {
state = reducer(state, action)
},
state: () => state
}
}
const combine = structure => (state = {}, action) => {
@alkhe
alkhe / init.lua
Last active March 19, 2023 05:27
Hammerspoon Config
hw = hs.window
hw.animationDuration = 0
hs.hotkey.bind('ctrl-alt', 'r', function()
hs.reload()
hs.notify.show('', '', 'Config Reloaded')
end)
function get_window()
@alkhe
alkhe / 1233.js
Created June 3, 2020 07:17
1233. Remove Sub-Folders from the Filesystem
function removeSubfolders(folders) {
const tree = {}
for (let i = 0; i < folders.length; i++) {
const f = folders[i]
let node = tree
let name = ''
for (let j = 1; j < f.length; j++) {
@alkhe
alkhe / async.js
Last active May 23, 2020 23:47
Javascript Async Monad
export default class Async {
result = null
finished = false
handlers = []
constructor(f) {
if (f !== undefined) f(r => this.done(r))
}
done(result) {
@alkhe
alkhe / corona-benford.js
Last active May 19, 2020 05:17
Demonstration of Benford's Law in National Coronavirus Cases and Deaths
// scraped from https://www.worldometers.info/coronavirus/ on May 19, 2020
const deaths = [91981, 2722, 27709, 16853, 34796, 32007, 28239, 8123, 4171, 7057, 3164, 2789, 4634, 5842, 320, 9080, 5332, 478, 5694, 939, 15, 2799, 1886, 171, 3698, 1231, 22, 1547, 224, 349, 936, 535, 1191, 1120, 276, 286, 749, 592, 629, 118, 645, 434, 831, 263, 548, 231, 279, 297, 382, 233, 555, 12, 173, 99, 192, 113, 35, 300, 191, 217, 29, 25, 61, 174, 107, 127, 462, 140, 40, 56, 165, 146, 16, 13, 105, 26, 133, 110, 95, 28, 38, 79, 104, 10, 64, 41, 59, 61, 7, 21, 28, 104, 57, 11, 30, 18, 14, 4, 4, 46, 4, 19, 9, 31, 26, 17, 50, 55, 52, 10, 51, 11, 51, 7, 20, 7, 13, 12, 41, 9, 10, 6, 43, 21, 9, 53, 33, 21, NaN, 7, 15, 2, 2, 5, 2, 24, 10, 12, 3, NaN, 9, NaN, 1, NaN, 4, NaN, 7, 22, 1, 2, 14, 6, NaN, 13, NaN, NaN, 1, NaN, 20, 9, 10, NaN, 8, 3, 4, 11, 1, 7, 1, 4, 15, 3, 3, NaN, 3, 3, 4, NaN, 1, 3, NaN, 8, 3, 1, 1, NaN, NaN, NaN, NaN, 2, NaN, NaN, NaN, NaN, 1, NaN, NaN, NaN, NaN, 1, NaN, 1, 1, 1, NaN, NaN, 2, 1, NaN, NaN, NaN, NaN, NaN, Na
@alkhe
alkhe / Implementations.hs
Created May 17, 2020 18:47
Implementations of various Haskell Types and Typeclasses
import qualified Prelude
import Prelude (String, Num (..), Eq (..), Int (..), show)
id :: a -> a
id x = x
const :: a -> b -> a
const x _ = x
($) :: (a -> b) -> a -> b
@alkhe
alkhe / state-monad.re
Last active May 14, 2020 20:32
General State Monad w/ Reason Module Functions
let id = x => x;
let ($) = (f, x) => f(x);
let flip = f => (x, y) => f(y, x);
let const = (x, _) => x;
let (<<<) = (f, g) => x => f(g(x));
let (>>>) = (f, g) => x => g(f(x));
module type Functor = {
type f('a);
let fmap: ('a => 'b) => f('a) => f('b);