-
Functor
2021-01-08 21:28:52<div><p>Added Functor and Maybe. <p>As I note in the test, Swift <em>seems</em> to infer the type returned from <code>fmap</code> incorrectly. Or maybe we messed up the types somewhere? <p>/cc </p><p>... -
functor
2018-05-23 14:41:19functor 是什么?在阅读Tensorflowy源码时看到了这个,后来明白这是个命名空间,但查了一下原来functor并不简单,以后很可能会用到,特此转载记下。引子有一次在美团面试的时候,第二轮面试官问道:“听说过functor...functor 是什么?
在阅读Tensorflowy源码时看到了这个,后来明白这是个命名空间,但查了一下原来functor并不简单,以后很可能会用到,特此转载记下。
下面是转载的作者原文:
引子
有一次在美团面试的时候,第二轮面试官问道:“听说过functor吗?”,妈呀,6年了,竟然没听说过这个概念,今天就学习了一会,哎,天不遂人愿,老天弄人啊,其实这个东西我们大家都不止一次的在使用它。举个例子(注意for_each的第三个参数):
- // for_each example
- #include <iostream> // std::cout
- #include <algorithm> // std::for_each
- #include <vector> // std::vector
- void myfunction (int i) { // function:
- std::cout << ' ' << i;
- }
- struct myclass { // function object type:
- void operator() (int i) {std::cout << ' ' << i;}
- } myobject;
- int main () {
- std::vector<int> myvector;
- myvector.push_back(10);
- myvector.push_back(20);
- myvector.push_back(30);
- std::cout << "myvector contains:";
- for_each (myvector.begin(), myvector.end(), myfunction);
- std::cout << '\n';
- // or:
- std::cout << "myvector contains:";
- for_each (myvector.begin(), myvector.end(), myobject);
- std::cout << '\n';
- return 0;
- }
是的,这就是functor的思想。
补充一下functor的实现(各个人实现可能不一样哦,不同的库,不同的语言也可能不一样哦),仅供参考:
- template<class InputIterator, class Function>
- Function for_each(InputIterator first, InputIterator last, Function fn)
- {
- while (first!=last) {
- fn (*first);
- ++first;
- }
- return fn; // or, since C++11: return move(fn);
- }
什么是functor?
functor的英文解释为something that performs a function,即其行为类似函数的东西。C++中的仿函数是通过在类中重载()运算符实现,使你可以像使用函数一样来创建类的对象。
为什么使用仿函数(functor)
迭代和计算逻辑分离
使用仿函数可以使迭代和计算分离开来。因而你的functor可以应用于不同场合,在STL的算法中就大量使用了functor.
参数可设置
可以很容易通过给仿函数(functor)设置参数,来实现原本函数指针才能实现的功能.
有状态
与普通函数另一个区别是仿函数(functor)是有状态的.
性能
我们看一下代码:
1std::transform(in.begin(), in.end(), out.begin(), add_x(1));
编译器可以准确知道std::transform需要调用哪个函数(add_x::operator)。这意味着它可以内联这个函数调用。而如果使用函数指针,编译器不能直接确定指针指向的函数,而这必须在程序运行时才能得到并调用。
一个例子就是比较std::sort 和qsort ,STL的版本一般要快5-10倍。
-
Counter functor
2020-12-25 18:58:44<div><p>While experimenting with the counter functor, I wrote the following trivial program <pre><code> .decl A(a:number) .output A A(1). A($) :- A(1). A($) :- A(1). A($) :- A(1). </code></pre> <p>... -
matlab开发-functor
2019-08-28 01:02:15matlab开发-functor。功能手柄的自动组合 -
C++ functor
2017-08-02 15:08:13functor就是一个重载了 operator()的类,用这个类生成的实例就像一个函数。(functor就是一个作为函数用的类),在c++11后可以用lambda函数实现同样的功能。参考链接:stackoverflow// this is a functor struct add...functor就是一个重载了 operator()的类,用这个类生成的实例就像一个函数。(functor就是一个作为函数用的类),在c++11后可以用lambda函数实现同样的功能。
参考链接:stackoverflow
// this is a functor struct add_x { add_x(int x) : x(x) {} int operator()(int y) const { return x + y; } private: int x; }; // 这也是一个functor struct inc{ int operator()(int _i) { return _i + 1;} }; // Now you can use it like this: add_x add42(42); // create an instance of the functor class int i = add42(8); // and "call" it assert(i == 50); // and it added 42 to its argument, 检查i是否等于50 std::vector<int> in; // assume this contains a bunch of values) std::vector<int> out(in.size()); // Pass a functor to std::transform, which calls the functor on every element // in the input sequence, and stores the result to the output sequence // add_x(1), 相当于创建了 add_x add1(1) // add1 相当于一个函数,传入一个int参数, 这个int 会加上 1 std::transform(in.begin(), in.end(), out.begin(), add_x(1)); //相当于 inc i_1; std::transform(in.begin(), in.end(), out.begin(), i_1); // 效果和下面这句相同 std::transform(in.begin(), in.end(), out.begin(), [](int x){ return x + 1;}); assert(out[i] == in[i] + 1); // for all i
-
Generic functor holder and constant image from functor.
2020-12-02 21:12:26- <strong>FunctorHolder</strong>: hold any callable object (function, functor, lambda, ...) as a C(Unary)Functor model without relying on <code>std::function - <strong>FunctorConstImage</strong>: ... -
functor gem extraction
2020-11-21 21:15:25<div><p>I think the functor and all it's attendant pieces and parts should be extracted into a separate gem. <p>I've been making a start at updating the tests and qed demos of facets. Right ... -
add Distributive functor
2021-01-12 14:27:31<p>A <code>Distributive</code> Functor is one that you can push any functor inside of. <pre><code>purescript distribute :: (Functor f, Distributive g) => f (g a) -> g (f a) </code></pre> ... -
Contravariant Functor?
2020-12-26 04:55:20<div><p>Would you be interested in a contravariant functor added to the spec? <p>It would have a method <code>contramap :: Contravariant f => (a -> b, f b) -> f a</code>. It would become ... -
Add queue functor
2021-01-10 07:49:19This solution extends the type arguments to provide the functor functionality without an inverse map function but still be able to provide the original queue offer.</p><p>该提问来源于开源项目:... -
Flatten /functor
2020-12-27 04:16:25<p>Continuing the flatten by flattening the <code>stan/math/{fwd, mix, rev, prim}/*/functor</code> folder to <code>stan/math/{fwd, mix, rev, prim}/functor</code>. There are no file merges here, just ... -
add interpolator functor
2020-12-02 14:08:01Interpolation should be done via a functor, so that the interpolation can have state. <pre><code> auto h = ... // histogram auto interp = interpolator(h, interpolator::linear); </code></pre>... -
Functor test failing
2020-12-30 08:49:45<div><p>As mentioned in the pull request for adding tests (#1), the functor test is failing: <pre><code> bilby.js(tests): grunt test Running "test:src" (test) task Testing features.js....F.. &... -
Haskell语言学习笔记(4)Functor
2017-01-28 07:23:45FunctorFunctor
Functor(函子)是一个类型类,它是一种广义的能被映射的容器或计算环境。class Functor (f :: * -> *) where fmap :: (a -> b) -> f a -> f b (<$) :: a -> f b -> f a (<$) = fmap . const (<$>) :: Functor f => (a -> b) -> f a -> f b (<$>) = fmap ($>) :: Functor f => f a -> b -> f b ($>) = flip (<$) void :: Functor f => f a -> f () void x = () <$ x
将一个 a->b 的函数映射到一个 a 类型 的 Functor 之上会得到一个 b 类型的 Functor。Functor的法则
fmap id ≡ id fmap (f . g) ≡ fmap f . fmap g
[] 是 Functor
instance Functor [] where -- fmap : (a -> b) -> [a] -> [b] fmap = map
Prelude> :k [] [] :: * -> * Prelude> fmap (+2) [1,2,3] [3,4,5] Prelude> (+2) <$> [1,2,3] [3,4,5] Prelude> 3 <$ [1,2,3] [3,3,3] Prelude Data.Functor> [1,2,3] $> 3 [3,3,3] Prelude Data.Functor> void [1,2,3] [(),(),()]
证明[]符合Functor法则: 1. fmap id ≡ id fmap id [a] ≡ [id a] ≡ [a] id [a] ≡ [a] 2. fmap (f . g) ≡ fmap f . fmap g fmap (f . g) [a] ≡ [(f . g) a] ≡ [f (g a)] (fmap f . fmap g) [a] ≡ fmap f (fmap g [a]) ≡ fmap f [g a] ≡ [f (g a)]
Maybe 是 Functor
instance Functor Maybe where -- fmap : (a -> b) -> (Maybe a) -> (Maybe b) fmap _ Nothing = Nothing fmap f (Just a) = Just (f a)
Prelude> :k Maybe Maybe :: * -> * Prelude> fmap (+2) (Just 3) Just 5 Prelude> (+2) <$> (Just 3) Just 5 Prelude> fmap (+2) Nothing Nothing Prelude> 2 <$ Just 3 Just 2
证明Maybe符合Functor法则: 1. fmap id ≡ id fmap id Nothing ≡ Nothing id Nothing ≡ Nothing fmap id (Just a) ≡ Just (id a) ≡ Just a id (Just a) ≡ Just a 2. fmap (f . g) ≡ fmap f . fmap g fmap (f . g) Nothing ≡ Nothing (fmap f . fmap g) Nothing ≡ fmap f (fmap g Nothing) ≡ fmap f Nothing ≡ Nothing fmap (f . g) (Just a) ≡ Just ((f . g) a) ≡ Just (f (g a)) (fmap f . fmap g) (Just a) ≡ fmap f (fmap g (Just a)) ≡ fmap f (Just (g a)) ≡ Just (f (g a))
((->) r) 是 Functor
instance Functor ((->) r) where -- fmap : (a -> b) -> ((->) r a) -> ((->) r b) -- fmap : (a -> b) -> (r -> a) -> (r -> b) fmap = (.)
fmap (+2) (*3) 4 = 4*3+2 = 14Prelude> fmap (+2) (*3) 4 14 Prelude> (+2) <$> (*3) $ 4 14 Prelude> 2 <$ (*3) $ 4 2
Either a 是 Functor
data Either a b = Left a | Right b instance Functor (Either a) where fmap _ (Left x) = Left x fmap f (Right y) = Right (f y)
Prelude> :k (Either String) (Either String) :: * -> * Prelude> fmap (replicate 3) (Right "blah") Right ["blah","blah","blah"] Prelude> replicate 3 <$> Right "blah" Right ["blah","blah","blah"] Prelude> fmap (replicate 3) (Left "foo") Left "foo" Prelude> "hello" <$ Right "blah" Right "hello" Prelude> "hello" <$ Left "foo" Left "foo"
((,) a) 是 Functor
instance Functor ((,) a) where fmap f (x,y) = (x, f y)
Prelude> :k ((,)String) ((,)String) :: * -> * Prelude> fmap (*3) (1,2) (1,6)
-
Add Functor Support To ChaiScript
2020-12-01 22:07:54<div><p>I would like to be able to call a functor within ChaiScript. <p>Example C++: <pre><code> C++ struct Functor { void operator()() { std::cout << 5 << '\n'; }... -
haskell Functor
2014-11-17 09:08:19Functor 这个 typeclass,基本上就代表可以被 map over 的事物。听到这个词你可能会联想到 List,因为 map over list 在 Haskell 中是很常见的操作。你没想错,List 的确是属于 Functor 这个 typeclass。typeclass Functor
代表可以被 map over 的事物。List, Maybe 都是Functor. 需要是一个参数的类型构造子或函数。需要满足functor law:(满足定理的好处, 用定律来推论类型的行为, 不用看 fmap 的实现)
- 如果我们对 functor 做 map
id
,那得到的新的 functor 应该要跟原来的一样。如果写得正式一点,他代表fmap id = id
- 先将两个函数合成并将结果 map over 一个 functor 的结果,应该跟先将第一个函数 map over
一个 functor,
再将第二个函数 map over 那个 functor 的结果是一样的。正式地写下来的话就是fmap (f . g) = fmap f . fmap g
。
(或 fmap (f . g) F = fmap f (fmap g F))
class Functor f where
fmap :: (a -> b) -> f a -> f binstance Functor [] where
fmap = mapinstance Functor Maybe where
fmap f (Just x) = Just (f x)
fmap f Nothing = Nothingdata Either a b = Left a | Right b
instance Functor (Either a) where
fmap f (Right x) = Right (f x)
fmap f (Left x) = Left xinstance Functor ((->) r) where
fmap f g = (\x -> f (g x))instance Functor ((->) r) where
fmap :: (a -> b) -> (r -> a) -> (r -> b)
fmap = (.)
说明;如果一个 type constructor 要是Functor
的 instance,那他的 kind 必须是* -> *
,这代表他必须刚好接受一个 type 当作 type parameter。像是Maybe
可以是 Functor 的一个 instance,因为他接受一个 type parameter,来做成像是Maybe Int
,或是Maybe String
。如果一个 type constructor 接受两个参数,像是Either
,我们必须给他两个 type parameter。所以我们不能这样写:instance Functor Either where
,但我们可以写instance Functor (Either a) where
,如果我们把fmap
限缩成只是Either a
的,那他的型态就是fmap :: (b -> c) -> Either a b -> Either a c
。对于是一个参数的函数的类型: (->) r 是描述类型。 原来看到 (*3), 一直以为 r是需要在输入的参数,是不对的。(->) 的Kind 如下,是一个两个参数的运算符。 (->) r 是一个整体, 表示 (->) 已经接受一个参数, 他的Kind 应该是 *-> * 的类型。
> :k (->) (->) :: * -> * -> *
- 如果我们对 functor 做 map
-
Flip domain axis functor
2020-12-01 18:49:07<div><p>Small basic functor to mirror domain coordinates </p><p>该提问来源于开源项目:DGtal-team/DGtal</p></div> -
Scala Cats - Functor
2020-11-30 23:25:32cats - FunctorFunctor不同的角度用 Functor 来管理效果(effect)Functor 的组合参考 Functor Functor 是用来表示抽象化出可以调用 map 方法的结构 的 类型类. 这种结构比如说有 List, Option, 和 Future. 实现了 ... -
Functor fix and tests
2020-12-25 19:04:11<div><p>Reorder functor arguments to fix #856 <p>Add tests for functors (-o followed by execution)</p><p>该提问来源于开源项目:souffle-lang/souffle</p></div> -
`Functor` instances
2020-12-08 23:20:24<div><p>Is it possible to make <code>Module</code> (and everything underneath it) a <code>Functor, so that I can use <code>fmap</code> to change what is stored in the <code>annot</code> fields?... -
functor_纯Java中的Functor和Monad示例
2020-06-26 18:35:52functor 本文最初是我们使用RxJava进行React式编程的附录。 但是,尽管与React式编程非常相关,但对monad的介绍却不太适合。 因此,我决定将其取出并作为博客文章单独发布。 我知道,“ 我对单子的自己的,一半正确... -
Capture view for functor
2021-01-07 14:24:35For every input index, a transform outputs a vector of indices based on the functor. However, I feel like I haven't completed my work updating this. Ideally, an input view of indices would result... -
Functor& Monad解读
2019-10-03 14:06:45trait Functor[F[_]] Functor:代表整体封装; F[_]:代表封装后的目标域。 A、B:代表普通的对象;f:代表对象间转换的函数。 Functor[F[_]] :映射的结果和源在同一个范畴内; Functor的代码表示 trait ... -
Haskell学习-functor
2018-08-16 09:24:00原文地址:Haskell学习-functor 什么是Functor functor 就是可以执行map操作的对象,functor就像是附加了语义的表达式,可以用盒子进行比喻。functor 的定义可以这样理解:给出a映射到b的函数和装了a的盒子,结果会...
-
smzy_idapro.rar
-
关于docker安装创建mongodb并且测试代码
-
Kotlin协程极简入门与解密
-
【数据分析-随到随学】SPSS调查问卷统计分析
-
将两条宽带通过两个路由器组成一个局域网的方法.zip
-
竞斗云2.0官方固件-备份
-
行业分类 职业分类 职务分类 单位性质
-
【数据分析-随到随学】数据可视化
-
【数据分析-随到随学】Python语法强化与数据处理
-
深入理解NIO与Epoll
-
java 扫雷源代码 带有详细的注释
-
CentOS 7 安装PHP 5.4.16
-
SpringBoot 在线协同办公小程序开发 全栈式项目实战
-
【数据分析-随到随学】机器学习模型及应用
-
PostgreSQL-11.10-osr507-Binary.tar.gz
-
(新)备战2021软考网络工程师分类强化培训套餐
-
9000题库-下.iso
-
(新)备战2021软考信息安全工程师顺利通关套餐
-
leetcode刷题day6
-
嵌入式系统开发笔记05:让VS Code在调试时启动特定文件