Given an array, like:
[0, 0.5, 0.51, 1.0, 1.5, 1.99, 2.0, 2.1, 2.5, 3.0]
I want to cluster together values into subarrays based on their sequential differences (e.g., where abs(x-y) < n
, and n = 0.2
), e.g.:
[[0], [0.5, 0.51], [1.0], [1.5], [1.99, 2.0, 2.1], [2.5], [3.0]].
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).
Thanks in advance.
Update:
Here's a one-liner that's kinda close:
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) < 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]]
Just need to get rid of duplicates.