Created
May 28, 2013 12:29
-
-
Save lloydmeta/5662413 to your computer and use it in GitHub Desktop.
Returns a List of consecutive Doubles based on a start and end and a step value. Guarantees the end value is in the last double.
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
def listOfConsecutivePairsInRange(start: Double, end: Double, step: Double = 1) = { | |
val initialRange = start to end by step | |
def splitIntoConsecutivePairs(xs: List[Double]):List[(Double,Double)] = { | |
xs match { | |
case x::y::Nil => { | |
if (y != end) { | |
List((x, end)) | |
} else { | |
List((x, y)) | |
} | |
} | |
case x::y::ys => List((x,y)) ::: splitIntoConsecutivePairs(y::ys) | |
case _ => Nil | |
} | |
} | |
splitIntoConsecutivePairs(initialRange.toList) | |
} | |
// Example | |
listOfConsecutivePairsInRange(1000, 3000, 300) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment