Skip to content

Instantly share code, notes, and snippets.

View vdka's full-sized avatar

Ethan Jackwitz vdka

View GitHub Profile
// JSONCore Extension
extension JSON {
func cast<T>() throws -> T {
guard let value: T = value as? T else { throw JSONConvertError.BadCast }
return value
}
/// Returns the content matching the type of its destination
@vdka
vdka / PCG.swift
Created May 18, 2016 00:04
A small PCG RNG in swift
/// Generates 32 Psuedo Random bits in a reproducible way using a (PCG)[http://www.pcg-random.org/] RNG
struct PCGRand32 {
var state: Int64
var inc: Int64
mutating func rand() -> UInt32 {
let oldState = state
//Advance internal state
state = oldState &* 6364136223846793005 &+ (inc | 1)
// Calculate output function (XSH RR), uses old state for max ILP
@vdka
vdka / OrderedDictionary.swift
Created May 26, 2016 03:00
Close to functional
//
// OrderedDictionary.swift
// JSON
//
// Created by Ethan Jackwitz on 5/26/16.
//
public struct OrderedDictionary<Key: Hashable, Value> {
public typealias Element = (Key, Value)
@vdka
vdka / SPMHelpers.swift
Last active June 21, 2016 14:15
Helpers I use to work more easily with the Swift package manager.
import PackageDescription
extension Version {
static let min: Version = Version(0, 0, 0)
static let max: Version = Version(.max, .max, .max)
static let any: Range<Version> = (Version.min..<Version.max)
}
import CSDL2
extension SDL {
public struct Error: ErrorProtocol {
public var message: String
public static var last: Error {
defer { SDL_ClearError() }
cmd: "$(pwd)/build.sh"
name: "Build script"
sh: true,
errorMatch:
- "(?<file>[\\/0-9a-zA-Z\\._]+):(?<line>\\d+):(?<col>\\d+): error: (?<message>.+)"
warningMatch:
- "(?<file>[\\/0-9a-zA-Z\\._]+):(?<line>\\d+):(?<col>\\d+): warning: (?<message>.+)"
targets:
"Run Script":
sh: true

Using Swift.Any can be expensive.

benchmarking the following code

let n = 1_000_000
let intArray = Array(repeating: 1, count: n)
let anyArray = Array(repeating: 1 as Any, count: n)

let intTime = measure {
@vdka
vdka / fish_prompt.fish
Last active August 16, 2016 06:26
My fish prompt
# Simple
# https://github.com/sotayamashita/simple
#
# MIT © Sota Yamashita
function __git_upstream_configured
git rev-parse --abbrev-ref @"{u}" > /dev/null 2>&1
end
function __print_color
@vdka
vdka / example.swift
Created August 18, 2016 13:36
Swift enum with private cases
public struct Key {
private let rawValue: UInt8
private init(_ rawValue: UInt8) {
self.rawValue = rawValue
}
public let somePublicCase = Key(0)
private let somePrivateCase = Key(1)
private let anotherPrivateCase = Key(2)
@vdka
vdka / labels.json
Last active August 23, 2016 23:53
Github Labels file for use with https://github.com/himynameisdave/git-labelmaker
[
{ "name": "bug", "color": "#e34424" },
{ "name": "feature", "color": "#6cc644" },
{ "name": "ready", "color": "#EAC04B" },
{ "name": "in progress", "color": "#56d1f3" },
{ "name": "on hold", "color": "#7d59c3" },
{ "name": "regression", "color": "#fe9a00" },
{ "name": "discussion", "color": "#7d59c3" },
{ "name": "weight: 1", "color": "#ededed" },
{ "name": "weight: 2", "color": "#ededed" },