Improve storage
This commit is contained in:
@ -4,4 +4,11 @@ extension Int {
|
||||
static func random() -> Int {
|
||||
random(in: Int.min...Int.max)
|
||||
}
|
||||
|
||||
mutating func increment(_ increment: Bool) {
|
||||
guard increment else {
|
||||
return
|
||||
}
|
||||
self += 1
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,10 @@ import Foundation
|
||||
|
||||
extension Optional {
|
||||
|
||||
func `default`(_ defaultValue: Wrapped) -> Wrapped {
|
||||
self ?? defaultValue
|
||||
}
|
||||
|
||||
func map<T>(_ transform: (Wrapped) throws -> T?) rethrows -> T? {
|
||||
guard let self else { return nil }
|
||||
return try transform(self)
|
||||
|
@ -8,4 +8,22 @@ extension Collection {
|
||||
}
|
||||
return sorted { conversion($0) < conversion($1) }
|
||||
}
|
||||
|
||||
func count(where predicate: (Element) throws -> Bool) rethrows -> Int {
|
||||
try reduce(0) { count, element in
|
||||
try predicate(element) ? count + 1 : count
|
||||
}
|
||||
}
|
||||
|
||||
func countThrows(where predicate: (Element) throws -> Void) -> Int {
|
||||
reduce(0) { count, element in
|
||||
do {
|
||||
try predicate(element)
|
||||
return count
|
||||
} catch {
|
||||
return count + 1
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -89,6 +89,10 @@ extension String {
|
||||
|
||||
extension String {
|
||||
|
||||
var fileNameWithoutExtension: String {
|
||||
dropAfterLast(".")
|
||||
}
|
||||
|
||||
var fileExtension: String? {
|
||||
let parts = components(separatedBy: ".")
|
||||
guard parts.count > 1 else { return nil }
|
||||
|
Reference in New Issue
Block a user