Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Preamble</strong></p> <p>It occurred to me that the status could be seen as having a priority, and if given a sequence of <code>(agent,status)</code> pairs then the task is to select only the highest priority status for each agent. Unfortunately, status isn't strongly typed with an explicit ordering so defined, but... as it's a string with only two values we can safely use string ordering as having a 1:1 correspondence to the priority.</p> <p>Both my answers take advantage of two useful facts:</p> <ol> <li><p>In natural string ordering, <code>"FAIL" &lt; "PASS"</code>, so:</p> <pre><code>List("PASS", "FAIL", "PASS").sorted.head = "FAIL" </code></pre></li> <li><p>For two tuples <code>(x,a)</code> and <code>(x,b)</code>, <code>(x,a) &gt; (x, b)</code> if <code>(a &gt; b)</code></p></li> </ol> <p><strong>UPDATED REPLY</strong></p> <pre><code>val solution = l.sorted.reverse.toMap </code></pre> <p>When converting a <code>Seq[(A,B)]</code> to a <code>Map[A,B]</code> via the <code>.toMap</code> method, each "key" in the original sequence of tuples can only appear in the resulting Map once. As it happens, the conversion uses the last such occurrence.</p> <pre><code>l.sorted.reverse = List( (Agent 2,PASS), // &lt;-- Last "Agent 2" (Agent 1,FAIL), // &lt;-- Last "Agent 1" (Agent,PASS), (Agent,PASS), (Agent,FAIL)) // &lt;-- Last "Agent" l.sorted.reverse.toMap = Map( Agent 2 -&gt; PASS, Agent 1 -&gt; FAIL, Agent -&gt; FAIL) </code></pre> <p><strong>ORIGINAL REPLY</strong></p> <p>Starting with the answer...</p> <pre><code>val oldSolution = (l groupBy (_._1)) mapValues {_.sorted.head._2} </code></pre> <p>...and then showing my working :)</p> <pre><code>//group l groupBy (_._1) = Map( Agent 2 -&gt; List((Agent 2,PASS)), Agent 1 -&gt; List((Agent 1,FAIL)), Agent -&gt; List((Agent,PASS), (Agent,FAIL), (Agent,PASS)) ) //extract values (l groupBy (_._1)) mapValues {_.map(_._2)} = Map( Agent 2 -&gt; List(PASS), Agent 1 -&gt; List(FAIL), Agent -&gt; List(PASS, FAIL, PASS)) //sort (l groupBy (_._1)) mapValues {_.map(_._2).sorted} = Map( Agent 2 -&gt; List(PASS), Agent 1 -&gt; List(FAIL), Agent -&gt; List(FAIL, PASS, PASS)) //head (l groupBy (_._1)) mapValues {_.map(_._2).sorted.head} = Map( Agent 2 -&gt; PASS, Agent 1 -&gt; FAIL, Agent -&gt; FAIL) </code></pre> <p>However, you can directly sort the <code>agent -&gt; status</code> pairs without needing to first extract <code>_2</code>:</p> <pre><code>//group &amp; sort (l groupBy (_._1)) mapValues {_.sorted} = Map( Agent 2 -&gt; List((Agent 2,PASS)), Agent 1 -&gt; List((Agent 1,FAIL)), Agent -&gt; List((Agent,FAIL), (Agent,PASS), (Agent,PASS))) //extract values (l groupBy (_._1)) mapValues {_.sorted.head._2} = Map( Agent 2 -&gt; PASS, Agent 1 -&gt; FAIL, Agent -&gt; FAIL) </code></pre> <hr> <p>In either case, feel free to convert back to a List of Pairs if you wish:</p> <pre><code>l.sorted.reverse.toMap.toList = List( (Agent 2, PASS), (Agent 1, FAIL), (Agent, FAIL)) </code></pre>
 

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