-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathArray.swift
80 lines (68 loc) · 2.07 KB
/
Array.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// Copyright 2025 Readium Foundation. All rights reserved.
// Use of this source code is governed by the BSD-style license
// available in the top-level LICENSE file of the project.
//
import Foundation
public extension Array {
init(builder: (inout Self) -> Void) {
self.init()
builder(&self)
}
/// Creates a new `Array` from the given `elements`, if they are not nil.
init(ofNotNil elements: Element?...) {
self = elements.compactMap { $0 }
}
func first<T>(where transform: (Element) throws -> T?) rethrows -> T? {
for element in self {
if let result = try transform(element) {
return result
}
}
return nil
}
@inlinable mutating func popFirst() -> Element? {
if isEmpty {
return nil
} else {
return removeFirst()
}
}
@inlinable func appending(_ newElement: Element) -> Self {
var array = self
array.append(newElement)
return array
}
}
public extension Array where Element: Equatable {
@inlinable func containsAny(_ elements: Element...) -> Bool {
contains { elements.contains($0) }
}
}
public extension Array where Element: Hashable {
/// Creates a new `Array` after removing all the element duplicates.
func removingDuplicates() -> Array {
var result = Array()
var added = Set<Element>()
for element in self {
if !added.contains(element) {
result.append(element)
added.insert(element)
}
}
return result
}
@inlinable func removing(_ element: Element) -> Self {
var array = self
array.removeAll { other in other == element }
return array
}
@inlinable mutating func remove(_ element: Element) {
removeAll { other in other == element }
}
}
public extension Array where Element: Equatable {
func firstMemberFrom(_ candidates: Element?...) -> Element? {
candidates.compactMap { $0 }.first { contains($0) }
}
}