Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's my take:</p> <ol> <li><p>There are many core clojure functions that take sequences as arguments while others take multiple arguments, so there is no real idiomatic way in my opinion. If you already have your data in a sequence, I would use a seq as argument, since it will save you a call to apply.</p></li> <li><p>I wouldn't write a function that returns a value in some cases and a list of values in others, because the calling code will always have to check the return value before using it. Instead I would return a single mode as a seq with just one item in it. But you may have your reasons, depending on the code that calls this function. </p></li> </ol> <p>Apart from that I would rewrite the mode function like this:</p> <pre><code>(defn mode [aseq] (let [amap (tally-map aseq) mx (apply max (vals amap)) modes (map key (filter #(= mx (val %)) amap)) c (count modes)] (cond (= c 1) (first modes) (= c (count amap)) nil :default modes))) </code></pre> <p>Instead of defining a function f you could use the identity function (unless your data contains values that are logically false). But you don't even need that. I find the modes in a different way, which is more readable to me: The map amap acts as a sequence of map entries (key-value pairs). First I filter only those entries that have the value mx. Then I map the key function on these, giving me a sequence of keys.</p> <p>To check whether there are any modes I don't loop over the map again. Instead I just compare the number of modes to the number of map entries. If they are equal, all elements have the same frequency!</p> <p>Here's the function that always returns a seq:</p> <pre><code>(defn modes [aseq] (let [amap (tally-map aseq) mx (apply max (vals amap)) modes (map key (filter #(= mx (val %)) amap))] (when (&lt; (count modes) (count amap)) modes))) </code></pre>
    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