Skip to content

Instantly share code, notes, and snippets.

Jaden Geller JadenGeller

View GitHub Profile
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() }
}
@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
}
View Swift Y Function.swift
// We define a Y function such that we can use recursion within an anonymous closure
// Note that this function Y is not a combinator because it recursively refers to itself
// There's not much reason to have an actual combinator in Swift, so we're not going to
// worry about it, it's just as useful.
/*
* Takes as input a function that takes in a function and returns a function of the same type
* and returns a function of that return type by feeding that function back into itself
* infinite times. Note that we use a closure to guard looping due to applicative order evaluation.
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]]
View AnyEquatable.swift
public struct AnyEquatable: Equatable {
private let value: Any
private let equals: Any -> Bool
public init<E: Equatable>(_ value: E) {
self.value = value
self.equals = { ($0 as? E == value) ?? false }
}
}
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 / Arbitrary Precision and Arbitrary Base Integers Example.swift
Last active Sep 12, 2018
Swift Arbitrary Precision, Arbitrary Base Integers
View Arbitrary Precision and Arbitrary Base Integers Example.swift
// Use an integer literal to instantiate
let a: BigInt = 23559821412349283
// Or use a string literal if you want to start with a number that is greater than IntMax
let b: BigInt = "123456789876543234567876543234567876543"
// Perform arithmetic operations
let c: BigInt = 123456 + 321 // -> 12777
let d = c / 100 // -> 127
let e = d << d // -> 12700000000......(127 zeros)
View Cluster.swift
class Number /* class cluser */ {
class Int8: Number {
var value: Swift.Int8
init(_ value: Swift.Int8) { self.value = value }
}
class Int: Number {
var value: Swift.Int
init(_ value: Swift.Int) { self.value = value }
}
@JadenGeller
JadenGeller / Readline.swift
Created Jan 17, 2016
Swift wrapper around GNU readline
View Readline.swift
// #include <readline/readline.h>
extension String {
private init(pointer: UnsafePointer<Int8>) {
self.init()
var p = pointer
while p.memory != 0 {
append(Character(UnicodeScalar(unsafeBitCast(p.memory, UInt8.self))))
p = p.successor()
}
You can’t perform that action at this time.