Fair enough. If we go this direction, there's little sense in conforming StrideTo and friends to Collection at the moment, I suppose?<br><div class="gmail_quote"><div dir="ltr">On Mon, Apr 11, 2016 at 9:56 PM Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
on Mon Apr 11 2016, Xiaodi Wu <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
<br>
> I realize what follows is actually an argument for restricting stride to<br>
> collections with randomly accessible elements, and maybe we should:<br>
><br>
> We've touched a little bit on performance, and I think my feeling with stride is<br>
> that just the name itself suggests a certain logic--namely, that we actually<br>
> skip over, rather than visit and discard, the elements that aren't in the<br>
> sequence.<br>
><br>
> I form this intuition from the ordinary sense of the word "stride"--if my<br>
> walking gait has a stride size of two feet and there's a puddle less than one<br>
> foot wide right in front of me, striding by two feet means that my feet stay<br>
> dry. It doesn't mean I drag one shoe through the puddle and ignore it. Likewise,<br>
> when I stride from 2 to 10 by 2, I'm adding two at every step, not adding one<br>
> twice.<br>
><br>
> Since an ordinary user of stride doesn't and shouldn't have to inspect the code<br>
> in the stride iterator, I think it would violate some users' expectations if<br>
> sequences that are not collections have each element visited regardless of<br>
> stride size. A user can trivially write a for loop iterating over the sequence<br>
> itself and discard every not-nth element. We shouldn't offer a stride function<br>
> that looks more performant but actually isn't.<br>
<br>
I don't see how stride neessarily has performance implications any more<br>
than other algorithms we also provide for Sequences, and in this case<br>
there's even less argument for restricting it. Advancing is still a<br>
constant factor of the cost of advancing through the underlying<br>
Sequence. We wouldn't even be able to offer a better big-O performance<br>
bound for restricting it to RandomAccessCollections.<br>
<br>
><br>
> On Mon, Apr 11, 2016 at 7:38 PM Dave Abrahams<br>
> <<a href="mailto:dabrahams@apple.com" target="_blank">dabrahams@apple.com</a>> wrote:<br>
><br>
> on Sun Apr 10 2016, Xiaodi Wu <<a href="http://xiaodi.wu-AT-gmail.com" rel="noreferrer" target="_blank">xiaodi.wu-AT-gmail.com</a>> wrote:<br>
><br>
> > On Sun, Apr 10, 2016 at 3:58 PM, Haravikk<br>
> <<a href="mailto:swift-evolution@haravikk.me" target="_blank">swift-evolution@haravikk.me</a>> wrote:<br>
> >><br>
> >> On 10 Apr 2016, at 14:25, Xiaodi Wu<br>
> <<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>> wrote:<br>
> >><br>
> >> What types do you have in mind that would only support positive<br>
> distances?<br>
> >> All numeric types (yes, even UInt, etc.) have signed distances, which<br>
> ><br>
> >> reflects the basic mathematical abstraction of a number line.<br>
> >><br>
> >><br>
> >> Say you wanted to stride through a singly-linked list, it would actually<br>
> be<br>
> >> beneficial to support only forward strides, the same is true of<br>
> sequences,<br>
> >> as you either may not know what the endpoint is, or would have to step<br>
> >> through the whole sequence to find it (plus buffer every value in order<br>
> to<br>
> >> do-so safely).<br>
> >><br>
> >> A consistent behavior with signed distances is so important that we are<br>
> >> currently struggling with an interesting issue with floating point types,<br>
> >> which is that due to rounding error 10.0 + a - a != 10.0 for some values<br>
> of<br>
> >> a.<br>
> >><br>
> >><br>
> >> While that’s interesting I’m not sure why the sign is important; to me a<br>
> >> stride is a width so it being negative makes no sense. For example, say I<br>
> >> laid an array of Ints, organised into groups of five (and also that I’m<br>
> >> lunatic who won’t use a tuple for this), the stride of this array is 5<br>
> >> whether I’m stepping through it forwards or backwards. Imagine I defined<br>
> >> this like so (more realistically it’d be a struct or a class):<br>
> >><br>
> >> typealias StridedIntegerArray:(stride:Int, array:[Int])<br>
> >><br>
> >> If the stride is set to 5, it’s always 5, the only thing that changes is<br>
> >> whether I want to stride from the start or end of the array, plus I could<br>
> >> things like:<br>
> >><br>
> >> myStridedIntegerArray.prefix(from: 2).striding(forwardBy:<br>
> >> myStridedIntegerArray.stride) // Returns element at index 2, 7, 12, etc.<br>
> ><br>
> > When you have a sequence returning elements at index 12, 7, 2, etc.,<br>
> > wouldn't you call the stride size -5? I would, because 12 + (-5) = 7.<br>
> ><br>
> >><br>
> >><br>
> >> It just occurred to me that perhaps you intended this method only for<br>
> ranges<br>
> >> specifically and that perhaps I’m confusing things, but it seems to me<br>
> like<br>
> >> it should be a method for all sequences (with reverse stride available on<br>
> >> collections with a reverse index type) returning a generator that only<br>
> >> returns (or computes) every Nth element, for generic<br>
> sequences/collections<br>
> >> this would take the start or end index and use advanced(by:), though<br>
> again,<br>
> >> I kind of feel like that should be two separate methods as well, but<br>
> that’s<br>
> >> for another issue I think.<br>
> ><br>
> > I don't think it should be for ranges only, but ranges are the extent<br>
> > of this proposal.<br>
> ><br>
> > That said, my own opinion is that striding should not be available on<br>
> > sequences but on collections only. In their most commonly used form,<br>
> > integer strides take a start and end, and there is a finite number of<br>
> > things to stride over; thus, in my reasoning, strides can be extended<br>
> > to cover anything else that has a known start and end and has a finite<br>
> > number of things, which is guaranteed by conformance to Collection but<br>
> > not to Sequence.<br>
><br>
> I dunno; it seems to me that if someone gives me a Sequence I should be<br>
> able to traverse it, skipping every other element. I don't see why<br>
> “stride” should be inapplicable here.<br>
><br>
> > (At the moment, StrideTo/Through conforms to Sequence and not to<br>
> > Collection, but that is considered something to be fixed and we will<br>
> > see if we can address that as part of this set of stride overhauls.)<br>
> ><br>
> > As I see it, we agree on the problem: the current algorithm cannot<br>
> > accommodate singly linked lists and sequences because those things do<br>
> > not have a known endpoint if you begin an attempt to stride. However,<br>
> > my conclusion is the opposite of yours: namely, that they should not<br>
> > have stride. Maybe they should have something similar, but it<br>
> > shouldn't be stride.<br>
> ><br>
> >><br>
> >> On Sun, Apr 10, 2016 at 12:53 PM Haravikk via swift-evolution<br>
> >> <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
> >>><br>
> >>><br>
> >>> On 10 Apr 2016, at 11:17, Brent Royal-Gordon<br>
> <<a href="mailto:brent@architechies.com" target="_blank">brent@architechies.com</a>><br>
> >>> wrote:<br>
> >>><br>
> >>> Why not just assign it the correct sign during the init function?<br>
> >>> (0 ... 6).striding(by: 2) // [0, 2, 4, 6], end > start, so stride = by<br>
> >>> (6 ... 0).striding(by: 2) // [6, 4, 2, 0], start > end, so stride = -by<br>
> >>><br>
> >>><br>
> >>> One reason not to do it this way is that, if we extend `striding(by:)`<br>
> to<br>
> >>> other collections, they will not be as easy to walk backwards through as<br>
> >>> this. You will have to do something like<br>
> >>> `collection.reversed().striding(by:)` which will be a hassle.<br>
> >>><br>
> >>><br>
> >>> Any thoughts on the alternative I mentioned a little earlier to define<br>
> >>> overloads instead of positive/negative? i.e- you would have two methods,<br>
> >>> .striding(forwardBy:) and .striding(backwardBy:). In addition to<br>
> eliminating<br>
> >>> the use of a negative stride to indicate direction, this has the<br>
> advantage<br>
> >>> that .striding(backwardBy:) can be defined only for types with a<br>
> >>> ReverseIndex or only for collections (as you can stride through a<br>
> sequence,<br>
> >>> but only by going forward).<br>
> >>><br>
> >>> This should also make documentation a bit clearer, otherwise you’ve got<br>
> >>> the caveat that to go backwards requires a negative value, but only if<br>
> the<br>
> >>> type supports that, which a developer would then need to check. Instead<br>
> it<br>
> >>> either has the backwardBy variant or not.<br>
> >>><br>
> >>> I know that advance(by:) supports negative values, but this is actually<br>
> >>> something I wouldn’t mind seeing changed as well, as it has the same<br>
> issues<br>
> >>> (passing a negative value in looks fine until you realise the type is a<br>
> >>> ForwardIndex only). It would also allow us to define Distance types that<br>
> >>> don’t support a direction, since this would be given by the choice of<br>
> method<br>
> >>> called instead.<br>
> >>><br>
> >>><br>
> >>> Of course I’d still like to be able to define 6 … 0 or whatever, but<br>
> this<br>
> >>> would at least eliminate what I dislike about using negatives for<br>
> direction.<br>
> >>> _______________________________________________<br>
> >>> swift-evolution mailing list<br>
> >>> <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
> >>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
> >><br>
> >><br>
><br>
> --<br>
> Dave<br>
><br>
> _______________________________________________<br>
> swift-evolution mailing list<br>
> <a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
--<br>
Dave<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>