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

I’m not a naming guru, but my understanding is that ‘reduce’ was picked because it was term of art (like map), which is what allowed the misuse of a verb.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

-Chris

···

On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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.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.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

It definitely occurred to me that this was kind of just a way to construct a generic iterator. Maybe a new AnyIterator (I believe there is such a thing) constructor?

···

--
Brent Royal-Gordon
Sent from my iPhone

I'd +1 that.

-- E

···

On May 5, 2016, at 2:39 PM, Chris Lattner <clattner@apple.com> wrote:

On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com <mailto:erica@ericasadun.com>> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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.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.

I’m not a naming guru, but my understanding is that ‘reduce’ was picked because it was term of art (like map), which is what allowed the misuse of a verb.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

-Chris

If we are discussing naming changes to reduce, here's my personal opinion:

* When I first encountered it, I understood exactly what it did because I knew that term of art. If it was named sequence, I would have been confused.
* If we are discussing name changes, I'd personally vote to change it to fold. It is the other term of art used for it, and it makes unfold work.

David

···

On 05 May 2016, at 22:39, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift-evolution@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.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.

I’m not a naming guru, but my understanding is that ‘reduce’ was picked because it was term of art (like map), which is what allowed the misuse of a verb.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

It just occurred to me that, if we follow existing naming conventions,
this function would end up returning a value of type SequenceSequence :grin:.

-Kevin

···

On Thu, May 5, 2016, at 01:39 PM, Chris Lattner via swift-evolution wrote:

On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift- >> evolution@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.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.

I’m not a naming guru, but my understanding is that ‘reduce’ was
picked because it was term of art (like map), which is what allowed
the misuse of a verb.

One idea that came out of the core team discussion was something like:

sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

    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 :-)

···

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

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution > <swift-evolution@swift.org> wrote:

* 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@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Dave

Using "where:" has a very high potential for confusion, because "where" makes it sound like it runs the predicate against every single element, whereas "while" makes it clear that it stops evaluating elements once the predicate returns false. Or in other words, `drop(where: predicate)` looks like it should be equivalent to `filter({ !predicate($0) })`.

-Kevin Ballard

···

On Fri, May 6, 2016, at 05:19 PM, Dave Abrahams via swift-evolution wrote:

on Wed May 04 2016, Chris Lattner <swift-evolution@swift.org> wrote:

> Proposal link: https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md
>
> Hello Swift Community,
>
> The review of SE-0045: "Add scan, prefix(while:), drop(while:), and
> unfold to the stdlib" ran from April 28...May 3, 2016. A subset of the
> proposal is *accepted, with modifications* for Swift 3. This proposal
> included four new additions to the standard library:
>
> Sequence.prefix(while:) & Sequence.drop(while:) - These are *accepted* as specified in revision 3 of the proposal.

I know the review is over and all, but…

Chris brought to my attention an idea that I liked, and asked me to post
here about it. Specifically, the argument label “where:” would probably
be better than “while:”. “While” makes it sound to me like it's going
to take a nullary predicate and be much more active. But it's hard to
love

     s.drop(where: { $0 > 1 })
     s.drop { $0 > 1 }

because it makes it sound like a general filtering operation.

Therefore, I'd much rather see

       s.droppingPrefix(where: {$0 > 1})

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.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.

I’m not a naming guru, but my understanding is that ‘reduce’ was picked because it was term of art (like map), which is what allowed the misuse of a verb.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

-Chris

I'd +1 that

+1 from me as well

···

Sent from my iPhone

On May 5, 2016, at 3:40 PM, Erica Sadun via swift-evolution <swift-evolution@swift.org> wrote:

On May 5, 2016, at 2:39 PM, Chris Lattner <clattner@apple.com> wrote:
On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

-- E

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Or new AnySequence constructors? So the sequence can be generated more than
once.

let a = AnySequence(from: 1){ $0 * 2 }.prefix(10)
print(Array(a)) // [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
  
let b = AnySequence(from: 10){ $0 == 0 ? nil : $0 - 1 }
print(Array(b)) // [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Code as implemented in Swift 2.2:

extension AnySequence {
init(from: Element, applying: (Element) -&gt; Element) {
   self.init({ () -&gt; AnyGenerator&lt;Element&gt; in
     var current: Element?
     return AnyGenerator{
       current = current.map(applying) ?? from
       return current
     }
   })
}
  
init(from: Element, applying: (Element) -&gt; Element?) {
   self.init({ () -&gt; AnyGenerator&lt;Element&gt; in
     var current: Element?
     return AnyGenerator{
       current = current.map(applying) ?? from
       return current
     }
   })
}
}

**Patrick Smith**

&gt; One idea that came out of the core team discussion was something like:

&gt;
&gt; sequence(from: 0) { $0 += 42 }
&gt;
&gt; Since it returns a sequence.

It definitely occurred to me that this was kind of just a way to construct a

generic iterator. Maybe a new AnyIterator (I believe there is such a thing)
constructor?

\--

Brent Royal-Gordon

···

On May 6 2016, at 7:27 am, Brent Royal-Gordon via swift-evolution &lt;swift- evolution@swift.org&gt; wrote:
Sent from my iPhone
_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I don't know if it's considered too late at this point to rename 'reduce', but I'll add an enthusiastic +1 to renaming it to 'fold' and adding 'unfold'. 'Fold' is just as obvious a name as 'reduce', IMO (actually I even prefer it). I think changing it now with other source-breaking changes is better than moving forward with 'reduce' and a corresponding function with a confusing name. Fold/unfold would fit in beautifully with the prefix/suffix pairings already in the standard library.

Cole

···

On May 6, 2016, at 1:29 AM, David Hart via swift-evolution <swift-evolution@swift.org> wrote:

If we are discussing naming changes to reduce, here's my personal opinion:

* When I first encountered it, I understood exactly what it did because I knew that term of art. If it was named sequence, I would have been confused.
* If we are discussing name changes, I'd personally vote to change it to fold. It is the other term of art used for it, and it makes unfold work.

David

On 05 May 2016, at 22:39, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift-evolution@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.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.

I’m not a naming guru, but my understanding is that ‘reduce’ was picked because it was term of art (like map), which is what allowed the misuse of a verb.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

If we are discussing naming changes to reduce, here's my personal opinion:

* When I first encountered it, I understood exactly what it did because I knew that term of art. If it was named sequence, I would have been confused.
* If we are discussing name changes, I'd personally vote to change it to fold. It is the other term of art used for it, and it makes unfold work.

I have always preferred fold to reduce myself. If it solves a naming issue it might be worth making this change.

···

Sent from my iPad

On May 6, 2016, at 1:29 AM, David Hart via swift-evolution <swift-evolution@swift.org> wrote:

David

On 05 May 2016, at 22:39, Chris Lattner via swift-evolution <swift-evolution@swift.org> wrote:

On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift-evolution@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.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.

I’m not a naming guru, but my understanding is that ‘reduce’ was picked because it was term of art (like map), which is what allowed the misuse of a verb.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

I would also be happy with ‘fold’/‘unfold’. The term of art argument applies to fold in the same way it does for ‘reduce’.

Otherwise (if we stick with ‘reduce’) I find both ‘induce’ and ‘expand’ to be good names. I can also suggest ‘accumulate’.
In each of these cases I prefer the local consistency of a pair of functions, both being verbs, over the global consistency of a pair of functions, one being a verb and one a noun (for consistency with other functions in the standard library). As such, I like the name ‘iterate’.

Regarding the name ’sequence’: I find that it works well as a name for the two versions that was specified in Revision 3:

func unfold<T>(_ initialElement: T, applying: T -> T) -> UnfoldSequence<T>
func unfold<T, State>(_ initialState: State, applying: State -> (T, State)?) -> UnfoldSequence<T>

corresponding to `iterate` and the list `unfoldr` (also a specialization of `ana` (for anamorphism)):

iterate :: (a -> a) -> a -> [a]
unfoldr :: (b -> Maybe (a,b)) -> b -> [a]

But would like to point out that by putting the return type in the name, it makes it less suitable as a potential general name for unfolding to other data structures (e.g trees).

- David

···

On 06 May 2016, at 09:12, Cole Campbell via swift-evolution <swift-evolution@swift.org> wrote:

I don't know if it's considered too late at this point to rename 'reduce', but I'll add an enthusiastic +1 to renaming it to 'fold' and adding 'unfold'. 'Fold' is just as obvious a name as 'reduce', IMO (actually I even prefer it). I think changing it now with other source-breaking changes is better than moving forward with 'reduce' and a corresponding function with a confusing name. Fold/unfold would fit in beautifully with the prefix/suffix pairings already in the standard library.

Cole

On May 6, 2016, at 1:29 AM, David Hart via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

If we are discussing naming changes to reduce, here's my personal opinion:

* When I first encountered it, I understood exactly what it did because I knew that term of art. If it was named sequence, I would have been confused.
* If we are discussing name changes, I'd personally vote to change it to fold. It is the other term of art used for it, and it makes unfold work.

David

On 05 May 2016, at 22:39, Chris Lattner via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com <mailto:erica@ericasadun.com>> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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.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.

I’m not a naming guru, but my understanding is that ‘reduce’ was picked because it was term of art (like map), which is what allowed the misuse of a verb.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

Why do you think so?

···

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

    One idea that came out of the core team discussion was something like:

    sequence(from: 0) { $0 += 42 }

    Since it returns a sequence.

It just occurred to me that, if we follow existing naming conventions, this
function would end up returning a value of type SequenceSequence :grin:.

--
Dave

If we are discussing naming changes to reduce, here's my personal opinion:

* When I first encountered it, I understood exactly what it did because I knew
that term of art. If it was named sequence, I would have been
confused.

Nobody's proposing to rename reduce to sequence, FWIW.

···

on Thu May 05 2016, David Hart <swift-evolution@swift.org> wrote:

* If we are discussing name changes, I'd personally vote to change it
to fold. It is the other term of art used for it, and it makes unfold
work.

David

On 05 May 2016, at 22:39, Chris Lattner via swift-evolution > <swift-evolution@swift.org> wrote:

        On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com> wrote:

        On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution > <swift-evolution@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.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.

    I’m not a naming guru, but my understanding is that ‘reduce’ was picked
    because it was term of art (like map), which is what allowed the misuse of a
    verb.

    One idea that came out of the core team discussion was something like:

    sequence(from: 0) { $0 += 42 }

    Since it returns a sequence.

    -Chris

    _______________________________________________
    swift-evolution mailing list
    swift-evolution@swift.org
    https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Dave

I don't know if it's considered too late at this point to rename 'reduce', but
I'll add an enthusiastic +1 to renaming it to 'fold' and adding 'unfold'. 'Fold'
is just as obvious a name as 'reduce', IMO (actually I even prefer
it).

It's not, IMO. “Reduce” was chosen deliberately over “fold” because we
think it is more in “common currency” among programmers, in no small
part to the notice that MapReduce has gotten.

···

on Fri May 06 2016, Cole Campbell <swift-evolution@swift.org> wrote:

I think changing it now with other source-breaking changes is better
than moving forward with 'reduce' and a corresponding function with a
confusing name. Fold/unfold would fit in beautifully with the
prefix/suffix pairings already in the standard library.

Cole

On May 6, 2016, at 1:29 AM, David Hart via swift-evolution > <swift-evolution@swift.org> wrote:

    If we are discussing naming changes to reduce, here's my personal opinion:

    * When I first encountered it, I understood exactly what it did because I
    knew that term of art. If it was named sequence, I would have been confused.
    * If we are discussing name changes, I'd personally vote to change it to
    fold. It is the other term of art used for it, and it makes unfold work.

    David

    On 05 May 2016, at 22:39, Chris Lattner via swift-evolution > <swift-evolution@swift.org> wrote:

            On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com> > wrote:

            On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution > <swift-evolution@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.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.

        I’m not a naming guru, but my understanding is that ‘reduce’ was picked
        because it was term of art (like map), which is what allowed the misuse
        of a verb.

        One idea that came out of the core team discussion was something like:

        sequence(from: 0) { $0 += 42 }

        Since it returns a sequence.

        -Chris

        _______________________________________________
        swift-evolution mailing list
        swift-evolution@swift.org
        https://lists.swift.org/mailman/listinfo/swift-evolution

    _______________________________________________
    swift-evolution mailing list
    swift-evolution@swift.org
    https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
Dave

Oops, I read your message too fast and missed the fact that you changed the function name too in the very last line.

If we switched to `droppingPrefix(where:)` then we'd need to change `dropFirst(_:)` as well.

-Kevin

···

On Fri, May 6, 2016, at 05:31 PM, Kevin Ballard wrote:

On Fri, May 6, 2016, at 05:19 PM, Dave Abrahams via swift-evolution wrote:
>
> on Wed May 04 2016, Chris Lattner <swift-evolution@swift.org> wrote:
>
> > Proposal link: https://github.com/apple/swift-evolution/blob/master/proposals/0045-scan-takewhile-dropwhile.md
> >
> > Hello Swift Community,
> >
> > The review of SE-0045: "Add scan, prefix(while:), drop(while:), and
> > unfold to the stdlib" ran from April 28...May 3, 2016. A subset of the
> > proposal is *accepted, with modifications* for Swift 3. This proposal
> > included four new additions to the standard library:
> >
> > Sequence.prefix(while:) & Sequence.drop(while:) - These are *accepted* as specified in revision 3 of the proposal.
>
> I know the review is over and all, but…
>
> Chris brought to my attention an idea that I liked, and asked me to post
> here about it. Specifically, the argument label “where:” would probably
> be better than “while:”. “While” makes it sound to me like it's going
> to take a nullary predicate and be much more active. But it's hard to
> love
>
> s.drop(where: { $0 > 1 })
> s.drop { $0 > 1 }
>
> because it makes it sound like a general filtering operation.
>
> Therefore, I'd much rather see
>
> s.droppingPrefix(where: {$0 > 1})

Using "where:" has a very high potential for confusion, because "where" makes it sound like it runs the predicate against every single element, whereas "while" makes it clear that it stops evaluating elements once the predicate returns false. Or in other words, `drop(where: predicate)` looks like it should be equivalent to `filter({ !predicate($0) })`.

Chris, as many people seem to be currently in favour of renaming reduce to fold to support unfold, should be write a proposal, or can the modification be accepted with less formality?

···

On 06 May 2016, at 15:11, Matthew Johnson <matthew@anandabits.com> wrote:

Sent from my iPad

On May 6, 2016, at 1:29 AM, David Hart via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

If we are discussing naming changes to reduce, here's my personal opinion:

* When I first encountered it, I understood exactly what it did because I knew that term of art. If it was named sequence, I would have been confused.
* If we are discussing name changes, I'd personally vote to change it to fold. It is the other term of art used for it, and it makes unfold work.

I have always preferred fold to reduce myself. If it solves a naming issue it might be worth making this change.

David

On 05 May 2016, at 22:39, Chris Lattner via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@swift.org>> wrote:

On May 5, 2016, at 1:03 PM, Erica Sadun <erica@ericasadun.com <mailto:erica@ericasadun.com>> wrote:

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution <swift-evolution@swift.org <mailto:swift-evolution@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.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.

I’m not a naming guru, but my understanding is that ‘reduce’ was picked because it was term of art (like map), which is what allowed the misuse of a verb.

One idea that came out of the core team discussion was something like:

   sequence(from: 0) { $0 += 42 }

Since it returns a sequence.

-Chris

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org <mailto:swift-evolution@swift.org>
https://lists.swift.org/mailman/listinfo/swift-evolution

   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

I'm sure that Kevin or I or some other proposalista could put together a quick fast-track rename request for

* suffix(n) -> Self.SubSequence // requires `n >= 0`, `n <= count`
* suffix(maxLength: n) -> Self.SubSequence // requires `n >= 0`
* suffix(while: predicate) -> Self.SubSequence
* prefix(n) -> Self.SubSequence
* prefix(maxLength: n) -> Self.SubSequence
* prefix(while: predicate) -> Self.SubSequence

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 :-)

I knew you'd point that out. It's not a noun when it's wearing its "I AM VERBING" t-shirt.

That aside, the prevailing sentiment is to rename reduce to `fold` and partner it with `unfold`.
(I personally prefer Chris's `sequence` because I think it better reflects how more Swift users will
use it (never for;;get; never sur;;render). But I recognize that terms of art have a strength of
momentum and familiarity.)

-- E

···

On May 6, 2016, at 6:27 PM, Dave Abrahams via swift-evolution <swift-evolution@swift.org> wrote:

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

On May 4, 2016, at 5:50 PM, Chris Lattner via swift-evolution >> <swift-evolution@swift.org> wrote:

A random sampling of functions that generate non-Array sequences and their corresponding sequence type:

* Sequence.flatten() -> FlattenSequence
* Sequence.joined() -> JoinedSequence
* LazySequenceProtocol.map() -> LazyMapSequence
* LazySequenceProtocol.filter() -> LazyFilterSequence

-Kevin Ballard

···

On Fri, May 6, 2016, at 02:23 PM, Dave Abrahams via swift-evolution wrote:

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

> One idea that came out of the core team discussion was something like:
>
> sequence(from: 0) { $0 += 42 }
>
> Since it returns a sequence.
>
> It just occurred to me that, if we follow existing naming conventions, this
> function would end up returning a value of type SequenceSequence :grin:.

Why do you think so?

We would *definitely* have to have a full review period to rename “reduce”. We specifically considered whether to go with “reduce” or “fold”, and chose reduce.

-Chris

···

On May 6, 2016, at 8:54 AM, David Hart <david@hartbit.com> wrote:

Chris, as many people seem to be currently in favour of renaming reduce to fold to support unfold, should be write a proposal, or can the modification be accepted with less formality?