Created
May 16, 2020 02:39
-
-
Save collinalexbell/ba86c46594d50f08de7ddd8e6a6b16eb to your computer and use it in GitHub Desktop.
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 converter._ | |
import scala.language.implicitConversions | |
import scala.math.log10 | |
trait Lengthable { | |
def length: Int | |
} | |
implicit class LengthableString(self: String) extends Lengthable { | |
def length = self.size | |
} | |
implicit class LengthableInt(self: Int) extends Lengthable { | |
def length = (log10(self)).toInt + 1 | |
} | |
def printLen(ol: Option[Lengthable]) = ol match { | |
case None => println(0) | |
case Some(l) => println(l.length) | |
} | |
printLen(Some(333)) | |
printLen(Some("asdfasdf")) | |
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
package converter | |
Object converter { | |
implicit def optionConverter[A, B](oa: Option[A])(implicit m: A => B): Option[B] = oa match { | |
case None => Option.empty[B] | |
case Some(a) => Some(m(a)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment