Skip to content

Instantly share code, notes, and snippets.

View robtimp's full-sized avatar

Rob Hudson robtimp

  • Apple
  • Seymour, CT
View GitHub Profile
@robtimp
robtimp / CircularEnum.swift
Created June 26, 2019 17:02
Circular enum with example
protocol CircularEnum: CaseIterable where Self.AllCases.Element: Equatable {
var currentIndex: Self.AllCases.Index { get }
func next() -> Self.AllCases.Element
func previous() -> Self.AllCases.Element
}
extension CircularEnum {
var currentIndex: Self.AllCases.Index {
return Self.allCases.firstIndex(of: self)!
}
@robtimp
robtimp / CaseIterableIndex.swift
Last active December 7, 2018 21:10
CaseIterable non-optional index
enum NoteLetter: CaseIterable {
case c
case d
case e
case f
case g
case a
case b
}
@robtimp
robtimp / EnumDictionary.swift
Last active October 25, 2018 17:18
EnumDictionary
struct EnumDictionary<Key: Hashable & CaseIterable, Value> {
private let dictionary: [Key: Value]
init(_ dictionary: [Key: Value]) {
for key in Key.allCases {
assert(dictionary[key] != nil, "All enum cases must be assigned a value.")
}
self.dictionary = dictionary
}
struct Scale {
let notes = ["C", "D", "E", "F", "G", "A", "B"]
}
struct ScaleIterator: IteratorProtocol {
private let scale: Scale
private var index = 0
init(_ scale: Scale) {
self.scale = scale
@robtimp
robtimp / ReimplementHitTest.swift
Created May 4, 2018 22:29
Reimplement UIView.hitTest in Swift
extension UIView {
func reimplementedHitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
guard isUserInteractionEnabled && !isHidden && alpha >= 0.01 else {
return nil
}
if point(inside: point, with: event) {
for subview in subviews {
let convertedPoint = subview.convert(point, from: self)
if let view = subview.reimplementedHitTest(convertedPoint, with: event) {
@robtimp
robtimp / Knapsack.swift
Created April 22, 2018 04:04
Knapsack Problem from Grokking Algorithms
func knapSack(capacity: Int, items: [(weight: Int, value: Int)]) -> Int {
var grid = Array(repeatElement(Array(repeatElement(0, count: capacity)), count: items.count))
for row in 0 ..< grid.count {
for column in 0 ..< capacity {
let item = items[row]
let maxWeight = column + 1
let previousMax = row >= 1 ? grid[row - 1][column] : 0
if item.weight <= maxWeight {
@robtimp
robtimp / ReverseNodesInGroupsOfK.swift
Created March 13, 2018 17:49
Reverse linked list nodes in groups of K length
func reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? {
var count = 0
var current = head
var previous: ListNode?
var newHead: ListNode?
while current != nil {
count += 1
current = current?.next
}
@robtimp
robtimp / ListNode.swift
Last active April 10, 2018 20:39
ListNode with description
public class ListNode: ExpressibleByArrayLiteral {
public var value: Int
public var next: ListNode?
public init(_ value: Int, next: ListNode? = nil) {
self.value = value
self.next = next
}
public required init(arrayLiteral elements: Int...) {
self.value = elements[0]
@robtimp
robtimp / LastDayOfMonth.swift
Last active June 8, 2017 18:56
Last day of the month
// Returns 30 or 31 (except for February)
NSCalendar.currentCalendar().rangeOfUnit(.Day, inUnit: .Month, forDate: NSDate()).length
func ordinal(forNumber: Int) -> String {
let tens = (number / 10) % 10
let ones = number % 10
let suffix: String
switch (tens, ones) {
case (1, _):
suffix = "th"
case (_, 1):