Skip to content

Instantly share code, notes, and snippets.

@Palleas
Last active June 16, 2018 06:00
Show Gist options
  • Save Palleas/4dc801a56ededd094b8d956563356edf to your computer and use it in GitHub Desktop.
Save Palleas/4dc801a56ededd094b8d956563356edf to your computer and use it in GitHub Desktop.
// 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) })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment