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
@rnapier
rnapier / TypeCoder.swift
Last active Jun 13, 2019
Decode based on a type field
View TypeCoder.swift
import Foundation
/// Simple example of decoding different values based on a "type" field
let json = Data("""
[{
"type": "user",
"name": "Alice"
},
{
View APIClient.swift
import Foundation
// Provides a nice bare-init for ID types
protocol IDType: Codable, Hashable {
associatedtype Value
var value: Value { get }
init(value: Value)
}
extension IDType {
View RepositoryStorage.swift
// Fully type-erased solution for
// https://stackoverflow.com/questions/55549318/avoiding-type-erasure-when-implementing-the-repository-pattern/55550454
// I don't like type-erasers like this; I believe it can be sliced a better way.
// Compare https://gist.github.com/rnapier/f7f0fa6202b0d6586af188635f54b28b, which I like, but relies on a common
// currency of serializing everything to Data and working with that instead of having storage that can deals with Element
// directly.
// FURTHER THOUGHTS:
//
// This example is interesting because, like so many of these toy projects, its design is completely broken and wouldn't
View DataStore.swift
import Foundation
protocol DataStorage {
subscript(identifier: String) -> Data? { get set }
}
extension UserDefaults: DataStorage {
subscript(identifier: String) -> Data? {
get { return data(forKey: identifier) }
set { set(newValue, forKey: identifier) }
@rnapier
rnapier / main.m
Last active Apr 5, 2019
GCD accessor
View main.m
//
// main.m
// test
//
// Created by Rob Napier on 4/5/19.
//
#import <Foundation/Foundation.h>
@interface MyClass: NSObject
View scp-10791.md

Item #: SCP-10791

Object Class: Safe

Special Containment Procedures: SCP-10791 must be maintained within the BMP (Basic Multilingual Plane) at all times. Removal from the BMP may cause disaggregation of the component parts, increasing nearby values by 2. While this is not considered a memetic hazard, the accounting department has made it clear that they will not accept non-standard operators wandering the halls since Incident ██/██/███ when [REDACTED].

Description: SCP-10791 is the result of unauthorized experimentation with SCP-889, SCP-1313, and three members of the Unicode Technical Committee. The less that is said about that, the better. What happens several miles north of ███████, Montana, USA....sometimes requires containment.

Since ██/██/2002, SCP-10791 has been contained in the ███████ Mathematical Operators block. Its name and appearance change regularly, though always an unlikely combination of symbols. Critic

View httpsleak.go
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
@rnapier
rnapier / doOnce.swift
Last active Mar 21, 2019
A function that only executes once. Good Swift or over-clever?
View doOnce.swift
// Swift3 gets rid of dispatch_once and recommends replacing it with a lazy global.
// That's very straightforward when dispach_once is used to initialize something, but
// isn't an exact match when you want something to execute once, and then become a noop
// in a thread-safe way.
// The following approach seems completely "correct" and I guess actually a bit elegant,
// if by "elegant" you mean "terse and not immediately obvious to the reader, which makes
// you look very clever."
var doOnce: () -> Void = {
@rnapier
rnapier / AnyRealmCollectionBox.swift
Created Feb 19, 2019
Boxing up AnyRealmCollection
View AnyRealmCollectionBox.swift
// https://stackoverflow.com/questions/54764362/swift-array-of-tuples-with-generic-tuple-elements/54768030
struct AnyRealmCollectionBox<Element> {
public func index(after i: Int) -> Int { return i + 1 }
public func index(before i: Int) -> Int { return i - 1 }
private let _realm: () -> Realm?
public var realm: Realm? { return _realm() }
private let _isInvalidated: () -> Bool
@rnapier
rnapier / snakecase.swift
Created Feb 14, 2019
Custom key decoding strategy
View snakecase.swift
import Foundation
let json = Data("""
{
"user_name":"Mark",
"user_info":{
"b_a1234":"value_1",
"c_d5678":"value_2"
}
}
You can’t perform that action at this time.