<div dir="ltr">Nate - I suppose that's really the crux of the matter here. I agree a binary predicate solves the problem, but is that the route we want to go? I think it makes sense, but is there a reason we should stick with a unary predicate? For some reason I had it in my mind that there was mention of preferring a unary predicate but now that I'm looking back I can't find anything to that end.<div><br></div><div>I'll work on a binary predicate implementation and see what the group thinks.</div><div><br></div><div>Thanks!</div><div>Jeff</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 16, 2016 at 2:17 AM, Nate Cook 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"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On Mar 15, 2016, at 1:58 PM, Lorenzo Racca via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><div><div style="word-wrap:break-word"><br><div><blockquote type="cite"><div>On Mar 15, 2016, at 6:49 PM, Haravikk <<a href="mailto:swift-evolution@haravikk.me" target="_blank">swift-evolution@haravikk.me</a>> wrote:</div><div><div style="word-wrap:break-word"><div><br></div><div><div><blockquote type="cite"><div>On 15 Mar 2016, at 15:48, Lorenzo Racca <<a href="mailto:lorenzo.racca@live.it" target="_blank">lorenzo.racca@live.it</a>> wrote:</div><div><br>I already knew the impossibility of applying such a predicate as “$0 == 3” and I actually couldn’t quite figure out a solution.</div></blockquote><br></div><div>I thought so, and I don’t think there is a way to do it, my point was really just that your swift doc comments weren’t clear on that point, then I went off at a bit of a tangent ;)</div></div><br></div></div></blockquote>No problem! What I am trying to figure out here is how we should implement the lowerBound and upperBound functions. Should they exactly reflect their C++ counterparts? </div><div>Anyway, it seems all of our implementations have the same problem, that they cannot be univocally called with any predicate whatsoever, (or at least it seemed to me during some tests with the implementations :) ), so I don’t really know how we should act. I am a little blocked.</div><div>Does anyone have ideas on how that could work no matter what predicate is given? Especially, an upperBound() function, which is a little trickier. </div></div></div></blockquote><div><br></div><div>The key is to use a binary predicate (as used in sort and partition) instead of a unary predicate. Then you can use the predicate as is for lowerBound or with the arguments "reversed" for upperBound. The methods would have a similar signature to indexOf—one that just takes a value for comparable collections and one that takes a value and a predicate.</div><div><br></div><div>The binary search method can be implemented by finding the lower bound, which is by definition not less than the given value, then using the same predicate to check whether the value is not less than the lower bound. If neither is less than the other, you've found the value.</div><div><br></div><div>Nate</div><div><br></div><blockquote type="cite"><div><div style="word-wrap:break-word"><div><blockquote type="cite"><blockquote type="cite">On Mar 15, 2016, at 6:07 PM, Jeff Hajewski<b style="font-family:'Helvetica Neue'"> </b> <<a href="mailto:jeff.hajewski@gmail.come" target="_blank">jeff.hajewski@gmail.come</a>> wrote:</blockquote></blockquote><blockquote type="cite"><blockquote type="cite">I suspect there is an easy solution here and I'm just having a mental block...</blockquote></blockquote></div><div><br></div><div>Jeff, I really do feel you, I’m in the same situation! </div><div>I think your solution could be applicable though, just in a little more complicated way than C++ did, which is to extract the complement of the predicate and act differently upon that. </div><div>As of now I don’t have the time to put down some code (time zone sucks) but will try asap.</div><div><br></div><div>Lorenzo</div></div></div></blockquote></div><br></div><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>
<br></blockquote></div><br></div>