Skip to content

Commit 875d2a1

Browse files
Merge pull request #15120 from airspeedswift/only-yoouuuuuuuuu (#16225)
1 parent 565abe4 commit 875d2a1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

stdlib/public/core/SequenceAlgorithms.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,21 @@ extension Sequence {
488488
}
489489
return false
490490
}
491+
492+
/// Returns a Boolean value indicating whether every element of a sequence
493+
/// satisfies a given predicate.
494+
///
495+
/// - Parameter predicate: A closure that takes an element of the sequence
496+
/// as its argument and returns a Boolean value that indicates whether
497+
/// the passed element satisfies a condition.
498+
/// - Returns: `true` if the sequence contains only elements that satisfy
499+
/// `predicate`; otherwise, `false`.
500+
@inlinable
501+
public func allSatisfy(
502+
_ predicate: (Element) throws -> Bool
503+
) rethrows -> Bool {
504+
return try !contains { try !predicate($0) }
505+
}
491506
}
492507

493508
extension Sequence where Element : Equatable {

validation-test/stdlib/SequenceType.swift.gyb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,20 @@ SequenceTypeTests.test("contains/Predicate") {
614614
}
615615
}
616616

617+
SequenceTypeTests.test("allSatisfy/Predicate") {
618+
for test in findTests {
619+
let s = MinimalSequence<OpaqueValue<Int>>(
620+
elements: test.sequence.map { OpaqueValue($0.value) })
621+
expectEqual(
622+
test.expected == nil,
623+
s.allSatisfy { $0.value != test.element.value },
624+
stackTrace: SourceLocStack().with(test.loc))
625+
expectEqual(
626+
test.expectedLeftoverSequence.map { $0.value }, s.map { $0.value },
627+
stackTrace: SourceLocStack().with(test.loc))
628+
}
629+
}
630+
617631
//===----------------------------------------------------------------------===//
618632
// reduce()
619633
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)