File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -488,6 +488,21 @@ extension Sequence {
488
488
}
489
489
return false
490
490
}
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
+ }
491
506
}
492
507
493
508
extension Sequence where Element : Equatable {
Original file line number Diff line number Diff line change @@ -614,6 +614,20 @@ SequenceTypeTests.test("contains/Predicate") {
614
614
}
615
615
}
616
616
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
+
617
631
//===----------------------------------------------------------------------===//
618
632
// reduce()
619
633
//===----------------------------------------------------------------------===//
You can’t perform that action at this time.
0 commit comments