Created
September 8, 2010 17:13
-
-
Save pragdave/570434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input = [ 1, 2, 3, 4, 5, 8, 9, 11, 12, 13, 15 ] | |
# Need to box the value to make it mutable | |
State = Struct.new(:last_value) | |
def returning(value) yield; value; end | |
# divide the input into runs of consecutive numbers | |
s = input.slice_before(State.new(input.first)) do |value, state| | |
returning(value != state.last_value.succ) do | |
state.last_value = value | |
end | |
end | |
# replace runs of 3 or more with first-last | |
p s.map {|runs| runs.size < 3 ? runs : "#{runs.first}-#{runs.last}"} | |
.flatten | |
.join(', ') # => 1-5, 8, 9, 11-13, 15 |
I totally agree. I don't think this is a good example of how to use slice_before
. But it's a great example of how to use returning
Thanks for posting this, I made use of this example in a refactoring of the scoring koan that I posted here: http://gist.github.com/580960
Totally loving playing with ruby's Enumerable.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love
returning
. I wish it was part of core Ruby along sidetap
. This is a prime example of how usingreturning
instead oftap
clearly expresses the intent.