Skip to content

Instantly share code, notes, and snippets.

View rnapier's full-sized avatar

Rob Napier rnapier

View GitHub Profile
@rnapier
rnapier / NSData.bytesView.swift
Last active January 30, 2019 00:40
Bytes collection for NSData
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) }
}
import Foundation
class Singleton: NSObject {
static let sharedInstance = Singleton()
@objc dynamic var aProperty = false
func updateDoesntWork() {
aProperty = !aProperty
}
class Fruit {}
class Apple: Fruit {}
class Orange: Fruit {}
class Basket<T> where T: Fruit
{
var items: [T] = []
func add(_ item: T) {
items.append(item)
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[]) {
@rnapier
rnapier / StringEncode.swift
Created July 20, 2018 00:50
JSONEncoder().stringEncode()
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
}
}
@rnapier
rnapier / retain.swift
Last active July 3, 2018 20:55
Laundering retain loops
// 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 {
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 {
@rnapier
rnapier / random.swift
Last active April 13, 2018 01:11
Random numbers in Swift
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
}
@rnapier
rnapier / main.swift
Created March 30, 2018 16:14
vvsqrtf performance tests
// 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) {
@rnapier
rnapier / gist:72f50e8ef1e08ef4aa7c
Last active February 3, 2018 23:22
Comparing Result vs ErrorPointer
// 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,