Skip to content

switch from local removeFirst copy to iteration for pairing #452

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 12 commits into from
Jun 5, 2017

Conversation

TheIronBorn
Copy link
Contributor

@TheIronBorn TheIronBorn commented Apr 30, 2017

Checklist

Description

we don't need a mutable copy of `array` to alter it, instead we can just drop the first element
we don't need to alter a mutable copy of `array`, instead we can just drop the first element.
We don't need to alter a local copy of `array` to generate each pair, instead use stride(from:to:by).  In my simple [tests](https://gist.github.com/TheIronBorn/cc0507b36fddad291aa34674eb52ad8f), this can be up to an order of magnitude faster.
Copy link
Member

@kelvinlauKL kelvinlauKL left a comment

Choose a reason for hiding this comment

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

I like the polish you gave to this. I left a suggestion that applies to the many of the functions. Let me know what you think.

@@ -3,13 +3,12 @@
*/

func maximum<T: Comparable>(_ array: [T]) -> T? {
var array = array
guard !array.isEmpty else {
Copy link
Member

Choose a reason for hiding this comment

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

Since we're force unwrapping array.first due to this guard statement's guarantee, let's merge the two:

// guards against the empty array and also extracts the first element of the array
guard let maximum = array.first else { return nil }

@kelvinlauKL kelvinlauKL self-assigned this Jun 4, 2017
@TheIronBorn
Copy link
Contributor Author

Oh yeah that's fine. I'll implement in a minute.

@kelvinlauKL
Copy link
Member

Looks great! Cheers

@kelvinlauKL kelvinlauKL merged commit 17bf20d into kodecocodes:master Jun 5, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants