Created
March 19, 2018 21:26
-
-
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
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
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