Skip to content

Instantly share code, notes, and snippets.

@stephen-lazaro
Last active November 19, 2018 18:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephen-lazaro/64caa1910c2957b8e826ef3c57362aff to your computer and use it in GitHub Desktop.
Save stephen-lazaro/64caa1910c2957b8e826ef3c57362aff to your computer and use it in GitHub Desktop.
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