[swift-evolution] Prosposal: LazyCollectionType.prefix() to return a lazy collection

Mark Aron Szulyovszky mark.szulyovszky at gmail.com
Sun Feb 28 07:36:15 CST 2016


Hi Dmitri,

Cool, thanks, it's in the making! Sorry I was really busy this week.

Can you tell me why is it required to have different implementation for
Forward and Bidirectional LazyCollectionTypes?

Otherwise it's all clear!

Thanks a lot!

Cheers,

Mark

On Mon, 22 Feb 2016 at 02:55 Dmitri Gribenko <gribozavr at gmail.com> wrote:

> On Sun, Feb 14, 2016 at 4:12 AM, Dmitri Gribenko <gribozavr at gmail.com>
> wrote:
> > On Sun, Feb 14, 2016 at 2:51 AM, Mark Aron Szulyovszky via
> > swift-evolution <swift-evolution at swift.org> wrote:
> >> Problem:
> >> .prefix() works eagerly even on LazyCollectionType. This means access
> is not
> >> O(1).
> >>
> >> Implications:
> >> There's no way to only get the first N elements of a lazy sequence
> without a
> >> nasty for loop. If prefix() used on a lazy sequence, it'll trigger
> >> computation on the whole array, just to return the first N elements.
> >>
> >> Proposal:
> >> It would be more more useful if lazy.<insert methods here>.prefix()
> returned
> >> LazyPrefixCollection<Generator.Element>
> >> instead of
> >> Slice<LazyCustomCollection<Range<Generator.Element>>>.
> >
> > Thanks, Mark!
> >
> > I think this would be a good addition for forward and bidirectional
> > collections.  You'd need two separate implementations for forward and
> > bidirectional wrappers.
> >
> > For random-access collections, the eager implementation is optimal
> > (finding the end index runs in O(1) and Slice<> does not introduce any
> > constant factor compared to non-trivial wrappers), and we should keep
> > that behavior by adding an overload that would return a
> > LazyCollection<Slice<Base>>.
>
> Hi Mark,
>
> I think you can continue with writing a formal proposal in the
> swift-evolution repository, presenting the full API that you are
> proposing.
>
> Dmitri
>
> --
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160228/83559eff/attachment.html>


More information about the swift-evolution mailing list