This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <()> | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for (key, value) in components { | |
if (value === f) { | |
let component = components.removeValueForKey(key) | |
println(component) | |
break | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 } | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 |
OlderNewer