Skip to content

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

Merged
merged 1 commit into from
Mar 3, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Sources/Utility/ArrayExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ extension Array {
var t = [T]()
var u = [U]()
for e in self {
if e is T {
t.append(e as! T)
} else {
u.append(e as! U)
if let e = e as? T {
t.append(e)
} else if let e = e as? U {
u.append(e)
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure - that works!

Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 👍

}
}
return (t, u)
Expand Down