Skip to content

Instantly share code, notes, and snippets.

Avatar

Rob Napier rnapier

View GitHub Profile
@rnapier
rnapier / json.swift
Last active Oct 7, 2021
Generic JSON Decodable
View json.swift
import Foundation
@dynamicMemberLookup
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)"
@jessesquires
jessesquires / generics_playground.swift
Last active Jun 27, 2018
Swift optional generic parameters?
View generics_playground.swift
protocol FactoryAType {
typealias Product
}
protocol FactoryBType {
typealias Product
}
@andymatuschak
andymatuschak / gist:2b311461caf740f5726f
Created Dec 28, 2014
A pragmatic and intentionally non-abstract solution to JSON decoding / initialization that doesn't require learning about five new operators.
View gist:2b311461caf740f5726f
struct User {
let id: Int
let name: String
let email: String?
}
extension User: JSONDecodable {
static func create(id: Int, name: String, email: String?) -> User {
return User(id: id, name: name, email: email)
}
@mattt
mattt / NSDecimalNumber.swift
Last active Apr 27, 2021
NSDecimalNumber Additions for Swift
View NSDecimalNumber.swift
import Foundation
// MARK: - Comparable
extension NSDecimalNumber: Comparable {}
public func ==(lhs: NSDecimalNumber, rhs: NSDecimalNumber) -> Bool {
return lhs.compare(rhs) == .OrderedSame
}
@rnapier
rnapier / result.swift
Last active Aug 29, 2015
The ever-important Result type (without methods)
View result.swift
enum Result<A> {
case Success(Box<A>)
case Failure(NSError)
}
// Due to current swift limitations, we have to include this Box in Result.
// Swift cannot handle an enum with multiple associated data (A, NSError) where one is of unknown size (A)
final class Box<T> {
let unbox: T
init(_ value: T) { self.unbox = value }
@ShamylZakariya
ShamylZakariya / debounce.swift
Created Sep 4, 2014
Simple Swift Debouncer
View debounce.swift
func debounce( delay:NSTimeInterval, #queue:dispatch_queue_t, action: (()->()) ) -> ()->() {
var lastFireTime:dispatch_time_t = 0
let dispatchDelay = Int64(delay * Double(NSEC_PER_SEC))
return {
lastFireTime = dispatch_time(DISPATCH_TIME_NOW,0)
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
@rnapier
rnapier / Curryinate.swift
Last active Aug 29, 2015
Curry and flip with ~~ postfixing. I wonder if this is a good idea…
View Curryinate.swift
// Based on https://gist.github.com/kristopherjohnson/adde22d2c53adfb756a1
// Warning, this is insanely slow to compile in Beta6 (can take several minutes)
// Remove some of the |> lines to speed things up
// For more on |>, see http://undefinedvalue.com/2014/07/13/fs-pipe-forward-operator-swift
infix operator |> { precedence 50 associativity left }
// "x |> f" is equivalent to "f(x)"
public func |> <T,U>(lhs: T, rhs: T -> U) -> U {
return rhs(lhs)
@h12w
h12w / goclean.sh
Last active Aug 24, 2021
golcean.sh does automatic checking on a Go package and its sub-packages.
View goclean.sh
#!/bin/bash
# The script does automatic checking on a Go package and its sub-packages, including:
# 1. gofmt (http://golang.org/cmd/gofmt/)
# 2. goimports (https://github.com/bradfitz/goimports)
# 3. golint (https://github.com/golang/lint)
# 4. go vet (http://golang.org/cmd/vet)
# 5. race detector (http://blog.golang.org/race-detector)
# 6. test coverage (http://blog.golang.org/cover)
set -e
View gist:9176925
static OSStatus
SSLVerifySignedServerKeyExchange(SSLContext *ctx, bool isRsa, SSLBuffer signedParams,
uint8_t *signature, UInt16 signatureLen)
{
OSStatus err;
SSLBuffer hashOut, hashCtx, clientRandom, serverRandom;
uint8_t hashes[SSL_SHA1_DIGEST_LEN + SSL_MD5_DIGEST_LEN];
SSLBuffer signedHashes;
uint8_t *dataToSign;
size_t dataToSignLen;