<div style="white-space:pre-wrap">I realize what follows is actually an argument for restricting stride to 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 that just the name itself suggests a certain logic--namely, that we actually skip over, rather than visit and discard, the elements that aren't in the sequence.<br><br>I form this intuition from the ordinary sense of the word "stride"--if my walking gait has a stride size of two feet and there's a puddle less than one foot wide right in front of me, striding by two feet means that my feet stay dry. It doesn't mean I drag one shoe through the puddle and ignore it. Likewise, when I stride from 2 to 10 by 2, I'm adding two at every step, not adding one twice.<br><br>Since an ordinary user of stride doesn't and shouldn't have to inspect the code in the stride iterator, I think it would violate some users' expectations if sequences that are not collections have each element visited regardless of stride size. A user can trivially write a for loop iterating over the sequence itself and discard every not-nth element. We shouldn't offer a stride function that looks more performant but actually isn't.<br></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Apr 11, 2016 at 7:38 PM Dave Abrahams <<a href="mailto:dabrahams@apple.com">dabrahams@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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 <<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 <<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 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 be<br>
>> beneficial to support only forward strides, the same is true of 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 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 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 ranges<br>
>> specifically and that perhaps I’m confusing things, but it seems to me 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 sequences/collections<br>
>> this would take the start or end index and use advanced(by:), though again,<br>
>> I kind of feel like that should be two separate methods as well, but 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 <<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:)` 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 eliminating<br>
>>> the use of a negative stride to indicate direction, this has the 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 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 the<br>
>>> type supports that, which a developer would then need to check. Instead 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 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 method<br>
>>> called instead.<br>
>>><br>
>>><br>
>>> Of course I’d still like to be able to define 6 … 0 or whatever, but this<br>
>>> would at least eliminate what I dislike about using negatives for 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>
</blockquote></div>