Last active
August 29, 2015 14:16
-
-
Save Jacoby6000/3b24d8397491c0f92cb4 to your computer and use it in GitHub Desktop.
Scala extension methods
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 Implicits { | |
implicit class PimpedString(str: String) { | |
def everyNthChar(skipNum: Int) = (for (i <- 0 until str.length if i%skipNum==0) yield str(i)).mkString | |
//Other methods I want strings to have go here | |
} | |
implicit class PimpedInt(int: Int){ | |
//Other methods I want ints to have do here | |
} | |
} | |
class SomeClassThatUsesImplicits { | |
import Implicits._ | |
println("Hey Look, I can use methods from PimpedString!".everyNthChar(3)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment