Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's lots of way of doing this without using the <code>if</code> statement, but still resorting to comparing the numbers. That would be simply hiding an implicit <code>if</code> statement behind something else.</p> <p>One alternative to <code>if</code> statements in object-oriented languages is polymorphism. Consider this:</p> <pre><code>sealed trait Bool object Bool { def apply(b: Boolean): Bool = b match { case true /* Implicit if! */ =&gt; True case false =&gt; False object True extends Bool object False extends Bool </code></pre> <p>From a <code>Boolean</code> you can construct a <code>Bool</code> (using an implicit <code>if</code>, granted). You can then add a method to <code>Bool</code>, implementing it differently for each <code>object</code>. This is how <code>Option</code> works, by the way. <code>None</code> has one implementation, and <code>Some</code> has another.</p> <p>There's one way to avoid <em>comparing</em> the numbers, however. If you don't compare, you don't use <code>if</code> either -- not even implicitly. Fun fact: a computer magazine back in the 80s had a contest for sorting algorithms, the winner of which would be the one comparing the fewest time. The winning algorithm sorted entirely without comparing, and then did a dummy compare just to stay within the rules of the contest.</p> <p>The tip here is that you have to know what the range of the numbers is, and that range cannot be too big, or, otherwise, you'll have to do multiple passes to finish the ordering. Also, lists are inefficient to perform the operations.</p> <p>I hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload