Skip to content

Instantly share code, notes, and snippets.

View twfarland's full-sized avatar

Tim Farland twfarland

  • Farland Digital
  • Auckland, NZ
View GitHub Profile
@twfarland
twfarland / YoutubeDownloader.cs
Last active March 17, 2023 23:44
Downloads youtube videos and converts them to mp3 via streaming. ChatpGPT 4 wrote most of this (with minor babysitting from me).
using NAudio.Wave;
using NAudio.Lame;
using YoutubeExplode;
using YoutubeExplode.Videos.Streams;
namespace YoutubeDownloader
{
class Program
{
static async Task Main(string[] args)
@twfarland
twfarland / RbacOrg.sol
Created December 17, 2021 01:32
Role based access organisation smart contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
// RbacOrgManager (index of orgs, invites for self)
// RbacOrg
/**
* @title RbacOrg
* @dev Autonomous organisation with role based access
@twfarland
twfarland / dafsa.ml
Last active September 30, 2021 00:01
Incremental Construction of Minimal Acyclic Finite-State Automata (DAFSA aka DAWG) - Ocaml implementation
(* An implementation of algorithm 1 from https://aclanthology.org/J00-1002.pdf *)
open Printf
module IntSet = Set.Make(Int)
module CharSet = Set.Make(Char)
module IntMap = Map.Make(Int)
module CharMap = Map.Make(Char)
module StringSet = Set.Make(String)
module StringMap = Map.Make(String)
@twfarland
twfarland / svelte-frp.ts
Created July 21, 2021 20:03
Experiment in doing FRP directly with svelte stores (no external library like RxJS)
import {
derived,
writable,
Readable,
Unsubscriber,
readable,
} from "svelte/store";
export function map<A, B>(
mapper: (value: A) => B,
async function* seconds() {
let i = 0
while (i < 10) {
await new Promise(resolve => setTimeout(resolve, 1000))
i++
yield i
}
}
function map(f, gen) {
@twfarland
twfarland / createStore.ts
Created May 13, 2020 22:45
createStore hooks to share state across components, a lightweight and simpler replacement for redux or even context
import { useState, useEffect, Dispatch, SetStateAction } from "react"
type SetState<S> = Dispatch<SetStateAction<S>>
/*
Shares state amongst all instances, without using context API
This is a more lightweight redux alternative
Note that the initial state is set outside of any mounted instance,
as in redux.
*/
@twfarland
twfarland / permutate.js
Last active April 21, 2019 03:53
word games - permutate/temperature game
const fs = require('fs')
const readline = require('readline')
async function readDict(dictFile, wordLength) {
const words = {}
return new Promise((resolve) => {
@twfarland
twfarland / xylophone.js
Created August 13, 2018 06:14
web audio xylophone
function createXylophone() {
const context = new (window.AudioContext || window.webkitAudioContext)()
const oscillator = context.createOscillator()
const gain = context.createGain()
oscillator.type = 'sine'
oscillator.connect(gain)
gain.connect(context.destination)
@twfarland
twfarland / PNCounter.ts
Last active April 8, 2018 20:46
Postitive - Negative Counter CvRDT
function uuid(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
type NumberMap = { [id: string]: number }
class PNCounter {
@twfarland
twfarland / proxy-store.ts
Last active March 29, 2018 21:34
Using Proxies as a less verbose redux replacement
// ---------- Store builder
export interface Initial<S> {
(): S
}
export interface Listener<S> {
(obj: S, prop?: (keyof S), value?: any): any
}