- slots = @slots.map{|k,v| k}.reduce{|a,b|
- a = [(a..a)] if !a.is_a?(Array)
- if b == (a[-1].last)+1
- a[-1] = (a[-1].first)..b
- a
+ # We want to display the hash slots assigned to this node
+ # as ranges, like in: "1-5,8,9,20-35,30"
+ #
+ # Note: this could be easily written without side effects,
+ # we use 'slots' just to split the computation into steps.
+
+ # First step: we want an increasing array of integers
+ # for instance: [1,2,3,4,5,8,9,20,21,22,23,24,25,30]
+ slots = @slots.keys.sort
+
+ # As we want to aggregate adiacent slots we convert all the
+ # slot integers into ranges (with just one element)
+ # So we have something like [1..1,2..2, ... and so forth.
+ slots = slots.map{|x| x..x}
+
+ # Finally we group ranges with adiacent elements.
+ slots = slots.reduce([]) {|a,b|
+ if !a.empty? && b.first == (a[-1].last)+1
+ a[0..-2] + [(a[-1].first)..(b.last)]