Created
January 15, 2019 16:42
-
-
Save bjhomer/a3c3e5a7fd935f3b9b6adc62277afc8f to your computer and use it in GitHub Desktop.
Is there a better way to do this?
This file contains 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 Thing { | |
case empty | |
case int(Int) | |
} | |
let things: [Thing] = [.empty, .int(3), .int(7)] | |
// Surely there's a more concise way to write this `filter` closure? | |
let intThings = things.filter({ | |
if case Thing.int = $0 { return true } | |
else { return false } | |
}) |
Not a pretty solution but I’ve done the following before. It tidied up the call site and could arguably be generated
enum Thing {
case empty
case int(Int)
var unassociated: Unassociated {
switch self {
case .empty: return .empty
case int: return .int
}
}
enum Unassociated {
case empty
case int
}
}
It then becomes at the call site
things.filter { $0.unassociated == .int }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Things that don't work: