Skip to content

Instantly share code, notes, and snippets.

@Centaur
Last active August 29, 2015 13:59
Show Gist options
  • Save Centaur/10696514 to your computer and use it in GitHub Desktop.
Save Centaur/10696514 to your computer and use it in GitHub Desktop.
extract pair as expected type
trait CanFromString[T] {
def fromString(s: String): T
}
implicit object intCanFromString extends CanFromString[Int] {
override def fromString(s: String): Int = s.toInt
}
implicit object SymbolCanFromString extends CanFromString[Symbol] {
override def fromString(s: String): Symbol = Symbol(s)
}
def pair[K: CanFromString, V: CanFromString](s: String, keyColumn: Int, valueColumn: Int): (K,V) = {
val arr = s.split(",")
implicitly[CanFromString[K]].fromString(arr(keyColumn)) -> implicitly[CanFromString[V]].fromString(arr(valueColumn))
}
assert(pair[Int, Symbol]("0001,notyy,37", 0, 1) == (1, 'notyy))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment