Skip to content

Instantly share code, notes, and snippets.

@collinalexbell
Created May 16, 2020 02:39
Show Gist options
  • Save collinalexbell/ba86c46594d50f08de7ddd8e6a6b16eb to your computer and use it in GitHub Desktop.
Save collinalexbell/ba86c46594d50f08de7ddd8e6a6b16eb to your computer and use it in GitHub Desktop.
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"))
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