Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morgen-peschke/d3fef1c7868e34065acdf5c2d3a92a59 to your computer and use it in GitHub Desktop.
Save morgen-peschke/d3fef1c7868e34065acdf5c2d3a92a59 to your computer and use it in GitHub Desktop.
Example of how to create an Extension Method on a Companion Object
object ExtensionMethodsOnCompanionObjects {
implicit class ListFactoryOps(val list: List.type) extends AnyVal {
def fizzbuzz(n: Int): List[String] =
(0 to n).map {
case n if n % 15 == 0 => "FizzBuzz"
case n if n % 3 == 0 => "Fizz"
case n if n % 5 == 0 => "Buzz"
case n => n.toString
}.toList
}
def lengthFromArgs(args: Array[String]): Int = args.headOption.fold(15)(_.toInt)
def main(args: Array[String]): Unit =
List.fizzbuzz(lengthFromArgs(args)).foreach(println)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment