Instantly share code, notes, and snippets.

• Sort options
Last active Feb 6, 2020
AnyEquatable
View AnyEquatable.swift
 public struct AnyEquatable: Equatable { private let value: Any private let equals: Any -> Bool public init(_ value: E) { self.value = value self.equals = { (\$0 as? E == value) ?? false } } }
Last active Jan 8, 2020
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 Dec 12, 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 }
Last active Oct 30, 2019
Correct-by-composition set operations that cannot branch on ordering
View Set.hs
 {-# LANGUAGE FlexibleInstances, UndecidableInstances, LambdaCase, DeriveFunctor, MonadComprehensions #-} import qualified Prelude import Prelude (Show, Num, Functor, Bool(..), not, Int, Maybe(..), Either(..), Eq, (==), (.), (\$), const, otherwise) import Control.Monad (MonadPlus, guard, when, unless, (>>=), fmap, mzero, mplus, return) import qualified Data.Foldable import Data.Foldable (Foldable) import Data.Maybe (maybe, isJust) import Data.Functor.Foldable (Fix)
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
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() } }
You can’t perform that action at this time.