Skip to content

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.


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