Guest User

Untitled

a guest
Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. // This is a generic filter
  2. typealias Filter<T> = (T) -> Bool
  3.  
  4. // This is a simplified entry
  5. struct Entry {
  6. let title: String
  7. }
  8.  
  9. // This is a tiny helper function that returns a filter
  10. // that takes an entry and checks if the title starts with the
  11. // provided string
  12. func start(with prefix: String) -> Filter<Entry> {
  13. return { $0.title.starts(with: prefix) }
  14. }
  15.  
  16. // This is a list of filters I want to apply on a list of entry
  17. let defaultFilters = [start(with: "Marvel")]
  18.  
  19. // This is my list of entries
  20. let entries = ["Marvel Cinematic Universe", "DC Cinematic Universe", "Some Other Universe"]
  21.  
  22. // This is a collection of filters that I want to apply on each entry
  23. // Ideally, I'd want to make it generic so I can use it with other
  24. // things than entries.
  25. // How can I make `Entry` generic?
  26. extension Array where Element == Filter<Entry> {
  27. func matches(on entry: Entry) -> Bool {
  28. return reduce(false, { acc, filter in acc || filter(entry) })
  29. }
  30. }
Add Comment
Please, Sign In to add comment