Scalaz(30)- Free :Natural Tranformation ~> - map higher kinded types for free
来源:互联网 发布:税务申报软件 编辑:程序博客网 时间:2024/06/03 17:52
当我们需要定义一些对应高阶类型进行相互类型转换的操作函数时,我们发现scala语言并不提供能定义这种函数的支持。举例来说:如果我们希望定义一个函数把对于任何T值的Option[T]转换成List[T]的话,我们可能这样定义:
def toList[T](opt: Option[T]): List[T] = opt.toList //> toList: [T](opt: Option[T])List[T]val hOptFun = toList _ //> hOptFun : Option[Nothing] => List[Nothing] = <function1>
hOptFun(None) //> res0: List[Nothing] = List()//hOptFun(Some(10)) //type mismatch; found : Int(10) required: Nothing//hOptFun(Some("hi")) //type mismatch; found : String("hi") required: Nothing
/** 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]...
val optionToListTrans = new (Option ~> List) { def apply[T](opt: Option[T]): List[T] = opt.toList} //> optionToListTrans : scalaz.~>[Option,List] = Exercises.naturaltransform$$an //| onfun$main$1$$anon$1@2d554825optionToListTrans(None) //> res1: List[Nothing] = List()optionToListTrans(Some("hi")) //> res2: List[String] = List(hi)optionToListTrans.apply(3.some) //> res3: List[Int] = List(3)
0 0
- Scalaz(30)- Free :Natural Tranformation ~> - map higher kinded types for free
- Scalaz(29)- Free :Coyoneda - Functor for free
- Scalaz(33)- Free :算式-Monadic Programming
- Scalaz(34)- Free :算法-Interpretation
- Scalaz(38)- Free :Coproduct-Monadic语句组合
- Scalaz(32)- Free :lift - Monad生产线
- Scalaz(32)- Free :lift - Monad生产线 企
- Scalaz(39)- Free :a real monadic program
- Scalaz(40)- Free :versioned up,再回顾
- Scalaz(54)- scalaz-stream: 函数式多线程编程模式-Free Streaming Programming Model
- Scalaz(36)- Free :实践-Free In Action - 实用体验
- Scalaz(37)- Free :实践-DB Transaction free style
- Scalaz(41)- Free :IO Monad-Free特定版本的FP语法
- Scalaz(31)- Free :自由数据结构-算式和算法的关注分离
- Scalaz(35)- Free :运算-Trampoline,say NO to StackOverflowError
- Scalaz(42)- Free :FreeAp-Applicative Style Programming Language
- Cats(4)- 叠加Free程序运算结果,Stacking monadic result types
- free.
- UNITY3D shader代码例子<二> 双面显示
- POJ 2236 Wireless Network
- Android Toast与Looper的深入研究,Toast是否属于修改UI界面
- 《Unix高级环境编程》 第十一章 Threads
- Oracle数据库字符集和客户端字符集不同,强制转化可能会造成不可预期的后果
- Scalaz(30)- Free :Natural Tranformation ~> - map higher kinded types for free
- 二分查找法及复杂度计算
- 开源图像检索项目PicSearch is now an open source project
- POJ 1182 食物链
- PLSQL developer 连接不上64位Oracle
- PAT (Advanced Level) Practise 1035 Password (20)
- VB实现自动取得服务器IP并连接服务器(利用UDP广播)
- Android5.0之后获取后台正在运行的程序
- 多线程