score:1
Given that unselect
currently just returns a Zipper[Node]
, I don't see a way to get what you want without bossing the type system around a bit, and that's going to take something like a cast.
In this case you really do know something the type system can't, given the current state of the Anti-XML library: you know that the parent of the zipper resulting from your transformation is a Zipper[Elem]
, and therefore that what unselect
gives you actually will be a Zipper[Elem]
, even though it's typed as a Zipper[Node]
.
So I think the best you can do is package up the unpleasantness a little more cleanly:
def toEndo(t: Elem => Zipper[Elem]): Elem => Elem =
t andThen (_.unselect.head.asInstanceOf[Elem])
Or, to make your intent more apparent:
def toEndo(t: Elem => Zipper[Elem]) = t andThen (_.unselect.head match {
case e: Elem => e
case _ => throw new RuntimeException("Aaaaah! This was never meant to happen!")
})
Then you can write the following:
val transforms: Seq[Elem => Zipper[Elem]] = Seq(
_ \\ "book" filter (_.attrs("year").toInt > 2000),
_ \\ "book" filter (_.attrs("title").contains("J"))
)
val result = Function.chain(transforms map toEndo)(lib)
Note that I've moved the unselect
into the helper to give us a bit more type safety.
Source: stackoverflow.com
Related Query
- Chaining transformations in AntiXML
- RxJava: chaining observables
- What are the Spark transformations that causes a Shuffle?
- Scala - Chaining Futures Try blocks?
- Chaining methods left to right in Haskell (as opposed to right to left)
- Chaining PartialFunctions with andThen in Scala
- Best practice to implement Scala trait which supports method chaining
- Chaining implicits via the shortest route
- Apply several string transformations in scala
- Chaining method calls with Either
- Can you use antixml to create xml documents?
- Chaining function in Scala using dot notation
- Chaining Scalaz validation functions: Function1[A,Validation[E,B]]
- State transformations with a shapeless State monad
- How to call superclass constructor from child class in scala and how to do constructor chaining
- Type safe method chaining that doesn't allow repeats of operations
- "does not take parameters" when chaining method calls without periods
- Scala's monadic chaining of Try
- Chaining scala Try instances that contain Options
- Why does chaining match expressions does not compile?
- Omitting dots when chaining calls
- Functional patterns for better chaining of collect
- Chaining operations on values without naming intermediate values
- Scala: Affine Transformations
- Future Recursion Patterns/Future Chaining of arbitrary length
- Scala - how to pattern match when chaining two implicit conversions?
- What is difference between transformations and rdd functions in spark?
- Chaining implicit conversions of function to a generic class
- Chaining a number of transitions with the state Monad
- Scala chaining functions with andThen type mismatch
More Query from same tag
- unrecognizable code in scala Predef object
- How to access an element of list within a list in scala
- spark avoid collect as much as possible
- How can I convert Rep[Option[Instant]] to Rep[Option[String]] in scala
- Curried method call by infix notation
- Spark job doesn't print when running on multiple clusters on databrick notebook
- Filter for various scala types
- Play Framework how to set selected option in the form?
- Scala's for-comprehension `if` statements
- Self-consistent and updated example of using Spark over ElasticSearch
- How to merge and aggregate 2 Maps in scala most efficiently?
- How to take last event from cassandra event stream
- Scala tuple unpacking for constructors with abstract class for inheritance
- Possible to embed information at compile time with SBT?
- Passing DEFAULTS args in function scala
- Akka: testing monitoring\death watch
- Why does for-comprehension reverse the input list?
- Compilation issue when accessing parameter value in Scala macro
- Removing values after particular character from rdd in scala
- MultilayerPerceptronClassifier in Spark. Layers and strange errors
- Serving multiple result Futures as soon as available to a client
- Unit testing in Spark with SQLContext implicits
- Apache-Spark UDF defined inside object raises "No TypeTag available for String"
- Defining a Map from String to Function in Scala
- How to read data from a file as a Graph (GraphX)?
- Scala Multiple Future wrapped in Try
- Functor implementation for types with more than one type
- filter a List according to multiple contains
- Multidimentional array of arrays in Scala
- How to exclude package from Scala SBT