Skip to content

Instantly share code, notes, and snippets.

View numist's full-sized avatar

Scott Perry numist

View GitHub Profile
@numist
numist / Exponentiation.swift
Last active April 5, 2024 19:28
Python-style exponentiation with the ** operator.
infix operator **: MultiplicationPrecedence
public func **<T: UnsignedInteger>(base: T, exponent: T) -> T {
return pow(base, exponent)
}
/// Implements pow() for integers using exponentiation by squaring
public func pow<T: BinaryInteger, U: UnsignedInteger>(_ base: T, _ exponent: U) -> T {
var result: T = 1
@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 / selector_belongsToProtocol.m
Last active April 26, 2022 07:16
Detect if a selector belongs to a protocol
/*
* Copyright © 2012 Scott Perry (http://numist.net)
*
* Released under the MIT License; its terms are at the end of this file.
*/
#import <objc/runtime.h>
/**
* `selector_belongsToProtocol` solves a common problem in proxy objects for delegates where selectors that are not part of the protocol may be unintentionally forwarded to the actual delegate.
@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 / irsz.js
Created January 29, 2012 06:58
dynamically resize images to fit viewport (intelligently!)
/*
* Copyright © 2012 by Scott Perry
* Released under the MIT License; its terms are at the end of this file.
*
* This file depends on:
* • jQuery (tested against 1.7.1)
* http://jquery.com/
*
* Basic logic of this file:
* + if irsz_auto is true
@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 / UIApplication+DelegateMultiDispatch.h
Created April 5, 2014 07:26
Simple category to UIApplication allowing objects to subscribe to application events via a UIApplicationDelegate-like protocol instead of notifications.
#import <UIKit/UIKit.h>
@protocol NNApplicationSubscriber <NSObject>
@optional
- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)application:(UIApplication *)application didChangeStatusBarFrame:(CGRect)oldStatusBarFrame;
- (void)application:(UIApplication *)application didChangeStatusBarOrientation:(UIInterfaceOrientation)oldStatusBarOrientation;
- (void)applicationDidEnterBackground:(UIApplication *)application;
- (void)applicationDidFinishLaunching:(UIApplication *)application;