score:2
i guess tt should be a reverse to flatmap with a function f:seq[char] => char. does it make sense ?
not really. what should your inverse function f:seq[char] => char
return on "abc"
? it should apply to any sequence of characters and return a single character. you could try using partialfunction[seq[char], char]
instead, but you'll run into other problems. do you apply it to every subsequence of your input?
the more general solution would be to use foldleft
with the accumulator type containing both the built-up part of the result and the escaping sequence, something like (untested):
def unescape(str: string) = {
val result = str.foldleft[(string, option[string])](("", none)) { case ((acc, escapedacc), c) =>
(c, escapedacc) match {
case ('&', none) =>
(acc, some(""))
case (_, none) =>
(acc + c, none)
case ('&', some(_)) =>
throw new illegalargumentexception("nested escape sequences")
case (';', some(escapedacc1)) =>
(acc + unescapemap(escapedacc1), none)
case (_, some(escapedacc1)) =>
(acc, some(escapedacc1 + c))
}
}
result match {
case (escaped, none) =>
escaped
case (_, some(_)) =>
throw new illegalargumentexception("unfinished escape sequence")
}
}
val unescapemap = map("amp" -> "&", "lt" -> "<", ...)
(it's much more efficient to use stringbuilder
s for the accumulators, but this is simpler to understand.)
but for this specific case you could just split the string on &
, then split each part except first on ;
, and get the parts you want this way.
score:1
this seems to be a follow-up to my own answer to the question whose follow-up this question is... use scala.xml.utility.unescape
:
val sb = new stringbuilder
scala.xml.utility.unescape("amp", sb)
println(sb.tostring) // prints &
or if you just want to unescape once and throw away the stringbuilder
instance:
scala.xml.utility.unescape("amp", new stringbuilder).tostring // returns "&"
this just parses individual escapes; you'll have to build a parser of entire xml strings around it yourself—the accepted answer seems to provide that bit but fails to not reinvent the scala.xml.utility
wheel— or use something from scala.xml
instead.
Source: stackoverflow.com
Related Query
- How to implement Map with default operation in Scala
- How do I implement a generic mathematical function in Scala
- How do I implement a collection in Scala 2.8?
- How to implement enums in scala slick 3?
- How to implement breadth first search in Scala with FP
- How do I implement Kafka Consumer in Scala
- scala 3 macro how to implement generic trait
- How to elegantly implement the pipeline pattern using Scala
- How to implement Scala apply method in Java
- How to implement Lazy Chain Pattern in Scala
- How does one implement a Hadoop Mapper in Scala 2.9.0?
- How to implement a short-circuit with IO monad in Scala
- how to implement this future/state concept as a monad in scala
- How does Scala implement return from within an expression?
- How to implement Java interface in Scala with multiple variable parameter methods (type eraser issue)?
- How to implement a recursive Fibonacci sequence in Scala using FS2?
- How to implement types like MapK in Scala 3 / Dotty?
- How to implement org.hibernate.Session in Scala
- How to implement tail-recursive quick sort in Scala
- How to create an Encoder for Scala collection (to implement custom Aggregator)?
- How to implement a trait with a generic case class that creates a dataset in Scala
- How to implement chain of string.replaceAll in Scala
- How to implement maxBy with multiple max in Scala
- How to best implement "first success" in Scala (i.e., return the first success from a sequence of failure-prone operations)
- How to implement a custom tail recursive map for lists in Scala
- How to implement security Authorization using scala and play?
- How would you implement a caching aspect in Scala
- How to implement scala generic function that takes subtype of parameterized traits and returns it
- How to implement this simple algorithm elegantly in Scala
- How to implement generic function in Scala with two argument types?
More Query from same tag
- How to create correct data frame for classification in Spark ML
- How to use array of scala.collection.immutable.List in java code
- Calling clojure code from a scala sbt task
- specify partitions size with spark
- Scala Parser Combinator compilation issue: failing to match multiple variables in case
- spark-submit 'Unable to coerce 'startDate' to a formatted date (long)'
- Mix pattern matching into a chain of maps and filters
- Zio, transform Seq[ZIO] to ZIO[Seq]
- sbt compiles fine but IntelliJ shows errors
- Getting sub-scores for sub-queries in Lucene
- different development/production databases in scalaquery
- How to set System property for a scalatest from sbt command line
- How Implicit Ordering[Option[T]] works in scala?
- Akka streams - throw away a message
- Scala union operation on binary search tree
- What does it mean to have an 'empty' case statement in Scala?
- Lifting Functions using Cats Functor
- Is Communicating Sequential Processes [CSP] an alternative to the actor model in Scala?
- Scala Lagom development mode & Docker
- Too many arguments when I use (...)(...) function
- Play framework how do sessions and cookies work?
- How to match ngrams for each document in Spark LDA code
- Declare js dependencies in sbt
- How reduceByKey in RddPair<K,Tuple> in Scala
- Scala pattern matching an equation case
- Last Aggregation function in Dataframe Spark Scala
- Simple cross platform GUI app
- Could not copy native binaries to temp directory Kinesis KPL
- Multiple lower type bounds in Scala
- How to build an eclipse plugin with sbt and sbt-osgi?