title: Functional clustering of array values in Swift
body: <p>Given an array, like:</p>
  
  <pre><code>[0, 0.5, 0.51, 1.0, 1.5, 1.99, 2.0, 2.1, 2.5, 3.0] 
  </code></pre>
  
  <p>I want to cluster together values into subarrays based on their sequential differences (e.g., where <code>abs(x-y) &lt; n</code>, and <code>n = 0.2</code>), e.g.:</p>
  
  <pre><code>[[0], [0.5, 0.51], [1.0], [1.5], [1.99, 2.0, 2.1], [2.5], [3.0]]. 
  </code></pre>
  
  <p>I'd like to do it declaratively — just to get a better grasp on how more complex sequence operations might work in a functional context (it seems like most "functional Swift" demos/tutorials are pretty basic).</p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>Here's a one-liner that's kinda close:</p>
  
  <pre><code>let times = [0, 0.5, 0.99, 1, 1.01, 1.5, 2, 2.5, 2.51, 3, 3.49, 3.5]
  
  let result = times.map { t1 in
      return times.filter { fabs($0 - t1) &lt; 0.2 }
  }
  
  // [[0.0], [0.5], [0.99, 1.0, 1.01], [0.99, 1.0, 1.01], [0.99, 1.0, 1.01], [1.5], [2.0], [2.5, 2.51], [2.5, 2.51], [3.0], [3.49, 3.5], [3.49, 3.5]]
  </code></pre>
  
  <p>Just need to get rid of duplicates.</p>