Skip to content

Instantly share code, notes, and snippets.

@kolemannix
Last active March 9, 2017 18:43
Show Gist options
  • Save kolemannix/2b9478eb732c0fe5a78f3a77d8fde237 to your computer and use it in GitHub Desktop.
Save kolemannix/2b9478eb732c0fe5a78f3a77d8fde237 to your computer and use it in GitHub Desktop.
ScalaCheck Polymorphic Generators for Latitude and Longitude
import org.scalacheck.Gen._
def latitudeGen[T](implicit num: Numeric[T], c: Choose[T]): Gen[T] = {
import num._
val min = fromInt(-90)
val max = fromInt(90)
Gen.sized(n => c.choose(min, max))
}
def longitudeGen[T](implicit num: Numeric[T], c: Choose[T]): Gen[T] = {
import num._
val min = fromInt(-180)
val max = fromInt(180)
Gen.sized(n => c.choose(min, max))
}
// Example Usage
// Concrete type is inferred (Float)
val x: Float = latitudeGen.sample.get
case class Coordinate(lat: Double, lon: Double)
// Can also supply type (Double)
val coordinateGen: Gen[Coordinate] = for {
lat <- latitudeGen[Double]
lon <- longitudeGen[Double]
} yield Coordinate(lat, lon)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment