[swift-evolution] Feature proposal: Range operator with step
Hans Huck
gutgedacht at gmx.de
Mon Jun 6 13:42:57 CDT 2016
Thorsten Seitz via swift-evolution <swift-evolution at ...> writes:
>
> > Am 03.05.2016 um 13:42 schrieb Hans Huck via swift-evolution
<swift-evolution <at> swift.org>:
> >
> > Thorsten Seitz via swift-evolution <swift-evolution <at> ...> writes:
> >>
> >> -1
> >>
> >> I don't see the need for special syntax where a method can be easily used
> > and is more general.
> >>
> >> -Thorsten
> >>
> >
> > That, dear Thorsten, is a non-argument. Why? Let's see, how about "I don't
> > see the need for a For-loop where a While-loop can be easily used and is
> > more general.“
>
> I meant „more general“ in the sense of allowing reuse (of these methods)
in other contexts and of
> allowing more powerful abstractions (in this case expressed by simple
methods on sequences) to be used easily.
>
Yup. And that's exactly what a While-loop does in comparison to a classic
For-loop: it allows reuse in other and more general contexts, because it's a
superset in terms of functionality. So do we get rid of the latter? Of
course not, because iterating up and down is used so abundantly that it's
well worth a special case syntax.
And the special case of a "step" syntax is justified on the same grounds as
implementing a For-loop as a special case of a While-loop: you technically
don't need it, but it saves you a significant amount of redundant typing and
cognitive load.
> For the same reason I would not rate Swift’s for-loop as more general than
Scala’s for-loop which is
> more akin to Haskell’s monadic do-notation. More basic or more low level,
yes, but not more general.
>
> -Thorsten
>
What may that possibly have got to do with the question at hand? Nobody
wants a "more general" For-loop in Swift, quite the opposite in fact: we've
just removed (due to it being a fully-fledged While-loop instead of just a
subset) the highly general C-style For-loop from the language.
You can have and keep your "powerful abstraction" in the form of "for
(0..<10).striding(by:2)"; all we ask for is to also provide "for i in 0..<10
step 2" and map it to the former internally.
The fashionable propensity towards smothering even the most basic and
iterative tasks with OOP boilerplate is exceedingly annoying. Let's keep
simple things simple, shall we?
-- Hans
> >
> > The "special syntax" summarized below by Vladimir is absolutely justified,
> > because
> >
> > a) Zipf's Law and
>
> > b) forcing people to use convoluted OOP notation for basic, iterative tasks
> > is simply offensive.
>
> >
> > -- Hans
> >
> >
> >>> Am 18.04.2016 um 17:28 schrieb Vladimir.S via swift-evolution
> > <swift-evolution <at> swift.org>:
> >>>
> >>> On 15.04.2016 3:57, Hans Huck via swift-evolution wrote:
> >>>> Anyway, why not just make it .step() then, like in Ruby?
> >>>>
> >>>> Instead of a "by" keyword, I'd be happy with syntactic sugar in the
> > form of
> >>>>
> >>>> for i in p1..<p2 step x
> >>>
> >>> As for 'step' word: It seems like for now IMO this is the best
> > suggestion : very explicit, anyone knows what
> >> "step" means especially in context of loop, clear that "step" belongs to
> > for-in construction(not to
> >> range itself).
> >>>
> >>> for i in 0..<10 step 2 {
> >>> }
> >>>
> >>> for i in 0..<10 step -2 {
> >>> }
> >>>
> >>> for i in 0.1..<10.5 step 0.5 {
> >>> }
> >>>
> >>> all are mapped to needed ranges/intervals under the hood
> >>> Do you want some custom Range-specific methods to provide steps for loop
> > - no problems, use what you need.
> >> But don't force any of us to use (0..<10).striding(by:2) for myriads of
> > simple loops in our code.
> >>>
> >>> I want to see such constructions in our Swift. Who is with me ? :)
> >
More information about the swift-evolution
mailing list