Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Sources/FoundationEssentials/Predicate/Predicate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,52 @@ extension Predicate {
}
}

@available(FoundationPreview 6.4, *)
extension Predicate {
public init(all subpredicates: some BidirectionalCollection<Self>) {
var iterator = subpredicates.reversed().makeIterator()

if let guarded = iterator.next() {
self.init({ (input: repeat PredicateExpressions.Variable<each Input>) in
var pieces: any StandardPredicateExpression<Bool> = PredicateExpressions.build_evaluate(PredicateExpressions.build_Arg(guarded), repeat each input)

while let next = iterator.next() {
pieces = Self.meet(PredicateExpressions.build_evaluate(PredicateExpressions.build_Arg(next), repeat each input), pieces)
}

return pieces
})
} else {
self.init(value: true)
}
}

public init(any subpredicates: some BidirectionalCollection<Self>) {
var iterator = subpredicates.reversed().makeIterator()

if let guarded = iterator.next() {
self.init({ (input: repeat PredicateExpressions.Variable<each Input>) in
var pieces: any StandardPredicateExpression<Bool> = PredicateExpressions.build_evaluate(PredicateExpressions.build_Arg(guarded), repeat each input)

while let next = iterator.next() {
pieces = Self.join(PredicateExpressions.build_evaluate(PredicateExpressions.build_Arg(next), repeat each input), pieces)
}

return pieces
})
} else {
self.init(value: false)
}
}

fileprivate static func meet<T: StandardPredicateExpression<Bool>, U: StandardPredicateExpression<Bool>>(_ lhs: T, _ rhs: U) -> any StandardPredicateExpression<Bool> {
PredicateExpressions.build_Conjunction(lhs: lhs, rhs: rhs)
}

fileprivate static func join<T: StandardPredicateExpression<Bool>, U: StandardPredicateExpression<Bool>>(_ lhs: T, _ rhs: U) -> any StandardPredicateExpression<Bool> {
PredicateExpressions.build_Disjunction(lhs: lhs, rhs: rhs)
}
}

// Namespace for operator expressions
@available(macOS 14, iOS 17, tvOS 17, watchOS 10, *)
Expand Down
38 changes: 38 additions & 0 deletions Tests/FoundationEssentialsTests/PredicateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,42 @@ private struct PredicateTests {
#expect(try expression.evaluate(i) == i + 1)
}
}

@Test func all() throws {
#expect(try Predicate(all: []).evaluate(42))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually a bit surprised this compiles without telling it what the Input type of the predicate is here. Does it infer an input of type Int from the call to evaluate using the type of 42?

#expect(try !Predicate(all: [Predicate.true, Predicate.false]).evaluate(42))
let input = Object(a: 6, b: "xyz", c: 0.27, d: 97, e: "u", f: false, g: [7, 42, 1, 0, 25])
var predicates: [Predicate<Object>] = [
#Predicate { $0.a == 6 },
#Predicate { $0.b == "xyz" },
#Predicate { $0.c < 0.3 },
#Predicate { $0.d == 97 },
#Predicate { $0.d % 2 != 0 },
#Predicate { !$0.f },
#Predicate { $0.g.contains(42) },
#Predicate { $0.g.min() == 0 }
]
#expect(try Predicate(all: predicates).evaluate(input))
predicates.append(#Predicate { $0.g.max() == 7 })
#expect(try !Predicate(all: predicates).evaluate(input))
predicates.append(#Predicate { $0.c < 0.0 })
#expect(try !Predicate(all: predicates).evaluate(input))
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add tests that validate the short circuiting behavior to validate that predicates later in the list aren't evaluated if a prior expression fails? This should be possible with a custom object and a property (referenced in the predicate via a keypath) that records when the property is accessed.


@Test func any() throws {
#expect(try !Predicate(any: []).evaluate(42))
#expect(try Predicate(any: [Predicate.true, Predicate.false]).evaluate(42))
let input = Object(a: 6, b: "xyz", c: 0.27, d: 97, e: "u", f: false, g: [7, 42, 1, 0, 25])
let predicates: [Predicate<Object>] = [
#Predicate { $0.g.max() == 7 },
#Predicate { $0.c < 0.0 },
#Predicate { $0.a == 6 },
#Predicate { $0.b == "xyz" },
#Predicate { $0.c < 0.3 }
]
#expect(try Predicate(any: predicates.dropLast()).evaluate(input))
#expect(try Predicate(any: predicates.dropLast(2)).evaluate(input))
#expect(try !Predicate(any: predicates.dropLast(3)).evaluate(input))
#expect(try !Predicate(any: predicates.dropLast(4)).evaluate(input))
}
}