-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Optionally bind elements before appending to Arrays #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optionally bind elements before appending to Arrays #170
Conversation
This is a bit safer than the force-unwrap solution.
if let e = e as? T { | ||
t.append(e) | ||
} else if let e = e as? U { | ||
u.append(e) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about
if case let e as T = e {
t.append(e)
} else if case let e as U = e {
u.append(e)
}
Does the same thing but avoids the ?
in as
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure - that works!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as for me if case let
is to heavy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, that does not work at all. Still asks to force-downcast. Agree with @kostiakoval.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for me
extension Array {
public func partition<T, U>() -> ([T], [U]) {
var t = [T]()
var u = [U]()
for e in self {
if case let e as T = e {
t.append(e)
} else if case let e as U = e {
u.append(e)
}
}
return (t, u)
}
}
let a = [1, 2, 3, "2", "3"]
let partition: ([Int], [String]) = a.partition()
agreed though that if case let
looks heavier at first but I have grown to like it lol
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't say better or worse just different syntactically I guess?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aciidb0mb3r's solution is syntactic sugar (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/swift/grammar/optional-pattern). Same solution, different way of implementing, which I would deem non-traditional.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both solutions works fine and there is no big difference. if let e = e as? T
solutions is safe, very clear and it's very often used in Swift community.
I feel there is no need for further discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! 👍
…nsions Optionally bind elements before appending to Arrays
Use `set` instead of `unordered_set`
This is a bit safer than the force-unwrap solution.