Skip to content

Instantly share code, notes, and snippets.

View numist's full-sized avatar

Scott Perry numist

View GitHub Profile
@numist
numist / README.md
Last active January 8, 2022 18:33
"defer { … }" implementation for Objective-C and C when __has_extension(blocks)

This all started because I was complaining about some uninitialized pointer value causing me grief[^mmap] and someone (explicitly trolling) said they always check pointers using:

int fds[2] = { -1, -1}; 
pipe(fds);
if (write(fds[0], pointer_to_check, sizeof(intptr_t)) == -1) {
    close(fds[0]);
    close(fds[1]);
    return not_valid;
} else {
@numist
numist / FixedWidthInteger.swift
Created September 28, 2021 18:07
Swift Jigs
extension FixedWidthInteger where Self: UnsignedInteger {
// Enumerates `self`'s bits from most significant to least
var bits: some Sequence {
return sequence(state: Self.bitWidth) { i -> Bool? in
guard i > 0 else { return nil }
i -= 1
return (self & (1 << i) != 0)
}
}
}
@numist
numist / layout.kbd.json
Last active January 31, 2021 20:25
Harmonic Isomorphic MIDI Keyboard
[
[{"c":"#cccccc", "a":7, "x":6.0}, {"c":"#9999cc"}, "C₈", {"c":"#cccccc"}, {"c":"#333333","t":"#cccccc"},"C₈#",{"c":"#cccccc","t":"#000000"}, "D₈", {"c":"#333333","t":"#cccccc"},"D₈#",{"c":"#cccccc","t":"#000000"}, "E₈", "F₈", {"c":"#333333","t":"#cccccc"},"F₈#",{"c":"#cccccc","t":"#000000"}, "G₈", {"c":"#333333","t":"#cccccc"},"G₈#",{"c":"#cccccc","t":"#000000"}, "A₈", {"c":"#333333","t":"#cccccc"},"A₈#",{"c":"#cccccc","t":"#000000"}, "B₈"],
[{"c":"#cccccc", "a":7, "x":5.5}, {"c":"#333333","t":"#cccccc"},"G₇#",{"c":"#cccccc","t":"#000000"}, "A₇", {"c":"#333333","t":"#cccccc"},"A₇#",{"c":"#cccccc","t":"#000000"}, "B₇", {"c":"#9999cc"}, "C₈", {"c":"#cccccc"}, {"c":"#333333","t":"#cccccc"},"C₈#",{"c":"#cccccc","t":"#000000"}, "D₈", {"c":"#333333","t":"#cccccc"},"D₈#",{"c":"#cccccc","t":"#000000"}, "E₈", "F₈", {"c":"#333333","t":"#cccccc"},"F₈#",{"c":"#cccccc","t":"#000000"}, "G₈", {"c":"#333333","t":"#cccccc"},"G₈#",{"c":"#cccccc","t":"#000000"}],
[{"c":"#cccccc", "a":7, "x":5.0}, "E₇", "F₇", {"c":"#3333
@numist
numist / WorkQueue-FrontierBiNode.swift
Created April 10, 2020 19:08
Internal type FrontierBiNode from the Club diffing algorithm's work queue
/*
FrontierBiNode is a quadtree node with two additional restrictions:
1) all insertions to the southwest are dropped
2) insertions to the northeast are not allowed
Restriction 2) is satisfied by performing all insertions in descending
order of (x+y), resulting in a binary tree structure (all children lie to
the northwest or southeast) containing only points representing edit paths
that have made novel progress.
*/
@numist
numist / Opto.cpp
Created September 29, 2019 01:17
Digispark firmware providing a digital optical sensor used for controlling timing on model motors
#include "Opto.h"
bool Opto::getState() {
return state;
}
bool Opto::loop(int val) {
bool prev_state = state;
if (val > highest) { highest = val; }
if (val < lowest) { lowest = val; }
@numist
numist / Main.swift
Last active May 27, 2022 06:54
A rough Swift implementation of the Vose alias method
// What is an alias method?
// If we have a list of elements with different frequencies,
// like the letters in the English language…
let frequencies = [
("a", 8.167), ("b", 1.492), ("c", 2.782), ("d", 4.253), ("e", 12.702),
("f", 2.228), ("g", 2.015), ("h", 6.094), ("i", 6.966), ("j", 0.153),
("k", 0.772), ("l", 4.025), ("m", 2.406), ("n", 6.749), ("o", 7.507),
("p", 1.929), ("q", 0.095), ("r", 5.987), ("s", 6.327), ("t", 9.056),
("u", 2.758), ("v", 0.978), ("w", 2.360), ("x", 0.150), ("y", 1.974),
@numist
numist / README.md
Last active August 2, 2017 04:49
Parse input from `ping 8.8.8.8 | ts '%s'` and print outage statistics

Prints outage statistics from ping output

Usage

ping 8.8.8.8 | ts '%s' | ./outageReport.rb

Examples:

Packet loss

@numist
numist / Model-Z70.kbd.json
Last active November 6, 2017 22:58
Model Z70
[
{
"name": "Model Z70",
"switchMount": "cherry",
"plate": true
},
[
"~\n`",
{
"a": 0
@numist
numist / plot.swift
Last active March 14, 2016 14:23
A function that plots (as a string) a function of (Double) -> Double
import Darwin // Provides: sin/cos/tan
// memoize function from https://gist.github.com/berkus/8a9e104f8aac5d025eb5
func memoize<T: Hashable, U>( body: ( (T)->U, T )->U ) -> (T)->U {
var memo = Dictionary<T, U>()
var result: ((T)->U)!
result = { x in
if let q = memo[x] { return q }
let r = body(result, x)
memo[x] = r
@numist
numist / RSSKit.swift
Created July 31, 2015 06:41
Feed and Folder protocols with a couple of concrete implementations in the spirit of http://inessential.com/2015/07/19/secret_projects_diary_2_swift_2_0_prot
func ==<T:Feed>(lhs: T, rhs: T) -> Bool {
return lhs.link == rhs.link
}
protocol Feed : Equatable {
var link : String {get}
}
protocol Folder {
typealias FeedType: Feed