首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Scalaz(30)- Free :Natural Tranformation ~> - map higher kinded types for free

Scalaz(30)- Free :Natural Tranformation ~> - map higher kinded types for free

作者头像
用户1150956
发布2018-01-05 10:28:39
5530
发布2018-01-05 10:28:39
举报

    当我们需要定义一些对应高阶类型进行相互类型转换的操作函数时,我们发现scala语言并不提供能定义这种函数的支持。举例来说:如果我们希望定义一个函数把对于任何T值的Option[T]转换成List[T]的话,我们可能这样定义:

1 def toList[T](opt: Option[T]): List[T] = opt.toList
2                                                   //> toList: [T](opt: Option[T])List[T]
3 val hOptFun = toList _                            //> hOptFun  : Option[Nothing] => List[Nothing] = <function1>

看看hOptFun的类型:Option[Nothing] => List[Nothing], 即我们无法对T的类型进行限定。如果我们使用hOptFun:

1 hOptFun(None)                                     //> res0: List[Nothing] = List()
2 //hOptFun(Some(10)) //type mismatch;  found   : Int(10)  required: Nothing
3 //hOptFun(Some("hi")) //type mismatch;  found   : String("hi")   required: Nothing

结果是无法正常使用hOptFun。这就证明了scala是不支持高阶类型转换函数的。一个可行的方法是通过一个特殊类来实现高阶类型转换,这就是scalaz的NaturalTransformation类型的主要功能。scalaz的NaturalTransformation类型是这样定义的:scalaz/NaturalTransformation.scala

/** A universally quantified function, usually written as `F ~> G`,
  * for symmetry with `A => B`.
  *
  * Can be used to encode first-class functor transformations in the
  * same way functions encode first-class concrete value morphisms;
  * for example, `sequence` from [[scalaz.Traverse]] and `cosequence`
  * from [[scalaz.Distributive]] give rise to `([a]T[A[a]]) ~>
  * ([a]A[T[a]])`, for varying `A` and `T` constraints.
  */
trait NaturalTransformation[-F[_], +G[_]] {
  self =>
  def apply[A](fa: F[A]): G[A]
...

我们只需要实现apply就行了:

1 val optionToListTrans = new (Option ~> List) {
2   def apply[T](opt: Option[T]): List[T] = opt.toList
3 }                                                 //> optionToListTrans  : scalaz.~>[Option,List] = Exercises.naturaltransform$$an
4                                                   //| onfun$main$1$$anon$1@2d554825
5 optionToListTrans(None)                           //> res1: List[Nothing] = List()
6 optionToListTrans(Some("hi"))                     //> res2: List[String] = List(hi)
7 optionToListTrans.apply(3.some)                   //> res3: List[Int] = List(3)

从optiontoListTrans.apply可以看出我们实际上直接调用了那个按我们要求实现的apply方法。换句话说:如果我们需要把F[A]与G[A]对应,我们可以通过实现apply的方式来表达它们具体的对应方式,这个apply方法就可以实现高阶类型的相互转换。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档