I implemented something similar with two filters initially, but because of 2N vs N I changed it<div><span></span><br>On Wednesday, June 8, 2016, Dave Abrahams via swift-evolution <<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
on Wed Jun 08 2016, Nate Cook <natecook-AT-gmail.com> wrote:<br>
<br>
>> On Jun 8, 2016, at 3:40 PM, Dave Abrahams via swift-evolution <<a href="javascript:;" onclick="_e(event, 'cvml', 'swift-evolution@swift.org')">swift-evolution@swift.org</a>> wrote:<br>
>><br>
>><br>
>> on Wed Jun 08 2016, Dave Abrahams <<a href="javascript:;" onclick="_e(event, 'cvml', 'swift-evolution@swift.org')">swift-evolution@swift.org</a>> wrote:<br>
>><br>
><br>
>>> on Wed Jun 08 2016, gadiraju praneeth <<a href="javascript:;" onclick="_e(event, 'cvml', 'swift-evolution@swift.org')">swift-evolution@swift.org</a>> wrote:<br>
>>><br>
>>>> Many times, I came across a scenario where I had to filter an array with a<br>
>>>> condition and filter the same array with opposite of that condition. For<br>
>>>> example:<br>
>>>><br>
>>>> let values = [2, 4, 3, 5, 6, 9]<br>
>>>><br>
>>>> let divisibleByTwo = values.filter { $0 % 2 == 0 }<br>
>>>> let notDivisibleByTwo = values.filter { $0 % 2 != 0 }<br>
>>>><br>
>>>> Is there a way currently where we can filter the array into two arrays<br>
>>>> based on a condition?<br>
>>><br>
>>> Well, you need a stable partition for this if you care about ordering<br>
>>> (see<br>
>>> <a href="https://github.com/apple/swift/blob/master/test/Prototypes/Algorithms.swift#L299" target="_blank">https://github.com/apple/swift/blob/master/test/Prototypes/Algorithms.swift#L299</a>)<br>
>>> but then you can do<br>
>>><br>
>>> var parts = values<br>
>>> let mid = values.stablePartition { $0 % 2 == 0 }<br>
>>> let divisibleByTwo = parts.prefix(upTo: mid)<br>
>>> let notDivisibleByTwo = parts.suffix(from: mid)<br>
>>><br>
>>> Nate Cook has an enhancement to the result of stablyPartitioned in that<br>
>>> prototype that would let you write:<br>
>>><br>
>>> let parts = values.stablyPartitioned { $0 % 2 == 0 }<br>
>>> let divisibleByTwo = parts.prefix(upTo: parts.partitionPoint)<br>
>>> let notDivisibleByTwo = parts.suffix(from: parts.partitionPoint)<br>
><br>
> Mine was for the result of the 'rotated' methods, but should work for<br>
> the partitioning ones, too. It's not as clear to me what the benefit<br>
> of the "lazy" partitioning in that Algorithm.swift is<br>
<br>
Just to pass “laziness” on from the result, so further computations can<br>
also be lazy.<br>
<br>
> —wouldn't it be better to wrap a collection around two lazy filter<br>
> sequences?<br>
<br>
Perhaps something like a<br>
LazyFlatMap<CollectionOfTwo<LazyFilterCollection>> would be better, but<br>
I have the concern that this would evaluate the predicate 2N times for<br>
each traversal.<br>
<br>
> Here is a quick proof of concept of 'divided' and 'partitioned'<br>
> methods:<br>
> <a href="http://swiftlang.ng.bluemix.net/#/repl/57588cbda79b317716f02e04" target="_blank">http://swiftlang.ng.bluemix.net/#/repl/57588cbda79b317716f02e04</a><br>
><br>
>> Hmm, come to think of it, Nate, maybe there should also be a more<br>
>> convenient way to get the two partitions from the result.<br>
><br>
> Definitely!<br>
><br>
> Nate<br>
><br>
>> --<br>
>> Dave<br>
>><br>
>> _______________________________________________<br>
>> swift-evolution mailing list<br>
>> <a href="javascript:;" onclick="_e(event, 'cvml', 'swift-evolution@swift.org')">swift-evolution@swift.org</a><br>
>> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br>
--<br>
Dave<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="javascript:;" onclick="_e(event, 'cvml', 'swift-evolution@swift.org')">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div>