Instantly share code, notes, and snippets.

• Sort options
Last active Oct 15, 2019
Swift Class Reflection Without Valid Instance
View ClassReflection.swift
 /* -fno-objc-arc CFTypeRef _unsafeCreatePartiallyInitialized(Class c) { Method method = class_getInstanceMethod([NSObject class], @selector(init)); IMP imp = method_getImplementation(method); return ((id (*)(id, SEL))imp)([c alloc], @selector(init)); } void _unsafeDestructPartiallyInitialized(CFTypeRef x) { Method method = class_getInstanceMethod([NSObject class], @selector(dealloc)); IMP imp = method_getImplementation(method);
Created May 10, 2016
Swift Lazy Prime Factorization
View LazyPrimeFactorization.swift
 import Darwin extension Int { // Lazily computes prime factors public var primeFactors: AnySequence { func factor(input: Int) -> (prime: Int, remainder: Int) { let end = Int(sqrt(Float(input))) if end > 2 { for prime in 2...end { if input % prime == 0 {
Last active Oct 4, 2019
Matrices in Swift
View Matrix Examples.swift
 // Numerical matrix examples let x: Matrix = [[10, 9, 8], [3, 2, 1]] let y: Matrix = [[1, 2, 3], [4, 5, 6]] let z: Matrix = [[1, 2], [3, 4], [5, 6]] x + y // [[11, 11, 11], [7, 7, 7]] x * y // [[10, 18, 24], [12, 10, 6]] 2 * x // [[20, 18, 16], [6, 4, 2]] y ** z // [[22, 28], [49, 64]]
Last active Oct 4, 2019
Fractions in Swift
View Fraction.swift
 // Now availbile as a Swift package on GitHub! // https://github.com/jadengeller/fractional public typealias Fraction = Fractional private func gcd(var lhs: Number, var _ rhs: Number) -> Number { while rhs != 0 { (lhs, rhs) = (rhs, lhs % rhs) } return lhs }
Created Apr 2, 2018
Dynamic Type Checking
View checking.py
 #!/usr/bin/python import inspect def typeAsserting(f): signature = inspect.signature(f) def wrapper(*args, **kwargs): bound_args = signature.bind(*args, **kwargs) for name, parameter_value in bound_args.arguments.items(): expected_type = signature.parameters[name].annotation
Created Mar 17, 2015
Swift Atomic Operators
View Swift Atomic Operators.swift
 import Foundation infix operator +=! { associativity right precedence 90 } // Int32 or Int64 infix operator -=! { associativity right precedence 90 } // Int32 or Int64 postfix operator ++! {} // Int32 or Int64 postfix operator --! {} // Int32 or Int64 infix operator |=! { associativity right precedence 90 } // UInt32 infix operator &=! { associativity right precedence 90 } // UInt32
Last active May 21, 2019
Swift Unless/When
View Swift Unless_When.swift
 // Basically, these are if and else statements respectively // without the opposite clause func when(test: @autoclosure () -> Bool, action: () -> ()) { if test() { action() } } func unless(test: @autoclosure () -> Bool, action: () -> ()) { if !test() { action() } }
Last active May 14, 2019
Powerset
View Powerset_flatMap.swift
 extension Array { var powerSet: [[Element]] { guard !isEmpty else { return [[]] } return Array(self[1...]).powerSet.flatMap { [\$0, [self[0]] + \$0] } } } print([1,2,3,4].powerSet) // -> [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3], [4], [1, 4], [2, 4], [1, 2, 4], [3, 4], [1, 3, 4], [2, 3, 4], [1, 2, 3, 4]]
Created Jan 29, 2017
Reusable Identifier Allocator
View Allocator.swift
 public struct Allocator where Resource.Stride: SignedInteger { internal var available: [CountableClosedRange] public init(of available: CountableClosedRange) { self.available = [available] } public mutating func allocate() -> Resource { guard !available.isEmpty else { fatalError("no resources are available")
Last active Jan 1, 2019
A Quick Overview of Unsafe Bit Cast
View Unsafe Bit Cast.swift
 // Let's declare two structs that with different variables and different boolean values: struct A { let x = true } struct B { let y = false }
You can’t perform that action at this time.