Last active
August 29, 2015 13:59
-
-
Save Centaur/10696514 to your computer and use it in GitHub Desktop.
extract pair as expected type
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
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