<div>very often I would wish that sort (+ related functions) could take unaryFun arguments and convert them to binaryFun as follows:</div><div><br></div><div>//pseudocode:<br><div>template unaryToBinaryComp(alias foo) {</div>
<div>bool unaryToBinaryComp(T)(T a, T b) if (__traits(compiles,foo(a) < foo(b)) ) {</div><div>return foo(a) < foo(b);</div><div>}</div><div>}</div><div><br></div><div><br></div></div><div>Using this we could support much nicer syntax for sorting with unary functions, for example:</div>
<div><br></div><div><div>sort!"foo(a)" <=>sort!(unaryToBinaryComp!(unaryFun!"foo(a)")) <=> sort!"foo(a) < foo(a)"</div></div><div>sorting in reverse order is easy: just use sort!"-foo(a)" (works for signed, unsigned, floating point types etc).</div>
<div><br></div><div>Examples of use:</div><div><br></div><div>E1)</div><div><br></div><div>struct A{</div><div>double score;</div><div>int index;</div><div>}</div><div><br></div><div>[A.init,A.init].sort!"a.score";</div>
<div>instead of: </div><div><div>[A.init,A.init].sort!"a.score < b.score";</div></div><div><br></div><div><div>E2)</div></div><div><br></div><div>mytuple.sort!"a[0]"</div><div>instead of: </div><div>
<div>[A.init,A.init].sort!"a[0]<b[0]"</div></div><div><br></div><div>I find that in large majority of cases, binary fun is used in that way. So let's support both unaryFun and binaryFun.</div><div><br></div>