很多时候我们会遇到一些高阶类型F[_],但又无法实现它的map函数,也就是虽然形似但F不可能成为Functor。 但是,往往在一些场合里我们需要把F当做Functor来使用,如用Free Structure把F升格成Monad。 也就是说我们需要把Interact当做Functor才能构建一个基于Interact的Free Monad。 Scalaz里的Coyoneda与任何F[_]类型成同构(互等),而Coyoneda是个Functor,这样我们可以用Coyoneda来替代F。 ,A](coyo: _Coyoneda[F,A]): F[A] = 15 Functor[F].map(coyo.fi)(coyo.k) 16 17 } 对于任何类型F及A, 我们通过toCoyo
; interface Functor<T> { <R> Functor<R> map(Function<T, R> f); } But mere syntax is not enough to understand what functor is. <T,F extends Functor<? Apart from looking horrible, having a functor in functor ruins composition and fluent chaining: 1 2 Having a function of int -> Date we can easily transform from Functor<Integer> to Functor<Date>, we know
scalaz提供了Functor typeclass不但使用户能map over自定义的高阶类型F[T],并且用户通过提供自定义类型的Functor实例就可以免费使用scalaz Functor typeclass scalaz中Functor的trait是这样定义的:scalaz/Functor.scala 1 trait Functor[F[_]] extends InvariantFunctor[F] { self 针对我们自定义的类型,我们只要实现map函数就可以得到这个类型的Functor实例。一旦实现了这个类型的Functor实例,我们就可以使用以上scalaz提供的所有Functor组件函数了。 我们先试着创建一个类型然后推算它的Functor实例: 1 case class Item3[A](i1: A, i2: A, i3: A) 2 val item3Functor = new Functor 进行compose: 1 scala> val f = Functor[List] compose Functor[Item3] 2 f: scalaz.Functor[[α]List[Item3[α]
这就是 Functor、 Applicative、 Monad、 Arrow 等概念的基础。 Maybe 数据类型定义了两种相关上下文: ? 究竟什么是 Functor 呢? 在 Haskell 中 Functor 是一个类型类。 其定义如下: ? 在 Kotlin 中,可以认为 Functor 是一种定义了 fmap 方法/扩展函数的类型。 以下是 fmap 的工作原理: ? Iterable 也是 functor! 是实现了 Functor 类型类的数据类型。
前言 学习笔记输出~ 内容 Functor(函子) 什么是函子 容器: 包含值和值的变形关系(函数) 函子: 是一个特殊的容器,通过一个普通的对象来实现,该对象具有map方法,map方法可以运行一个函数对值进行处理
一.Functor像盒子? functor and box 函数也是Functor类实例?! 那么,是不是所有的Functor类实例都可以这样理解呢? 里的函数作用于另一个包在Functor里的值上 那么有没有一种对任何Functor都有效的通用模式,能帮助我们完成这个事情(把一个Functor里的函数作用于另一个Functor里的值)? 里的函数作用于另一个Functor里的值 所以,Applicative对Functor的增强体现在<*>函数上,增强方式是让这些Functor实例都实现个<*>,支持把一个Functor里的函数作用于另一个 但有了applicative functor,我们可以对好多个functor套用一个(多参)函数 其二是允许Functor结合(而不像fmap算一次得到个Functor就只能结束了,通过<*>能够继续运算下去
我们成功设计了个Functor函数 2、A => F[B] 1 case class Box[A](a: A) 2 def flatMap[A,B](f: A => Box[B]): Box[A //> res2: ch12.ex3.Box[Int] = Box(12) apply函数就是Applicative函数 虽然我们在上面分别实现了Functor,Applicative,Monad的功能函数 但Functor,Applicative,Monad都是泛函数据类型,我们还没有明确定义这些数据类型。这些数据类型自提供了操作函数对嵌在内部的变量进行更新。也就是说它们应该自带操作函数。 ,bxHello是个Functor实例。 看来,Functor, Applicative, Monad除了名称怪异外实际上并不可怕,我们可以从它们的用途中了解它们的意义。
可惜的是它对F[A]是有所要求的:F必须是个Functor。Free Monad由此被称为由Functor F 产生的Monad。 F必须是Functor,这个门槛使我们在使用Free Monad时很不方便。 现在的问题是如果能有个什么方法把F[A]变成Functor,就像Free Monad那样有个Free Functor就好了。 1 trait Functor[F[_]] { 2 def map[A,B](fa: F[A])(f: A => B): F[B] 3 } 4 object Functor { 5 def apply[F[_]: Functor]: Functor[F] = implicitly[Functor[F]] 6 } 7 trait Monad[M[_]] { 8 def
文章目录 一、仿函数 functor 1、仿函数 functor 简介 2、仿函数 functor 调用 3、代码示例 - 仿函数 functor 调用 二、为自定义类元素设置排序规则 - 仿函数 functor 1、自定义类排序规则 2、仿函数 - 实现自定义类排序规则 3、重载 < 运算符函数 - 实现自定义类排序规则 一、仿函数 functor 1、仿函数 functor 简介 在上一篇博客 【C++】STL set 集合容器 的 排序规则 , 可以使用 函数回调 实现 , 函数回调 可以使用 函数指针 实现 , 但是 C++ 语言中 , 提供了 仿函数 机制 , 可以借助 仿函数 实现 回调函数 ; 仿函数 functor 调用 要调用一个 仿函数 functor : 给定如下仿函数 : struct IntCompare { bool operator()(const int& a, const int& b) const 圆括号来调用仿函数 , 就像调用普通函数一样 , 将所需的参数传递给 仿函数对象 进行调用 ; // 通过 仿函数对象 调用仿函数 bool b = ic(1, 2); 3、代码示例 - 仿函数 functor
Applicative 就是某种Functor,因为我们可以用map2来实现map,所以Applicative可以map,就是Functor,叫做Applicative Functor。 表面上看来Monad已经覆盖了Functor, Applicative。可能就是因为Monad的功能太强大了,所以Monad的函数组合(functional composition)非常有限。 也就是说把traverse需要的Applicative Functor降至Id Functor后traverse相当于map操作。 换句话说Traverse可以概括Functor并且traverse操作要比map操作强大许多。 因为所有Monad都是Applicative,所以等于我们已经获取了State Applicative Functor实例。
和Applicative隐式实例(implicit instance),我们希望Configure类型既是一个Functor也是一个Applicative。 properties: 1 object functor { 2 def identity[F[_], X](implicit F: Functor[F], afx: Arbitrary[ 我们先测试Functor属性: 1 functor.laws[Configure].check //> 2 + functor.invariantFunctor.identity 成功通过Functor定律测试。 现在我们可以说Configure类型既是Functor也是Applicative。
template <typename Functor> bool consume_one(Functor & f) template <typename Functor> bool consume_one (Functor const & f) template <typename Functor> size_t consume_all(Functor & f) template <typename Functor> size_t consume_all(Functor const & f) template <typename Functor> size_t consume_all_atomic (Functor & f) template <typename Functor> size_t consume_all_atomic(Functor const & f) template <typename Functor> size_t consume_all_atomic_reversed(Functor & f) template <typename Functor> size_t consume_all_atomic_reversed
那么我们应该可以把这个map抽象出来:通过增加一个高阶类型Functor,用它来概括实现map: 1 trait Functor[F[_]] { 2 def map[A,B](a: F[ 所以Functor的类参数是F[_],即: Functor[List], Functor[Option], Functor[Par] ...,这里面F[_]就是F[A],A可以是任何类型。 我们可以设计一个List的Functor实例: 1 object ListFunctor extends Functor[List] { 2 def map[A,B](la: List[ 也就是说List,Option等本身就是Functor。换句话讲就是:它们都可以map,所以都是Functor。 我们称这个抽象模型为Monad,它继承了Functor的特性,是Functor,因为Monad可以map。
先看看Functor的composition: 1 trait Functor[F[_]] { 2 def map[A,B](fa: F[A])(f: A => B): F[ B] 3 } 4 def compose[F[_],G[_]](m: Functor[F], n: Functor[G]) = 5 new Functor[({type l [F], n: ch12.ex2.Functor[G])ch12.e 10 //| x2.Functor 这个Functor[({type l[x] = F[G[x]]})#l]就是一个Functor实例,因为我们可以实现map[A,B](fga: F[G[A]])(f: A => B)。 有了这个Functor实例,我们就可以处理F[G[A]]这样类型的数据类型: 1 val listFunctor = new Functor[List] { 2 override def
{ public: Functor() : _function(NULL) { } Functor(const Functor _function = NULL; } ~Functor() { delete _function; } Functor * functor = (Functor*)parameter; (*functor)(); delete functor; // 记得这里需要delete ) { // bind()返回的是一个临时对象, // 故需要new一个,以便在thread_proc()过程中有效 Functor* new_functor = new Functor(functor); int errcode = pthread_create(&_thread, NULL, thread_proc, new_functor
相关文章 Functor & Monad | 函子和单子 Functor Functor 的特性 Summary 相关文章 一本书里面内容较多, 因此分成了多篇 Post, 可以从此处看到相关文章 Functor 的特性 A functor is anything (such as an object) that can be mapped over or that implements the 原生的 Array 类型就是一个 Functor [1, 2, 3, 4].map(add).map(add) Functor 的设计原则 identity: 如果给 map 传递一个返回自己的方法 Example Functor with Mixin const Functor = { map(f = identity) { /* 注意当前 Functor 需要用到 Container }; /* 将 Functor 扩展到 Monad 中 */ const Monad = Object.assign({}, Functor, { flatMap(f) { return
Capture By Value class Functor { public: Functor(const int x): m_x(x) {} int operator()(int functor(x); volatile int y1 = functor(5); volatile int y2 = lambda(5); return 0; } 按值捕获lambda Capture By Reference class Functor { public: Functor(int& x): m_x(x) {} int operator()(int functor(x); volatile int y1 = functor(5); volatile int y2 = lambda(5); return 0; } 当通过引用捕获时, 与按值捕获一样,functor和lambda调用代码是等价的,但是lambda的构造函数是内联的,而functor的则不是。 结论 C ++ lambda和函子比相似之处更多。
注意,Suspend(a: S[Free[S,A]])是个递归类型,S必须是个Functor,但不是任何Functor,而是map over Free[S,A]的Functor,也就是运算另一个Free 简单来说Free是一个把Functor S[_]升格成Monad的产生器。我们可以用Free.liftF函数来把任何一个Functor升格成Monad。 看看Free.liftF的函数款式就知道了: /** Suspends a value within a functor in a single step. 实际上我们可以把这个必须是Functor的门槛取消,因为用Coyoneda就可以把任何F[A]拆解成Coyoneda[F,A],而Coyoneda天生是个Functor。 但是又想了想,一个是Functor的Interact又是怎样的呢?
最大的感受是,以前对 Functor、Applicative 和 Monad 的理解太片面了。 Functor 谈 Monad(单子) 之前还是要谈谈 Functor(函子),毕竟所有的 Monad 都是 Functor。 在范畴论中,函子是范畴间的一类态射(这个定义给我的直观感受是函子指的是 fmap 函数……),数学上的概念就不多说了,下面我们来看看 Haskell 中的 Functor。 Haskell 中有一个叫 Functor 的类型类(暂时可以粗略地理解为 OO 语言中的接口),它的定义是这样的: class Functor f where fmap :: (a -> b) - 所以从 Functor 的定义来看,似乎只要实现了 fmap 函数的类型构造器,就是函子了。
这个要求从上面Free[F,A]类型里的map,flatMap可以了解:我们用了implicit F: Functor[F]参数,因为必须有个Functor实例F才能实现map和flatMap。 [F],因为Free[F,A]的F必须是个Functor:用Functor F可以产生Free[F,A]。 我还没想到如何得出它的Functor实例。好像没办法实现那个map函数。 这样我们就可以得出Interact的Functor实例。 实例: 1 implicit val stackOpsFunctor: Functor[StackOps] = new Functor[StackOps] { 2 def map[A,B](oa