Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@lmarburger

This comment has been minimized.

Copy link

commented Sep 8, 2010

I love returning. I wish it was part of core Ruby along side tap. This is a prime example of how using returning instead of tap clearly expresses the intent.

@drewblas

This comment has been minimized.

Copy link

commented Sep 8, 2010

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

@zeedunk

This comment has been minimized.

Copy link

commented Sep 15, 2010

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
You can’t perform that action at this time.