This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)! | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum NoteLetter: CaseIterable { | |
case c | |
case d | |
case e | |
case f | |
case g | |
case a | |
case b | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Returns 30 or 31 (except for February) | |
NSCalendar.currentCalendar().rangeOfUnit(.Day, inUnit: .Month, forDate: NSDate()).length |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
NewerOlder