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

Dave Abrahams dabrahams at apple.com
Fri May 6 19:27:38 CDT 2016


on Thu May 05 2016, Erica Sadun <swift-evolution at swift.org> wrote:

> On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution
> <swift-evolution at swift.org> wrote:
>
>     Proposal link:
>     https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md
>
>     Sequence.prefix(while:) & Sequence.drop(while:) - These are *accepted* as
>     specified in revision 3 of the proposal.
>
> I'm still a little sad we didn't go for `prefix`/`suffix` or `take`/`drop` pairs
> that linguistically matched.

I think building an API family around “prefix” and “suffix” that covers
this and other functionality is a great idea, and would make a great
proposal**

This whole area needs some design love.  Among other things,

     s.prefix(4)

should be

     s.prefix(ofMaxLength: 4)

** though we haven't been able to completely eliminate the moral
equivalent of “drop” from the “dropWhile” function, whatever it's
eventually called, in any of our experiments

> Nonetheless I'm gratified these are hopping into the language. That
> said, I'm going to put on my painters cap to consider selecting some
> exterior latex for the feature I was most looking forward to in this
> proposal:
>
> Core team writes:
>
>     unfold(_:applying:) - This addition is *rejected* by the core team as
>     written, but deserves more discussion in the community, and potentially
>     could be the subject of a future proposal. The core team felt that the
>     utility of this operation is high enough to be worth including in the
>     standard library, but could not find an acceptable name for it. “unfold” is
>     problematic, despite its precedence in other language, because Swift calls
>     the corresponding operation “reduce” and not “fold”. No one could get
>     excited about “unreduce”. “iterate” was also considered, but a noun is more
>     appropriate than an verb in this case. Given the lack of a good name, the
>     core team preferred to reject to let the community discuss it more.
>
> A few thoughts:
>
> * I'm not sure why a noun is more appropriate than a verb. Reduce isn't a noun,
> prefix isn't a noun, drop isn't a noun. 

Can't let that one go by; prefix is definitely a noun :-)

> 
> * Not a fan of unfold or unreduce, either.  * Why not `induce` as a
> counter to `reduce`? (induction/reduction if you want to noun it,
> which I don't)
>
> Stepping back, the definition of `reduce` is:
>
> Returns the result of repeatedly calling `combine` with an accumulated value 
> initialized to `initial` and each element of `self`, in turn
>
> public func reduce<T>(initial: T, @noescape combine: (T, Self.Generator.Element)
> throws -> T) rethrows -> T
>
> e.g. print("Hello".characters.reduce(" ", combine: {$0 + String($1) + " "}))
>
> The definition of whatever *this* is, is more or less:
>
> Returns the lazy sequence of repeatedly calling `generate` with an accumulated
> value
> initialized to `initial`. The sequence terminates when the generation closure
> returns `nil`.
>
> public func ???<T>(initial: T, generate: (T) -> T?) -> Sequence<T>
>
> So why not `induce`? It's got a cute name relationship with `reduce`?
>
> var seq = induce(10, generate: { $0 == 0 ? nil : $0 - 1 })
> print(Array(seq)) // [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>
> seq = induce(1, generate:{ $0 * 2 }).prefix(while: { $0 < 1000 })
> print(Array(seq)) // [2, 4, 8, 16, 32, 64, 128, 256, 512]
>
> -- E
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-- 
Dave



More information about the swift-evolution mailing list