Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Created June 27, 2013 09:19
Show Gist options
  • Save aya-eiya/5875143 to your computer and use it in GitHub Desktop.
Save aya-eiya/5875143 to your computer and use it in GitHub Desktop.
ちょっとしたことがGroovy「ある値まで一定間隔のRangeのリストを作りたい」 ref: http://qiita.com/aya_eiya/items/d48541f1ca2e57c16ef2
def getSteps = {start=1,max,step=1000 ->
if(max <= start + step) return [[start..max]]
def rs = (start..max).step(step).inject([]) {a,c -> (c>start) && a << [ ((a!=[])?a.last().to[0]+1:start)..c-1];a}
(rs.last().to[0] != max) && rs << [rs.last().to[0]+1..max];rs
}
println"""
getSteps(max=10020) = ${getSteps(max=10020)}
"""
println"""
getSteps(100,1000) = ${getSteps(100,1000)}
"""
println"""
getSteps(100,1015,10) = ${getSteps(100,1015,10)}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment