Can Map be performed on a Scala HList

前端 未结 3 851
再見小時候
再見小時候 2021-01-30 17:09

I have done a few implementations of HList now. One based on Daniel Spiewak\'s High Wizardry in the Land of Scala talk and another based on a post in Apocalisp blog. The goal

3条回答
  •  时光取名叫无心
    2021-01-30 18:13

    what you need is a Klist with type constructor Request, and a natural transformation execute: Request ~> Id. All of this is detailed in the marvelous type-level programming series of posts at Apocalisp, in particular:

    1. Natural transformation literals
    2. Klist basics

    you can checkout the code for the whole series from Mark Harrah's up repo

    In your case, you'll need something like

    val reqList = new Request[Int](1) :^: new Request[String]("1") :^: KNil    
    val exec = new (Request ~> Id) { def apply[T](reqs: Request[T]): T = reqs.execute }    
    val results = reqList down exec
    

    the down method above is conceptually the same as map for a nat transf M ~> Id; you also have more general map which from a nat transf M ~> N and a Klist of kind M yields a KList of kind N.

提交回复
热议问题