Last active
March 9, 2017 18:43
-
-
Save kolemannix/2b9478eb732c0fe5a78f3a77d8fde237 to your computer and use it in GitHub Desktop.
ScalaCheck Polymorphic Generators for Latitude and Longitude
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
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