-
-
Save stephen-lazaro/64caa1910c2957b8e826ef3c57362aff 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
def getSheet: Either[Throwable, Sheet] = Try(WorkbookFactory.create(i).getSheetAt(0)).toEither | |
val decodeId: RowDecoder[Int] = | |
RowDecoder.from(row => Try(row.getCell(0).toInt).toEither) | |
val decodeBalance: RowDecoder[Double] = | |
RowDecoder.from(row => Try(row.getCell(1).toDouble).toEither) | |
val decodeLoanState: RowDecoder[String] = | |
RowDecoder.from(row => Try(row.getCell(2)).toEither) | |
// Boom! Decode each row into a Loan object so long as we are able to decode each cell. | |
val decodeLoan: RowDecoder[Loan] = map3( | |
decodeId, | |
decodeBalance, | |
decodeLoanState | |
)(Loan.apply) | |
def processSheet[A: RowDecoder](sheet: Sheet): Iterator[Either[Throwable, Loan]] = | |
sheet.iterator() | |
.asScala | |
.map(RowDecoder[A].decode) | |
val records: Iterator[Either[Throwable, Loan]] = getSheet.flatMap(processSheet[Loan]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment