Skip to content

Instantly share code, notes, and snippets.

View numist's full-sized avatar

Scott Perry numist

View GitHub Profile
@numist
numist / swift-repl.fish
Created June 9, 2014 15:55
A function for your fish configuration to run the Swift REPL without making Xcode6-beta your default toolchain.
function swift
set developer_dir $DEVELOPER_DIR
set -x DEVELOPER_DIR /Applications/Xcode6-Beta.app/Contents/Developer/
xcrun swift
if echo $developer_dir | grep "/"
set -x DEVELOPER_DIR $developer_dir
else
set -e DEVELOPER_DIR
end
end
@numist
numist / Set.swift
Last active August 29, 2015 14:02
A quick implementation of Sets along with some operators for performing some set arithmetic.
import Foundation
struct SetGenerator<T:Hashable> : Generator {
var backingGenerator: DictionaryGenerator<T, Void>
init(backingGenerator: DictionaryGenerator<T, Void>) {
self.backingGenerator = backingGenerator;
}
@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 / there_i_fixed_it.rb
Created November 13, 2014 17:48
Get the system time zone in Olson format using Ruby
def get_local_timezone_str
# Yes, this is actually a shell script…
olsontz = `if [ -f /etc/timezone ]; then
cat /etc/timezone
elif [ -h /etc/localtime ]; then
readlink /etc/localtime | sed "s/\\/usr\\/share\\/zoneinfo\\///"
else
checksum=\`md5sum /etc/localtime | cut -d' ' -f1\`
find /usr/share/zoneinfo/ -type f -exec md5sum {} \\; | grep "^$checksum" | sed "s/.*\\/usr\\/share\\/zoneinfo\\///" | head -n 1
fi`.chomp
@numist
numist / scheduler.c
Last active August 29, 2015 14:17
Simple work scheduler and test harness for microcontroller projects in C.
#include "scheduler.h"
#include <stddef.h>
#include <stdbool.h>
//
// Compatibility macros
//
// Missing from Arduino toolchain
#ifndef UINT16_MAX
@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
@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 / 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 / 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 / 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),