title: Haskell maximumBy for Ord instances
body: <p>I keep finding myself wanting a function for this, and I always need to implement it myself, but I feel like there must be a built-in for it.</p>
  
  <p>Basically, what I want is sort of halfway between <code>maximum</code> and <code>maximumBy</code>.</p>
  
  <p><code>maximum</code> takes a list of <code>Ord</code> values and returns the maximum. This is O(n).</p>
  
  <pre><code>maximum :: (Ord a) =&gt; [a] -&gt; a
  </code></pre>
  
  <p><code>maximumBy</code> takes a list of non-<code>Ord</code> values and a sort function that provides an ordering for them, and computes the maximum using that function. This is O(nlogn) (or whatever the complexity is for a sort).</p>
  
  <pre><code>maximumBy :: (a -&gt; a -&gt; Ordering) -&gt; [a] -&gt; a
  </code></pre>
  
  <p>I want a <code>maximumBy</code> that takes a list of values of type <code>a</code> and a function that returns <code>Ord</code> values for <code>a</code> values, and computes the maximum <code>a</code> using the <code>Ord</code> values. This would be O(n) because it only needs to keep track of the maximum as it goes along. This is what that signature would be:</p>
  
  <pre><code>maximumBy' :: (Ord b) =&gt; (a -&gt; b) -&gt; [a] -&gt; a
  </code></pre>
  
  <p>And this is my rough implementation:</p>
  
  <pre><code>maximumBy' func list = foldl1' (\max i -&gt; let (maxby, iby) = (func max, func i) in if iby &gt; maxby then i else max) list
  </code></pre>
  
  <p>Is there such a function built-in, or perhaps an easy way to write this using existing built-ins?</p>