Module | Tins::Minimize |
In: |
lib/tins/minimize.rb
lib/tins/minimize.rb |
This module can be mixed into all classes, whose instances respond to the
should respond to the succ method.
Returns a minimized version of this object, that is successive elements are substituted with ranges a..b. In the situation …, x, y,… and y != x.succ a range x..x is created, to make it easier to iterate over all the ranges in one run. A small example:
[ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ].minimize # => [ 'A'..'C', 'G'..'G', 'K'..'M' ]
If the order of the original elements doesn‘t matter, it‘s a good idea to first sort them and then minimize:
[ 5, 1, 4, 2 ].sort.minimize # => [ 1..2, 4..5 ]
Returns a minimized version of this object, that is successive elements are substituted with ranges a..b. In the situation …, x, y,… and y != x.succ a range x..x is created, to make it easier to iterate over all the ranges in one run. A small example:
[ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ].minimize # => [ 'A'..'C', 'G'..'G', 'K'..'M' ]
If the order of the original elements doesn‘t matter, it‘s a good idea to first sort them and then minimize:
[ 5, 1, 4, 2 ].sort.minimize # => [ 1..2, 4..5 ]
Invert a minimized version of an object. Some small examples:
[ 'A'..'C', 'G'..'G', 'K'..'M' ].unminimize # => [ 'A', 'B', 'C', 'G', 'K', 'L', 'M' ]
and
[ 1..2, 4..5 ].unminimize # => [ 1, 2, 4, 5 ]