title: How to avoid multiple iterations as a pattern?
body: <p>In functional languages (using F#), I am struggling to find a balance between the advantages of functional composition with single-responsibility and getting the performance of single iteration over sequences.  Any code pattern suggestions / examples for achieving both?</p>
  
  <p>I don't have a solid background in computational theory and I run into this general pattern over and over: Iterating over a collection and wanting to do side-effects while iterating to avoid further iterations over the same collection or its result set.</p>
  
  <p>A typical example is a "reduce" or "filter" function: There are many times while filtering that I want to take an additional step based on the filter's result, but I'd like to avoid a second enumeration of the filtered results.</p>
  
  <p>Let's take input validation as a simple problem statement:</p>
  
  <ol>
  <li>Named input array</li>
  <li>Piped to an "isValid" function filter</li>
  <li>Side-effect: Log invalid input names</li>
  <li>Pipe valid inputs to further execution</li>
  </ol>
  
  <h1>Problem Example</h1>
  
  <p>In F#, I might initially write:</p>
  
  <pre><code>inputs
  // how to log invalid or other side-effects without messing up isValid??
  |&gt; Seq.filter isValid
  |&gt; execution
  </code></pre>
  
  <h2>Solution Example #1</h2>
  
  <p>With an in-line side-effect, I need something like:</p>
  
  <pre><code>inputs
  |&gt; Seq.filter (fun (name,value) -&gt; 
      let valid = isValid (name,value)
      // side-effect
      if not valid then
          printfn "Invalid argument %s" name
      valid
  |&gt; execution
  </code></pre>
  
  <h2>Solution Example #2</h2>
  
  <p>I could use tuples to do a more pure separation of concerns, but requiring a second iteration:</p>
  
  <pre><code>let validationResults =
      inputs
      // initial iteration
      |&gt; Seq.filter (fun (name,value) -&gt; 
          let valid = isValid (name,value)
          (name,value,valid)
      |&gt; execution
  
  // one example of a 2nd iteration...
  validationResults
  |&gt; Seq.filter (fun (_,_,valid) -&gt; not valid)
  |&gt; Seq.map (fun (name,_,_) -&gt; printfn "Invalid argument %s" name)
  |&gt; ignore
  
  // another example of a 2nd iteration...
  for validationResult in validationResults do
      if not valid then
          printfn "Invalid argument %s" name
  </code></pre>
  
  <h1>Update 2014-07-23 per Answer</h1>
  
  <p>I used this as the solution per the answer. The pattern was to use an aggregate function containing the conditional.  There are probably even more elegantly concise ways to express this...</p>
  
  <pre><code>open System
  
  let inputs = [("name","my name");("number","123456");("invalid","")]
  
  let isValidValue (name,value) =
      not (String.IsNullOrWhiteSpace(value))
  
  let logInvalidArg (name,value) =
      printfn "Invalid argument %s" name
  
  let execution (name,value) =
      printfn "Valid argument %s: %s" name value
  
  let inputPipeline input =
      match isValidValue input with
      | true -&gt; execution input
      | false -&gt; logInvalidArg input
  
  inputs |&gt; Seq.iter inputPipeline
  </code></pre>