David Portabella
David Portabella

Reputation: 12710

In Scala, process a list of items with a Try operation, and keep the original item to report the possible failure

I have a list of strings, and I want to make several transformation to each item. I want to keep the original string, so that I can show it if it was invalid. Example as follows:

import scala.util.Try

val list = List("<entry id='1'/>", "haha", "<entry id='hehe'/>")

def parseXML(str: String) = Try { xml.XML.loadString(str) }

list
  .map(parseXML)
  .map(tryEntry => tryEntry.map(entry => (entry \ "@id").text))
  .map(tryId => tryId.flatMap(id => Try(id.toInt)))

// here I lose the original string
res17: List[Try[Int]] = List(
  Success(1),
  Failure(org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.),
  Failure(java.lang.NumberFormatException: For input string: "hehe")
)

// here I keep a copy of the original string, so I can report the invalid entry to the user
list
  .map(l => (l, parseXML(l)))
  .map { case(line, tryEntry) => (line, tryEntry.map(entry => (entry \ "@id").text)) }
  .map { case(line, tryId) => (line, tryId.flatMap(id => Try(id.toInt))) }

res19: List[(String, Try[Int])] = List(
  ("<entry id='1'/>", Success(1)),
  ("haha", Failure(org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.)),
  ("<entry id='hehe'/>", Failure(java.lang.NumberFormatException: For input string: "hehe"))
)

In res19, I keep a copy of the original string, so I can report the error and the original string. However, I need to carry this information each time during a mapping operation, and this is ugly. Is there a better way? (maybe using ScalaZ State and for?)

Upvotes: 1

Views: 99

Answers (3)

Dima
Dima

Reputation: 40500

If there are no duplicate strings in the list, you could make it a map, and then mapValues over it:

(l zip l toMap)
   .mapValues(parseXML)
   .mapValues(tryEntry => tryEntry.map(entry => (entry \ "@id").text))
   .mapValues(tryId => tryId.flatMap(id => Try(id.toInt)))
   .toList

Upvotes: 0

Dima
Dima

Reputation: 40500

Maybe, collapsing your multiple maps into one would help?

 val result: List[(String, Try[Int])] = list
   .map { line =>
      line -> parseXML(line)
        .map(_ \ "@id")
        .text
        .flatMap(id => Try(id.toInt)
   }

Upvotes: 0

Edwin de Jong
Edwin de Jong

Reputation: 26

This could elegantly be solved by adding an extra trait ParseContext[T], which, apart from the original context, such as line number and 'original' text, is a Functor (.map(f: T => T)) and a Monad (.flatMap(f: T => ParseContext[T])). You could optionally make two instances of ParseContext, named Success and Failure, to indicate a possible failure during parsing.

Basically, you extend the Try trait with some extra context information.

Upvotes: 1

Related Questions