This file contains 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
import AVFoundation | |
import Foundation | |
extension FileManager { | |
func removeFileIfNecessary(at url: URL) throws { | |
guard fileExists(atPath: url.path) else { | |
return | |
} | |
do { |
This file contains 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 Collection { | |
func eachConsecutive(_ size: Int) -> Array<SubSequence> { | |
let droppedIndices = indices.dropFirst(size - 1) | |
return zip(indices, droppedIndices) | |
.map { return self[$0...$1] } | |
} | |
} |
This file contains 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
@propertyWrapper | |
struct CopyOnWrite<Object> { | |
var wrappedValue: Object { | |
get { storage.object } | |
set { | |
if !isKnownUniquelyReferenced(&storage) { | |
storage = storage.copy() | |
} | |
storage.object = newValue | |
} |
This file contains 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 Matrix<Element> { | |
private var storage: [[Element]] | |
init(_ storage: [[Element]]) { | |
self.storage = storage | |
} | |
} | |
extension Matrix: Collection { | |
struct Index: Comparable { |
This file contains 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
final class Node<Value>: Sequence { | |
typealias NextNode = Node<Value>? | |
let value: Value | |
var next: NextNode | |
init(value: Value, nextNode: NextNode = nil) { | |
self.value = value | |
self.next = nextNode | |
} |
This file contains 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
import Foundation | |
final class Threadsafe<Value> { | |
init(_ value: Value) { | |
self._value = value | |
} | |
var value: Value { | |
return queue.sync { return _value } | |
} |
This file contains 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
final class Node<T>: Sequence { | |
let value: T | |
var next: Node<T>? | |
init(_ value: T, next: Node<T>?) { | |
self.value = value | |
self.next = next | |
} | |
func makeIterator() -> List<T> { |
This file contains 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 Sequence where Element == Int { | |
func pairsOfSum(k: Int) -> [(Int, Int)] { | |
var cache = [Int:Int]() | |
var result = [(Int, Int)]() | |
for i in self { | |
cache[i] = k - i | |
} | |
for (num, inv) in cache { |
This file contains 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 Sequence where Element: Comparable { | |
func isAlmostIncreasingSequence() -> Bool { | |
var foundMisplacedElement = false | |
for (a, b) in zip(self, self.dropFirst()) where a >= b { | |
guard !foundMisplacedElement else { return false } | |
foundMisplacedElement = true | |
} | |
This file contains 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
@import Foundation; | |
@interface ThreadSafe<Value>: NSProxy | |
- (instancetype)initWithValue:(Value)value; | |
@property (nonatomic, strong, readonly) Value value; | |
@end |
NewerOlder