Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is a generic filter
- typealias Filter<T> = (T) -> Bool
- // This is a simplified entry
- struct Entry {
- let title: String
- }
- // This is a tiny helper function that returns a filter
- // that takes an entry and checks if the title starts with the
- // provided string
- func start(with prefix: String) -> Filter<Entry> {
- return { $0.title.starts(with: prefix) }
- }
- // This is a list of filters I want to apply on a list of entry
- let defaultFilters = [start(with: "Marvel")]
- // This is my list of entries
- let entries = ["Marvel Cinematic Universe", "DC Cinematic Universe", "Some Other Universe"]
- // This is a collection of filters that I want to apply on each entry
- // Ideally, I'd want to make it generic so I can use it with other
- // things than entries.
- // How can I make `Entry` generic?
- extension Array where Element == Filter<Entry> {
- func matches(on entry: Entry) -> Bool {
- return reduce(false, { acc, filter in acc || filter(entry) })
- }
- }
Add Comment
Please, Sign In to add comment