This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: IF FILTER | |
var bookBefore2000 = [Book]() | |
for book in books { | |
if book.year < 2000 { | |
bookBefore2000.append(book) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: HIGHER ORDER FUNCTION | |
let bookBefore2020HOF = books.filter({$0.year < 2000}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct SearchingBook { | |
var byBeforeYear = 0 //0 meaning do not filter on this | |
var byFavorite = FavoriteSearch.all | |
var byAuthorFirstName = "" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum FavoriteSearch { | |
case all | |
case favorite | |
case notFavorite | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func filterBooks(books: [Book], by filters: SearchingBook) -> [Book] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: FILTER FUNCTION | |
func filterBooks(books: [Book], by filters: SearchingBook) -> [Book]{ | |
var bookFiltered = books.filter({ filters.byBeforeYear == 0 || $0.year < filters.byBeforeYear}) | |
//Search by favorite | |
switch filters.byFavorite { | |
case .favorite: | |
bookFiltered.removeAll(where: {!$0.favorite}) | |
case .notFavorite: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bookFiltered = books.filter({ filters.byBeforeYear == 0 || $0.year < filters.byBeforeYear}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
switch filters.byFavorite { | |
case .favorite: | |
bookFiltered.removeAll(where: {!$0.favorite}) | |
case .notFavorite: | |
bookFiltered.removeAll(where: {$0.favorite}) | |
default: | |
break | |
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
OlderNewer