[swift-evolution] [Pitch] Unify joined(separator:) and flatten()

Jacob Bandes-Storch jtbandes at gmail.com
Sun Jul 24 19:47:44 CDT 2016


The thing is, joined also works on arrays-of-arrays today. The only
difference is that flatten doesn't have a separator.

We wouldn't lose what your example shows — you could do this:

[["hey"], ["what"]].joined().joined(separator: “")
On Sun, Jul 24, 2016 at 5:45 PM Jose Cheyo Jimenez <cheyo at masters3d.com>
wrote:

> Here is a quick example that we would loose.
>
> [["hey"], ["what"]].flatten().joined(separator: “")
>
> [["hey"], ["what"]].flatten() //  ["hey", "what”]
>
> The way I think of it is flatten works on array of arrays while joined
> works on arrays of strings.
>
> I guess we could do this too
>
> [["hey"], ["what"]].joined(separator: []).joined(separator: "")
>
>
>
> On Jul 24, 2016, at 5:29 PM, Jose Cheyo Jimenez <cheyo at masters3d.com>
> wrote:
>
> -1 for this. To me there needs to be a difference between String (which is
> not a normal collection) and other regular collections.
>
> In addition, I really don’t think this proposal has the needed strong
> support for the change.
>
>
>
>
> On Jul 22, 2016, at 3:41 PM, Jacob Bandes-Storch via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> Here's a proposal draft. Comments welcome:
>
> https://gist.github.com/jtbandes/7978dc1848f7c37eeaa8e9aba27c7325
>
> On Fri, Jul 22, 2016 at 2:51 PM, Ben Rimmington <me at benrimmington.com>
> wrote:
>
>>
>> > On 22 Jul 2016, at 20:43, Jacob Bandes-Storch <jtbandes at gmail.com>
>> wrote:
>> >
>> >> On Fri, Jul 22, 2016 at 8:35 AM, Ben Rimmington <me at benrimmington.com>
>> wrote:
>> >>
>> >>> On 22 Jul 2016, at 02:46, Jacob Bandes-Storch wrote:
>> >>>
>> >>> In the swift-lang Slack channel, a few of us were discussing
>> joined(separator:) and realized that flatten() does almost exactly the same
>> thing.
>> >>>
>> >>> Is there interest in renaming flatten() to joined()?  Since joined
>> takes a separator that's any Sequence, we can't have a default value for
>> the separator parameter, but we can have a variant of joined() with no
>> arguments.
>> >>
>> >> I'd like default separators for the joined() methods.
>> >>
>> >> <https://bugs.swift.org/browse/SR-1428>
>> >>
>> >> But renaming flatten() to joined() seems complicated.
>> >>
>> >> <
>> https://github.com/apple/swift/blob/master/stdlib/public/core/Flatten.swift.gyb
>> >
>> >> <
>> https://github.com/apple/swift/blob/master/stdlib/public/core/Join.swift>
>> >
>> > What makes it seem complicated? At the very least, one could just
>> rename the flatten() function. There might also be an opportunity to
>> combine the two files and delete some code from stdlib.
>>
>> There's only one joined() method (for a sequence of sequences):
>>
>>         extension Sequence {
>>           func joined<Separator: Sequence>(separator: Separator) ->
>> JoinedSequence<Self>
>>         }
>>
>> There are many flatten() methods (`where` clauses omitted for brevity):
>>
>>         extension Sequence {
>>           func flatten() -> FlattenSequence<Self>
>>         }
>>
>>         extension LazySequenceProtocol {
>>           func flatten() -> LazySequence<FlattenSequence<Self.Elements>>
>>         }
>>
>>         extension LazyCollectionProtocol {
>>           func flatten() ->
>> LazyCollection<FlattenCollection<Self.Elements>>
>>         }
>>
>>         extension Collection {
>>           func flatten() -> FlattenCollection<Self>
>>         }
>>
>>         extension BidirectionalCollection {
>>           func flatten() -> FlattenBidirectionalCollection<Self>
>>         }
>>
>> So it's not a simple one-to-one rename.
>>
>> When there's no `separator` argument, will FlattenIterator perform better
>> than JoinedIterator?
>>
>> >> And what would happen to the flatMap() methods? Is flatten() a term of
>> art?
>> >>
>> >> <
>> https://github.com/apple/swift/blob/master/stdlib/public/core/FlatMap.swift
>> >
>> >
>> > I'd say flatMap is more a "term of art" than flatten. "flatten" just
>> describes literally what is being done. Frankly I'm surprised it was never
>> named flattened(). Anyway, flatMap should stay.
>>
>> ## Future directions
>>
>> Will the flatMap(_:) methods also have flatMap(separator:_:) variants?
>>
>
> That's an interesting idea. It seems to be purely additive, however, so I
> imagine it wouldn't happen until after Swift 3.
>
>
>> -- Ben
>>
>>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160725/35f3b097/attachment.html>


More information about the swift-evolution mailing list