Skip to content

Instantly share code, notes, and snippets.

View SegFaultAX's full-sized avatar

Michael-Keith Bernard SegFaultAX

View GitHub Profile
@SegFaultAX
SegFaultAX / fib.lua
Created May 23, 2012 00:48
Lua Fibonacci
-- Author: Michael-Keith Bernard
-- Date: May 22, 2012
-- Description: Various implementations of the Fibonacci sequence in Lua. Lua
-- has native support for tail-call elimination which is why `tail_call` and
-- `continuation` run in near constant time. For sufficiently large numbers of n
-- you can start to see linear performace characteristics (particularly for the
-- `continuation` implementation), but ultimately the `tail_call` implementation
-- is an order of magnitude faster than iteration even for values of n as small
-- as 500k.
@SegFaultAX
SegFaultAX / free.py
Created December 17, 2018 08:48
Simple Free Monad [Python]
import dataclasses as dc
import typing as ty
import inspect
import functools
S = ty.TypeVar("S")
A = ty.TypeVar("A")
B = ty.TypeVar("B")
@dc.dataclass(frozen=True)
@SegFaultAX
SegFaultAX / gist:2847710
Created June 1, 2012 00:56
Erlang sorting algorithms
-module(sortlib).
-export([qsort/1, bsort/1, ssort/1, isort/1, msort/2,
msort_lte/1, msort_gte/1]).
-export([test_sort/0, test_sort/1, shuffle/1, split/1, merge/3]).
-import(lists, [reverse/1]).
%% Various sorting algorithms implemented in Erlang
@SegFaultAX
SegFaultAX / jltocsv.py
Last active September 25, 2023 04:00
JSON Lines (.jsonl) to CSV (.csv) [Python]
#!/usr/bin/env python
import csv
import json
import sys
import argparse
import dataclasses as dc
@dc.dataclass(frozen=True)
@SegFaultAX
SegFaultAX / guestlib.py
Created March 14, 2019 19:58
vmGuestLib [Python] [ctypes]
from ctypes import CDLL, c_void_p, byref
from ctypes.util import find_library
# The following code fails on ubuntu 18.04, but succeeds on 14.04
lib = CDLL(find_library('guestlib'))
handle = c_void_p()
ret = lib.VMGuestLib_OpenHandle(byref(handle))
if ret != 0:
raise RuntimeError("failed to get handle")
@SegFaultAX
SegFaultAX / traverse.py
Created February 3, 2017 21:34
Traverse a Python dict and find all key paths
def iter_paths(d):
def iter1(d, path):
paths = []
for k, v in d.items():
if isinstance(v, dict):
paths += iter1(v, path + [k])
paths.append((path + [k], v))
return paths
return iter1(d, [])
@SegFaultAX
SegFaultAX / gist:10941721
Last active May 19, 2023 09:20
clojure.walk in Python
from functools import partial
def identity(e):
return e
def walk(inner, outer, coll):
if isinstance(coll, list):
return outer([inner(e) for e in coll])
elif isinstance(coll, dict):
return outer(dict([inner(e) for e in coll.iteritems()]))
@SegFaultAX
SegFaultAX / gen.go
Created May 14, 2022 03:48
Property-based testing, in Go! [go] [golang]
package main
import (
"fmt"
)
type RNG interface {
Next() (uint64, RNG)
}
@SegFaultAX
SegFaultAX / Function.java
Last active March 23, 2022 05:14
Functional programming Java
package com.segfaultax;
public interface Function<Param, Result> {
public Result apply(Param in);
}
@SegFaultAX
SegFaultAX / coderpad.py
Last active January 2, 2022 11:53
Install any package to coderpad
def main():
# Application code goes here!
pass
PACKAGES = ("praw", )
### IGNORE BELOW THIS LINE ###
def run(cmd, shell=False, silent=False):