Skip to content

Commit 9ef531f

Browse files
committed
add suffix while method
1 parent d872b64 commit 9ef531f

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Sources/Algorithms/Suffix.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Algorithms open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
//===----------------------------------------------------------------------===//
13+
// Suffix(while:)
14+
//===----------------------------------------------------------------------===//
15+
16+
extension BidirectionalCollection {
17+
@inlinable
18+
public __consuming func Suffix(while predicate: (Element) throws -> Bool
19+
) rethrows -> [Element] {
20+
var result = ContiguousArray<Element>()
21+
self.reverse()
22+
for element in self {
23+
guard try predicate(element) else {
24+
break
25+
}
26+
result.append(element)
27+
}
28+
result.reverse()
29+
return Array(result)
30+
}
31+
}

0 commit comments

Comments
 (0)