This file contains hidden or 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
| implicit val bob = "Bob" | |
| def greet(implicit name: String) = { | |
| println(s"Hello, $name!") | |
| } | |
| // usage | |
| greet | |
| // outputs "Hello, Bob!" |
This file contains hidden or 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
| implicit val bob = "Bob" | |
| implicit val alice = "Alice" | |
| def greet(implicit name: String) = { | |
| println(s"Hello, $name!") | |
| } | |
| // usage | |
| greet |
This file contains hidden or 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
| implicit def intToStr(num: Int): String = s"The value is $num" | |
| 42.toUpperCase() // evaluates to "THE VALUE IS 42" | |
| def functionTakingString(str: String) = str | |
| // note that we're passing int | |
| functionTakingString(42) // evaluates to "The value is 42" |
This file contains hidden or 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 Helpers { | |
| implicit class StringOps(str: String) { | |
| def yell = str.toUpperCase() + "!" | |
| def isQuestion = str.endsWith("?") | |
| } | |
| } | |
| "Hello world".yell // evaluates to "HELLO WORLD!" | |
| "How are you?".isQuestion // evaluates to 'true' |
This file contains hidden or 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
| case class StringOps(str: String) { | |
| def yell = str.toUpperCase() + "!" | |
| def isQuestion = str.endsWith("?") | |
| } |
This file contains hidden or 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
| case class StringOps(str: String) { | |
| def yell = str.toUpperCase() + "!" | |
| def isQuestion = str.endsWith("?") | |
| } | |
| implicit def stringToStringOps(str: String): StringOps = StringOps(str) | |
| "Hello world".yell // evaluates to "HELLO WORLD!" | |
| "How are you?".isQuestion // evaluates to 'true' |
This file contains hidden or 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
| // Our interface | |
| trait Monoid[A] { | |
| def zero: A | |
| def plus(a: A, b: A): A | |
| } | |
| // Implementation for integers | |
| implicit object IntegerMonoid extends Monoid[Int] { | |
| override def zero: Int = 0 | |
| override def plus(a: Int, b: Int): Int = a + b |
This file contains hidden or 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
| def sum[A](values: Seq[A])(implicit ev: Monoid[A]): A | |
| def sum[A:Monoid](values: Seq[A]): A |
This file contains hidden or 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
| def sum[A:Monoid](values: Seq[A]): A = { | |
| val ev = implicitly[Monoid[A]] | |
| values.foldLeft(ev.zero)(ev.plus) | |
| } |
This file contains hidden or 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
| def implicitly[T](implicit e: T) = e |
OlderNewer