This really depends on the scale of the problem, i.e. how many things do you expect to have in both of the groups. However, as a starting point, you could use Seq.groupBy
to get a list of ranges for each group and then use Seq.exist
to check whether a position is in any of the ranges.
For example, given the following inputs:
let ranges = [ (1, 0, 5); (1, 10, 15 ) ]
let coords = [ (1, 3); (1, 7); (1, 12) ]
You can create a dictionary that gives you a list of ranges for each group:
let byGroup =
ranges
|> Seq.groupBy (fun (g, _, _) -> g)
|> Seq.map (fun (k, v) -> k, Seq.map (fun (_, s, e) -> (s, e)) v)
|> dict
And then you can filter coordinates, and for each position, scan the appropriate ranges to see if the position belongs to any of them:
coords
|> Seq.filter (fun (g, pos) ->
byGroup.[g] |> Seq.exists (fun (l, h) -> pos >= l && pos <= h))
This is probably not going to work if the number of ranges is very high, but it should be good enough for smaller examples. For bigger data, you'll probably want to create some sort of binary tree-like structure (where you can search for ranges), but this is more complex question, which is more about algorithm than about F#.