`=<<`如何在Scalaz.Kleisli中工作

时间:2017-02-22 12:00:30

标签: 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.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((), (), (), ())

1 个答案:

答案 0 :(得分:1)

为了完整起见:if (indexPath as NSIndexPath).row < tableListInfo_Context.count { //Configure cells that are not the last cell //----------------------------- // This is what you are missing mainView.isHidden = false //----------------------------- } else { //Configure cells the last cell mainView.isHidden = true } 部分在这里并不真正相关 - 您所看到的行为完全是Kleisli方法中filter的结果。它返回包含方法参数的所有大陆,这意味着为continents返回AmericaAsia,为"A"返回所有内容,等等。