Skip to content

Instantly share code, notes, and snippets.

@mlaster
mlaster / Config.swift
Created March 27, 2019 02:44
Flattened hierarchical config
private protocol MutableConfigProtocol: ConfigProtocol {
var key1: String { get set }
var key2: Int { get set }
}
private protocol ConfigLayerProtocol {
var key1: String? { get }
var key2: Int? { get }
}
let n = 1000.0
let a = (n/2500.0) - 1.0
let b = log10(a)
print(b)
let n = 1000.0
let a = (n/2500.0) - 1.0
let b = log10(a)
print(b)
let n = 1000.0
let a = (n/2500.0) - 1.0
let b = log10(a)
print(b)
import Darwin
let Max = 5000.0
func log(_ x: Double, base: Double) -> Double {
let result = log(x + 1.0) / log(base + 1.0)
print(" log(\(x), base: \(base)) -> \(result)")
return result
}
extension Data {
func encrypt(key: Data) {
let ivData = Data.randomBytes(count: kCCKeySizeAES128)
var tag = Data(count: kCCKeySizeAES128)
var cipherText = Data(count: count)
_ = cipherText.withUnsafeMutableBytes { (cipherPtr) in
_ = tag.withUnsafeMutableBytes { (tagPtr) in
_ = ivData.withUnsafeBytes { (ivPtr) in
_ = key.withUnsafeBytes { (keyPtr) in
func createXPCDictionary(_ inDictionary: [String: Any?]) -> xpc_object_t {
var keyArray: [UnsafePointer<Int8>] = []
var valueArray: [xpc_object_t?] = []
for (key, value) in inDictionary {
keyArray.append(key)
let xo: xpc_object_t? = xpc_string_create(value as! String)
valueArray.append(xo)
}
@mlaster
mlaster / OptionSet.swift
Last active September 22, 2018 00:35
Attempt at enumerable OptionSet
// FIXME: How do a genericize this to work with any UInt* type?
public struct OptionSetIterator<Element: OptionSet>: IteratorProtocol where Element.RawValue == UInt16 {
private let value: Element
public init(element: Element) {
self.value = element
}
private lazy var remainingBits = value.rawValue
private var bitMask: Element.RawValue = 1
@mlaster
mlaster / gist:025d449fe0a5e8b9e9cc
Created March 4, 2016 19:17
Work around bad headers
#pragma clang diagnostic push
#pragma clang diagnostic warning "-Wnewline-eof"
#import <BadFramework/BadHeader.h>
#pragma clang diagnostic pop
protocol BaseCommandProtocol {
func requestComplete()
}
extension BaseCommandProtocol {
func requestComplete() {
// For subclasser overrides
print("You should override this")
}
}