[swift-evolution] [Accepted with modifications] SE-0045: Add scan, prefix(while:), drop(while:), and unfold to the stdlib

Dave Abrahams dabrahams at apple.com
Thu May 26 15:48:54 CDT 2016

on Fri May 06 2016, Kevin Ballard <swift-evolution at swift.org> wrote:

> As I said to Chris in an off-list email, unfold() was actually the
> function that I was the most excited about in the proposal, because it
> allows for a very elegant solution to a variety of looping needs,
> especially now that we've gotten rid of the C-style for loops. One
> example of its use is something like
>   // Iterate over self.view and all its ancestor views.
>   for view in unfold(self.view, applying: { $0.superview }) {
>       // ...
>   }
> In fact, just today I hit this exact issue and wished I had
> unfold(). This same pattern works for any case where an object has a
> property of the same type and you want to walk the entire chain, such
> as UIViewController.presentingViewController, or
> NSError.userInfo[NSUnderlyingErrorKey].
> It also integrates very nicely with the other sequence methods,
> including drop(while:) and prefix(while:). For example:
>   // Iterate over someView and all its ancestors, stopping at
> self.view
>   for view in unfold(someView, applying: { $0.superview
> }).prefix(while: { $0 !== self.view }) {
>       // ...
>   }
> Incidentally, I tried writing up an example of using the stateful
> version to reimplement zip(), but it ended up being a good argument in
> favor of the suggestion someone (maybe you?) had for turning the state
> into an inout parameter.

Thanks for the credit, but I think it goes to Joe Groff.  He's been
suggesting we do the same thing for reduce().


More information about the swift-evolution mailing list