<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div></div><div>+1 from me for adding this method to the standard library.</div><div>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.</div><div><br></div><div>-Thorsten&nbsp;</div><div><br>Am 08.06.2016 um 22:59 schrieb gadiraju praneeth via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt;:<br><br></div><blockquote type="cite"><div><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">&nbsp; &nbsp; func filterSplit(includeElement: (Element) -&gt; Bool) -&gt; ([Element], [Element]) {</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">&nbsp; &nbsp; &nbsp; &nbsp; var elementsSatisfyingCondition = [Element]()</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">&nbsp; &nbsp; &nbsp; &nbsp; var&nbsp;elementsNotSatisfyingCondition&nbsp;= [Element]()</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo;min-height:15px">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">&nbsp; &nbsp; &nbsp; &nbsp; self.forEach { element in</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if includeElement(element) {&nbsp;elementsSatisfyingCondition.append(element) }</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp;elementsNotSatisfyingCondition.append(element) }</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">&nbsp; &nbsp; &nbsp; &nbsp; }</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo;min-height:15px">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">&nbsp; &nbsp; &nbsp; &nbsp; return (elementsSatisfyingCondition,&nbsp;elementsNotSatisfyingCondition)</p><p style="color:rgb(0,0,0);margin:0px;font-size:13px;line-height:normal;font-family:Menlo">&nbsp; &nbsp; }</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 { &lt;yourPredicateBlock&gt; }</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="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 8, 2016 at 3:40 PM, Dave Abrahams via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
on Wed Jun 08 2016, Dave Abrahams &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
<br>
&gt; on Wed Jun 08 2016, gadiraju praneeth &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br>
&gt;<br>
&gt;&gt; Many times, I came across a scenario where I had to filter an array with a<br>
&gt;&gt; condition and filter the same array with opposite of that condition. For<br>
&gt;&gt; example:<br>
&gt;&gt;<br>
&gt;&gt; let values = [2, 4, 3, 5, 6, 9]<br>
&gt;&gt;<br>
&gt;&gt; let divisibleByTwo = values.filter { $0 % 2 == 0 }<br>
&gt;&gt; let notDivisibleByTwo = values.filter { $0 % 2 != 0 }<br>
&gt;&gt;<br>
&gt;&gt; Is there a way currently where we can filter the array into two arrays<br>
&gt;&gt; based on a condition?<br>
&gt;<br>
&gt; Well, you need a stable partition for this if you care about ordering<br>
&gt; (see<br>
&gt; <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>
&gt; but then you can do<br>
&gt;<br>
&gt; var parts = values<br>
&gt; let mid = values.stablePartition { $0 % 2 == 0 }<br>
&gt; let divisibleByTwo = parts.prefix(upTo: mid)<br>
&gt; let notDivisibleByTwo = parts.suffix(from: mid)<br>
&gt;<br>
&gt; Nate Cook has an enhancement to the result of stablyPartitioned in that<br>
&gt; prototype that would let you write:<br>
&gt;<br>
&gt;&nbsp; &nbsp;let parts = values.stablyPartitioned { $0 % 2 == 0 }<br>
&gt;&nbsp; &nbsp;let divisibleByTwo = parts.prefix(upTo: parts.partitionPoint)<br>
&gt;&nbsp; &nbsp;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 class="HOEnZb"><div class="h5"><br>
--<br>
Dave<br>
<br>
_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org">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></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>