`=<<;`在Scalaz工作,Kleisli

`=<<;`在Scalaz工作,Kleisli,scala,scalaz,Scala,Scalaz,对于Scalaz repo中Kleisli函数合成的以下示例: import scalaz._ import Scalaz._ import Kleisli._ import scala.util._ case class Continent(name: String, countries: List[Country] = List.empty) case class Country(name: String, cities: List[City] = List.em

对于Scalaz repo中Kleisli函数合成的以下示例:

  import scalaz._
  import Scalaz._
  import Kleisli._

  import scala.util._

  case class Continent(name: String, countries: List[Country] = List.empty)
  case class Country(name: String, cities: List[City] = List.empty)
  case class City(name: String, isCapital: Boolean = false, inhabitants: Int = 20)

  val data: List[Continent] = List(
    Continent("Europe"),
    Continent("America",
      List(
        Country("USA",
          List(
            City("Washington"), City("New York"))))),
    Continent("Asia",
      List(
        Country("India",
          List(City("New Dehli"), City("Calcutta"))))))

  def continents(name: String): List[Continent] =
    data.filter(k => k.name.contains(name))

  def countries(continent: Continent): List[Country] = continent.countries

  def cities(country: Country): List[City] = country.cities

  def inhabitants(c: City): Int = c.inhabitants
  val allCities = kleisli(continents) >==> countries >==> cities
我得到以下结果:

// fine, expected result for "America"
scala> (allCities =<< List("America")).map(println)
City(Washington,false,20)
City(New York,false,20)
res16: List[Unit] = List((), ())

// confused, why does this bring back the same cities for "Amer"
scala> (allCities =<< List("Amer")).map(println)
City(Washington,false,20)
City(New York,false,20)
res17: List[Unit] = List((), ())

// confused again, has brought back cities for America and Asia - seems to be matching on the first character??
scala> (allCities =<< List("A")).map(println)
City(Washington,false,20)
City(New York,false,20)
City(New Dehli,false,20)
City(Calcutta,false,20)

// confused again, brings back everything:

scala> (allCities =<< List("")).map(println)
City(Washington,false,20)
City(New York,false,20)
City(New Dehli,false,20)
City(Calcutta,false,20)
res19: List[Unit] = List((), (), (), ())
//很好,《美国》的预期结果

scala>(所有城市=(所有城市=(所有城市=(allCities=出于完整性考虑:
Kleisli
部分在这里不是真正相关的。您看到的行为完全是
大陆
方法中的
过滤器
的结果。它返回所有大陆的名称,其中包含该方法的参数,这意味着
美国
Asia
“A”
返回,一切为
返回,等等。

不确定我是否理解您的期望?
大陆
返回包含给定
名称
字符串的所有大陆(在任何位置),所以结果对我来说似乎是有针对性的。如果我们举第二个例子,为什么它为美国带回了城市?为什么/它如何匹配“Amer”?我希望它匹配完整的字符串America,而不是“Amer”。它是
.filter(k=>k.name.contains(name))
part.Doh!忽略了它。谢谢