Skip to content

Instantly share code, notes, and snippets.

import Foundation
/*
Toying with tools to help read binary formats.
I've seen lots of approaches in swift that create
an intermediate object per-read (usually another NSData)
but even if these are lightweight under the hood,
it seems like overkill. Plus this taught me about <()>
*/
@natecook1000
natecook1000 / Set.swift
Last active March 6, 2020 02:19
Creating a Set Type in Swift
// The MIT License (MIT)
//
// Copyright (c) 2014 Nate Cook
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
@natecook1000
natecook1000 / Explore.swift
Last active December 8, 2015 15:25 — forked from erica/gist:06d7e44f4f834757dc36
Swift: Reflection dump
// Original by Erica Sadun
// Source: http://ericasadun.com/2014/06/24/swift-reflection-dump/
import UIKit
import Foundation
func typestring(x : Any) -> String
{
if let obj = x as? NSObject {
return NSStringFromClass((x as NSObject).dynamicType)
for (key, value) in components {
if (value === f) {
let component = components.removeValueForKey(key)
println(component)
break
}
}
@natecook1000
natecook1000 / VulgarFraction.swift
Created August 28, 2014 03:08
Vulgar Fractions
// (c) 2014 Nate Cook, licensed under the MIT License
/**
Returns a tuple with the closest compound fraction possible with Unicode's built-in "vulgar fractions".
See here: http://www.unicode.org/charts/PDF/U2150.pdf
:param: number The floating point number to convert.
:returns: A tuple (String, Double): the string representation of the closest possible vulgar fraction and the value of that string
@natecook1000
natecook1000 / fibMemoized
Last active August 29, 2015 14:06
Memoized Fibonnaci series
// this version of memoize is from the WWDC 2014 Advanced Swift session
// https://developer.apple.com/videos/wwdc/2014/?id=404
func memoize<T: Hashable, U>(body: (T -> U, T) -> U ) -> (T) -> U {
var memo = [T: U]()
var result: (T -> U)!
result = {
value in
if let cached = memo[value] { return cached }
@natecook1000
natecook1000 / levenshteinDistance.swift
Last active October 2, 2019 04:21
Memoized Levenshtein Distance
// memoized Levenshtein Distance
// description given here: http://programmingpraxis.com/2014/09/12/levenshtein-distance/
// memoize for a two parameter recursive function
func memoize<T1: Hashable, T2: Hashable, U>(_ body: @escaping (@escaping (T1, T2) -> U) -> (T1, T2) -> U) -> ((T1, T2) -> U) {
var memo = [T1: [T2: U]]()
var result: ((T1, T2) -> U)!
result = { value1, value2 in
if let cached = memo[value1]?[value2] { return cached }
let toCache = body(result)(value1, value2)
@natecook1000
natecook1000 / genericVsArray.swift
Created September 17, 2014 19:23
Comparing speed of generic function vs. Slice -> Array conversion
// comparing speed of generic function vs. Slice -> Array conversion
import Foundation
// sum function that takes a Slice<Int>
func sumSlice(numbers: Slice<Int>) -> Int {
return numbers.reduce(0, +)
}
// sum function that takes an Array<Int>
@natecook1000
natecook1000 / toUnicodeScalar.swift
Created September 29, 2014 02:46
toUnicodeScalar() method for Character
extension Character {
func toUnicodeScalar() -> UnicodeScalar? {
let s = String(self)
if distance(s.unicodeScalars.startIndex, s.unicodeScalars.endIndex) > 1 {
return nil
}
return s.unicodeScalars[s.unicodeScalars.startIndex]
}
}
@natecook1000
natecook1000 / dumpmodule.sh
Created October 8, 2014 21:05
Dump the Swift-synthesized headers for a module
#! /bin/sh
# usage: <shellscript> [--osx] typename
if [ "$1" = "--osx" ] ; then
echo ":print_module $2" | xcrun swift -deprecated-integrated-repl
else
sdk_path=$(echo `xcrun --show-sdk-path` | sed 's#MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk#iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk#')
echo ":print_module $1" | xcrun swift -deprecated-integrated-repl -sdk "$sdk_path"
fi