<div dir="ltr">Its values.filterSplit { $0 % 2 == 0 } in this case</div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 8, 2016 at 3:59 PM, gadiraju praneeth <span dir="ltr"><<a href="mailto:praneethgadiraju@gmail.com" target="_blank">praneethgadiraju@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I added an extension to do this, something like this:<div><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">extension Array {</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> func filterSplit(includeElement: (Element) -> Bool) -> ([Element], [Element]) {</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> var elementsSatisfyingCondition = [Element]()</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> var elementsNotSatisfyingCondition = [Element]()</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo;min-height:15px"> </p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> self.forEach { element in</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> if includeElement(element) { elementsSatisfyingCondition.append(element) }</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> else { elementsNotSatisfyingCondition.append(element) }</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> }</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo;min-height:15px"> </p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> return (elementsSatisfyingCondition, elementsNotSatisfyingCondition)</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"> }</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">}</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><br></p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">that way you can right away do: values.filterSplit { <yourPredicateBlock> }</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo"><br></p></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 8, 2016 at 3:40 PM, Dave Abrahams via swift-evolution <span dir="ltr"><<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span><br>
on Wed Jun 08 2016, Dave Abrahams <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:<br>
<br>
> on Wed Jun 08 2016, gadiraju praneeth <<a href="mailto:swift-evolution@swift.org" target="_blank">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" rel="noreferrer" 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>
</span>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>
<div><div><br>
--<br>
Dave<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>