Skip to content

Instantly share code, notes, and snippets.

Rob Napier rnapier

Block or report user

Report or block rnapier

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View MyRandom.java
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[]) {
View TitleDecodable.swift
// From https://stackoverflow.com/questions/54129682/use-swift-codable-to-decode-json-with-values-as-keys
import Foundation
let json = Data("""
{
"7E7-M001" : {
"Drawer1" : {
"101" : {
"Partnumber" : "F101"
},
@rnapier
rnapier / StringEncode.swift
Created Jul 20, 2018
JSONEncoder().stringEncode()
View StringEncode.swift
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 Jul 3, 2018
Laundering retain loops
View retain.swift
// 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 {
@rnapier
rnapier / stream.swift
Last active Jun 22, 2019
Newer stream-based ideas on observables
View stream.swift
/*
Updated ideas on observation. Much more powerful and composeable than previous Observable approach.
Simpler, but less powerful, than RxSwift
*/
import Foundation
public class Disposable {
private var isDisposed = false
private let _dispose: () -> Void
View mapExceptLast.swift
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 / observable.swift
Created May 16, 2018
New Observable idea
View observable.swift
import Foundation
class Disposable {
let dispose: () -> Void
init(dispose: @escaping () -> Void) { self.dispose = dispose }
deinit {
dispose()
}
}
@rnapier
rnapier / main.swift
Created Mar 30, 2018
vvsqrtf performance tests
View main.swift
// 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 / ClassSet.swift
Last active Feb 3, 2018
SetAlgebra for classes
View ClassSet.swift
//
// ClassSet.swift
// audio
//
// Created by Rob Napier on 1/25/18.
// Copyright © 2018 Jaybird LLC. All rights reserved.
//
/// Unordered set of unique class objects. Does not require Equatable or Hashable.
struct ClassSet<Element> where Element: AnyObject {
@rnapier
rnapier / json.swift
Last active Aug 12, 2019
Generic JSON Decodable
View json.swift
import Foundation
enum JSON: Codable, CustomStringConvertible {
var description: String {
switch self {
case .string(let string): return "\"\(string)\""
case .number(let double):
if let int = Int(exactly: double) {
return "\(int)"
} else {
You can’t perform that action at this time.