How to fix this exercise with an endomorphic wrapper?

This is a continuation of my previous question .

Suppose I need to find an XML node along a path. I can write a function to get a child of a node named

import scala.xml.{Node => XmlNode}

def child(name: String): XmlNode = Option[XmlNode] = _.child.find(_.label == name)

I will compose functions childwith kleisli; eg.

 
scala> val a = <a><a0><a1><a2/></a1></a0></a>
a: scala.xml.Elem = <a><a0><a1><a2/></a1></a0></a>

scala> val find01 = Kleisli(child("a0")) >=> Kleisli(child("a1"))
findAB: scalaz.Kleisli[Option,scala.xml.Node,scala.xml.Node] = Kleisli(<function1>)

scala> find01(a)
res85: Option[scala.xml.Node] = Some(<a1><a2/></a1>)

Now I use an endomorphic shell, but it does not work:

 scala> List(child("a0"), child("a1")).foldMap(Endomorphic.endoKleisli[Option, XmlNode])
 res93: scalaz.Endomorphic[[α, β]scalaz.Kleisli[Option,α,β],scala.xml.Node] = Endomorphic(Kleisli(<function1>))

 scala> res93.run(a)
 res94: Option[scala.xml.Node] = None

Could you find a mistake?

+2
source share
1 answer

In the first example, you make left to right ( >=>).

But it calls the method (aka ), which works from right to left. Endomorphiccompose<=<

So, your second example captures first <a1>and then searches <a0>inside, which obviously fails.

. Dual.

+2

All Articles