jlandon / Alexandria

A library of Swift extensions to turbocharge your iOS development.
https://jlandon.github.io/Alexandria
MIT License
65 stars 8 forks source link

Adding next() and previous() functions for Array. #11

Closed hsoi closed 8 years ago

hsoi commented 8 years ago

If there's an easier way, or if I'm overlooking something obvious in the Swift standard library, please enlighten me.

I wrote this because I was implementing a UIPageViewControllerDataSource. I kept my UIViewControllers in an array, and given that data source's API, this was logical.

jlandon commented 8 years ago

I'm good with this functionality, but I'm not sure about the naming. To me, it'd be more clear if the methods were named after and before.

In the example you use, foo.next(1), it's not super clear that next(1) means "get the next element after 1". after(1) is a bit more clear, imo.

Also, GeneratorType already has a next() method, so adding a next() method directly to Array, which contains a generator, could potentially be confusing

hsoi commented 8 years ago

It's funny you mention that.

When I first wrote it I was thinking from the perspective of the argument element: relative to this element, there's a "next" and a "previous".

But then as I was writing the inline docs, look at the word choice I used to explain things: "before" and "after". When I was writing them, that was the natural way to explain it.

So yeah, good suggestion. :+1:

krider2010 commented 8 years ago

:+1: Big fan of the rename.

jlandon commented 8 years ago

Looks good :+1: