title: Composing Haskell filters
body: <p>I am converting the <a href="https://tech.dropbox.com/2012/04/zxcvbn-realistic-password-strength-estimation/" rel="nofollow">zxcvbn password strength</a> algorithm to Haskell.</p>
  
  <p>I have two functions that check for all characters being ASCII and that a brute force attack is possible:</p>
  
  <pre><code>filterAscii :: [String] -- ^terms to filter
              -&gt; [String] -- ^filtered terms
  filterAscii = filter $ all (\ chr -&gt; ord chr &lt; 128)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>filterShort :: [String] -- ^terms to filter
              -&gt; [String] -- ^filtered terms
  filterShort terms = map fst $ filter long $ zip terms [1..]
    where long (term, index) = (26 ^ length term) &gt; index
  </code></pre>
  
  <p>I composed these into a single function:</p>
  
  <pre><code>filtered :: [String] -- ^terms to filter
           -&gt; [String] -- ^filtered terms
  filtered = filterAscii . filterShort
  </code></pre>
  
  <p>I now have need to compose these with a third filter to check if the terms are not null:</p>
  
  <pre><code>filter (not . null) terms
  </code></pre>
  
  <p>It has occurred to me that I am creating a chain of filters and that it would make more sense to create a single function that takes a list of filter functions and composes them in the order given.</p>
  
  <p>If I recall from my reading, this is a job for an applicative functor, I believe. Can I use applicatives for this?</p>
  
  <p>I am not sure how to handle the <code>filterShort</code> function where I need to <code>zip</code> each item with its one-based index before filtering.</p>