[swift-evolution] Proposal: Filter split extension on Sequence to return tuple of sequences that meet criteria and that do not

gadiraju praneeth praneethgadiraju at gmail.com
Thu Jun 9 06:42:12 CDT 2016


Agree with the name. Also, the implementation could be better at Sequence
level rather than specific to Array, just like filter

On Wed, Jun 8, 2016 at 11:54 PM, Thorsten Seitz <tseitz42 at icloud.com> wrote:

> +1 from me for adding this method to the standard library.
> I would prefer the name `partition(by:)` for it, though, under which I
> know it from other languages and which is quite fitting, I think.
>
> -Thorsten
>
> Am 08.06.2016 um 22:59 schrieb gadiraju praneeth via swift-evolution <
> swift-evolution at swift.org>:
>
> I added an extension to do this, something like this:
>
> extension Array {
>
>     func filterSplit(includeElement: (Element) -> Bool) -> ([Element],
> [Element]) {
>
>         var elementsSatisfyingCondition = [Element]()
>
>         var elementsNotSatisfyingCondition = [Element]()
>
>
>
>         self.forEach { element in
>
>             if includeElement(element)
> { elementsSatisfyingCondition.append(element) }
>
>             else { elementsNotSatisfyingCondition.append(element) }
>
>         }
>
>
>
>         return
> (elementsSatisfyingCondition, elementsNotSatisfyingCondition)
>
>     }
>
> }
>
>
> that way you can right away do: values.filterSplit { <yourPredicateBlock> }
>
>
>
> On Wed, Jun 8, 2016 at 3:40 PM, Dave Abrahams via swift-evolution <
> swift-evolution at swift.org> wrote:
>
>>
>> on Wed Jun 08 2016, Dave Abrahams <swift-evolution at swift.org> wrote:
>>
>> > on Wed Jun 08 2016, gadiraju praneeth <swift-evolution at swift.org>
>> wrote:
>> >
>> >> Many times, I came across a scenario where I had to filter an array
>> with a
>> >> condition and filter the same array with opposite of that condition.
>> For
>> >> example:
>> >>
>> >> let values = [2, 4, 3, 5, 6, 9]
>> >>
>> >> let divisibleByTwo = values.filter { $0 % 2 == 0 }
>> >> let notDivisibleByTwo = values.filter { $0 % 2 != 0 }
>> >>
>> >> Is there a way currently where we can filter the array into two arrays
>> >> based on a condition?
>> >
>> > Well, you need a stable partition for this if you care about ordering
>> > (see
>> >
>> https://github.com/apple/swift/blob/master/test/Prototypes/Algorithms.swift#L299
>> )
>> > but then you can do
>> >
>> > var parts = values
>> > let mid = values.stablePartition { $0 % 2 == 0 }
>> > let divisibleByTwo = parts.prefix(upTo: mid)
>> > let notDivisibleByTwo = parts.suffix(from: mid)
>> >
>> > Nate Cook has an enhancement to the result of stablyPartitioned in that
>> > prototype that would let you write:
>> >
>> >   let parts = values.stablyPartitioned { $0 % 2 == 0 }
>> >   let divisibleByTwo = parts.prefix(upTo: parts.partitionPoint)
>> >   let notDivisibleByTwo = parts.suffix(from: parts.partitionPoint)
>>
>> Hmm, come to think of it, Nate, maybe there should also be a more
>> convenient way to get the two partitions from the result.
>>
>> --
>> Dave
>>
>> _______________________________________________
>> swift-evolution mailing list
>> swift-evolution at swift.org
>> https://lists.swift.org/mailman/listinfo/swift-evolution
>>
>
> _______________________________________________
> 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/20160609/64ece822/attachment.html>


More information about the swift-evolution mailing list