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 | |
// Why bytesView rather than just extending NSData directly? | |
// Because this way we can keep our extension internal and not conflict | |
// with someone who imports us and has also extended NSData. | |
// If you're top-level code, you can just hoist everyting up to NSData directly. | |
internal extension NSData { | |
var bytesView: BytesView { return BytesView(self) } | |
} |
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 | |
class Singleton: NSObject { | |
static let sharedInstance = Singleton() | |
@objc dynamic var aProperty = false | |
func updateDoesntWork() { | |
aProperty = !aProperty | |
} |
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
class Fruit {} | |
class Apple: Fruit {} | |
class Orange: Fruit {} | |
class Basket<T> where T: Fruit | |
{ | |
var items: [T] = [] | |
func add(_ item: T) { | |
items.append(item) |
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 java.security.SecureRandom; | |
// Targeting Android SDK 19 | |
// Goal is a (cryptographic) random long between 2^32 ..< 2^62. | |
// Ultimately I want to pick ~10M values with predictible and very low liklihood of collision (~0.001% Birthday Attack). | |
// Approach is to compute a random upper 32 bits between 1..<2^30 and add a lower 32-bits 0..<2^32. | |
// Need to be careful of lower 32 bits being treated as negative, since that would skew the results out of range. | |
public class MyRandom { | |
public static void main(String args[]) { |
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 JSONEncoder { | |
func stringEncode<T>(_ value: T) throws -> String where T : Encodable { | |
guard let string = String(data: try self.encode(value), encoding: .utf8) else { | |
throw EncodingError.invalidValue(value, EncodingError.Context(codingPath: [], debugDescription: "Could not UTF-8 encode string")) | |
} | |
return 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
// With functions/methods you can "launder" your code so it doesn't require self references anymore, | |
// but you wind up with retain loops that are very non-obvious. I'm having trouble finding good coding | |
// styles that reliably avoid this kind of problem. | |
class B { | |
// A retain loop waiting to happen | |
var completionHandler: () -> Void = {} | |
} | |
class C { |
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
let xs = [0,1,2,3,4,5] | |
let double: (Int) -> Int = { $0 * 2 } | |
let isEven: (Int) -> Bool = { $0 % 2 == 0 } | |
// Goal: [0,2,4,6,4,10]. Double all but the last even element. | |
// This impl makes me sad. Lots of state and copies the entire collection twice (once during transforming, and once to unreverse) | |
extension Collection { |
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 Darwin | |
extension Integer { | |
static func makeRandomValue() -> Self { | |
var result: Self = 0 | |
withUnsafeMutablePointer(&result) { resultPtr in | |
arc4random_buf(UnsafeMutablePointer(resultPtr), sizeof(Self.self)) | |
} | |
return result | |
} |
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
// swift -O main.swift | |
import Foundation | |
import Accelerate | |
let N = 100_000_000 | |
let x = (0...N).map { _ in Float(drand48() * 1_000_000) } | |
func test(_ f: () -> [Float], name: 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
// Parsing w/ Result and >>- | |
import Foundation | |
import Result | |
let queryBase = "http://en.wikipedia.org/w/api.php?" | |
+ "action=opensearch&format=json&search=" | |
func mkError(message: String) -> NSError { | |
return NSError(domain: "", code: 0, |