Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code does the following things:</p> <p>It set a constant named <code>DEFAULT_PRIOR</code> (java equivalent would be a static final variable) to an array containing the number 2 five times. In java:</p> <pre><code>static final int[] DEFAULT_PRIOR = {2,2,2,2,2}; </code></pre> <p>It defines a two-argument method named score where the second argument defaults to <code>DEFAULT_PRIOR</code>. In java this can be achieved with overloading:</p> <pre><code>float score(int[] votes) { return score(votes, DEFAULT_PRIOR); } </code></pre> <p>Inside the score method, it does the following:</p> <pre><code>posterior = votes.zip(prior).map { |a, b| a + b } </code></pre> <p>This creates an array posterior, where each element is the sum of the corresponding element in votes and the corresponding element in prior (i.e. <code>posterior[i] = votes[i] + prior[i]</code>).</p> <pre><code>sum = posterior.inject { |a, b| a + b } </code></pre> <p>This sets sum to be the sum of all elements in <code>posterior</code>.</p> <pre><code>posterior. map.with_index { |v, i| (i + 1) * v }. inject { |a, b| a + b }. to_f / sum </code></pre> <p>This bit multiplies each element in posterior with its index plus one and then sums that. The result is converted to a float and then divided by <code>sum</code>.</p> <p>In java you would iterate over posterior with <code>for</code>-loop (not foreach) and in each iteration add <code>(i + 1) * posterior[i]</code> (where <code>i</code> is the index of the <code>for</code>-loop) to a variable <code>tmp</code>, which you set to 0 before the loop. Then you would cast <code>tmp</code> to <code>float</code> and divide it by <code>sum</code>.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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