Skip to content

Instantly share code, notes, and snippets.

Jaden Geller JadenGeller

Block or report user

Report or block JadenGeller

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
@JadenGeller
JadenGeller / ClassReflection.swift
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);
@JadenGeller
JadenGeller / LazyPrimeFactorization.swift
Created May 10, 2016
Swift Lazy Prime Factorization
View LazyPrimeFactorization.swift
import Darwin
extension Int {
// Lazily computes prime factors
public var primeFactors: AnySequence<Int> {
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 {
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]]
@JadenGeller
JadenGeller / Fraction.swift
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<Int>
private func gcd<Number: IntegerType>(var lhs: Number, var _ rhs: Number) -> Number {
while rhs != 0 { (lhs, rhs) = (rhs, lhs % rhs) }
return lhs
}
@JadenGeller
JadenGeller / checking.py
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
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
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() }
}
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]]
@JadenGeller
JadenGeller / Allocator.swift
Created Jan 29, 2017
Reusable Identifier Allocator
View Allocator.swift
public struct Allocator<Resource: Strideable & Comparable> where Resource.Stride: SignedInteger {
internal var available: [CountableClosedRange<Resource>]
public init(of available: CountableClosedRange<Resource>) {
self.available = [available]
}
public mutating func allocate() -> Resource {
guard !available.isEmpty else {
fatalError("no resources are available")
@JadenGeller
JadenGeller / Unsafe Bit Cast.swift
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.