Skip to content

Instantly share code, notes, and snippets.

@pragdave
Created September 8, 2010 16:52
Show Gist options
  • Save pragdave/570406 to your computer and use it in GitHub Desktop.
Save pragdave/570406 to your computer and use it in GitHub Desktop.
# encoding: utf-8
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)
# divide the input into runs of consecutive numbers
s = input.slice_before(State.new(input.first)) do |value, state|
(_, state.last_value = value != state.last_value.succ, value)[0]
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
@pragdave
Copy link
Author

pragdave commented Sep 8, 2010

that's the return value of the block, which is set to true whenever it detects a break in the sequence of input values

@JonKernPA
Copy link

wow, i have a lot to learn. (yippee!)
too bad you couldn't throw in some c-style bit twiddling .

@JonKernPA
Copy link

looking at this a couple months later and after reading PragProg's awesome MetaProgramming with Ruby book (even tho there is not really anything specific in the book that applies here), I can make better sense out of the original source now. Progress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment