functor

Why is `pure` only required for Applicative and not already for Functor? [duplicate]

冷暖自知 提交于 2019-11-30 05:19:27
This question already has an answer here: Why Functor class has no return function? 4 answers Reading this Wikibook about Haskell and Category Theory basics , I learn about Functors: A functor is essentially a transformation between categories, so given categories C and D, a functor F : C -> D maps any object A in C to F(A), in D. maps morphisms f : A -> B in C to F(f) : F(A) -> F(B) in D. ... which sounds all nice. Later an example is provided: Let's have a sample instance, too: instance Functor Maybe where fmap f (Just x) = Just (f x) fmap _ Nothing = Nothing Here's the key part: the type

Why have unary_function, binary_function been removed from C++11?

∥☆過路亽.° 提交于 2019-11-30 05:15:17
I found that binary_function is removed from C++11. I am wondering why. C++98: template <class T> struct less : binary_function <T,T,bool> { bool operator() (const T& x, const T& y) const {return x<y;} }; C++11: template <class T> struct less { bool operator() (const T& x, const T& y) const {return x<y;} typedef T first_argument_type; typedef T second_argument_type; typedef bool result_type; }; MODIFIED ---------------------------------------------------------------------------- template<class arg,class result> struct unary_function { typedef arg argument_type; typedef result result_type; };

What's the relationship between profunctors and arrows?

倾然丶 夕夏残阳落幕 提交于 2019-11-30 03:44:39
Apparently, every Arrow is a Strong profunctor. Indeed ^>> and >>^ correspond to lmap and rmap . And first' and second' are just the same as first and second . Similarly every ArrowChoice is also Choice . What profunctors lack compared to arrows is the ability to compose them. If we add composition, will we get an arrow? In other words, if a (strong) profunctor is also a category , is it already an arrow? If not, what's missing? What profunctors lack compared to arrows is the ability to compose them. If we add composition, will we get an arrow? MONOIDS This is exactly the question tackled in

Use of a functor on for_each

一曲冷凌霜 提交于 2019-11-30 03:43:58
问题 Why does the for_each call on functor doesn't update sum::total at the end? struct sum { sum():total(0){}; int total; void operator()(int element) { total+=element; } }; int main() { sum s; int arr[] = {0, 1, 2, 3, 4, 5}; std::for_each(arr, arr+6, s); cout << s.total << endl; // prints total = 0; } 回答1: for_each takes the functor by value - so it is copied. You can e.g. use a functor which is initialized with a pointer to an external int. struct sum { sum(int * t):total(t){}; int * total;

Visual Studio 2010 and std::function

心已入冬 提交于 2019-11-30 02:29:25
问题 I have this code: #include <iostream> #include <functional> struct A { int operator()(int i) const { std::cout << "F: " << i << std::endl; return i + 1; } }; int main() { A a; std::tr1::function<int(int)> f = std::tr1::ref(a); std::cout << f(6) << std::endl; } The aim is to pass the functor object by a reference_wrapper, in a way to avoid useless copy costructor calls. I expect the following output: F: 6 7 It works correctly with GCC >= 4.4.0, Visual Studio 2008 and with boost by substituting

Is an is_functor C++ trait class possible?

微笑、不失礼 提交于 2019-11-30 02:09:42
How can I deduce statically if an argument is a C++ function object (functor)? template <typename F> void test(F f) {} I tried is_function<F>::value , but this doesn't work. It also seems there is no is_functor trait, so perhaps it's not possible. I appear to be only looking for a specific member function, in this case the function call operator: F::operator() . It is possible to create such a trait, with two restrictions: For the compiler, a free function is something fundamentally different from a class functor that overloads operator() . Thus we have to treat both cases seperately when

What is exactly an indexed functor in Haskell and what are its usages?

流过昼夜 提交于 2019-11-30 00:49:41
问题 When studying functors in Haskell I came up with Functor.Indexed type of functor. This functor defines an operation called imap . I didn't understood its definition and imap signature: imap :: (a -> b) -> f j k a -> f j k b . I tried to find it s formal definition and found only this: http://ncatlab.org/nlab/show/indexed+functor . But it really didn't help me at all. So can someone clarify in more simple words this kind of functor and in what cases should I use it? Thanks. 回答1: An indexed

Functors when should I use them whats their intended use [closed]

浪子不回头ぞ 提交于 2019-11-30 00:36:27
I Just can't seem to wrap my head around them. As I understand it's dynamically adding logic to a class. Are classes within the framework prepared for this? Why should I just extend the class and add the functionality to it in the extension. I would be globally accessible and afaik much easier to maintain. I've Read there are 4 functor types: Comparer Closure Predicate Transformer We should probably Handle each one of them. p.s. is there something like it in vb? So I can state I think that lambda expressions are functors. This clears up things for me a bit :) (hehe) Lambda expressions are

Is there a Functor that cannot be a law-abiding Apply?

不问归期 提交于 2019-11-30 00:25:37
问题 A recent question asked generally about boundaries between various Haskell classes. I came up with Handler as an example of a valid Functor with no sensible instance of Apply**, where class Functor f => Apply f where (<.>) :: f (a -> b) -> f a -> f b -- optional bits omitted. However, I've not yet been able to find an example of a valid Functor that cannot be made a valid (if senseless) instance of Apply . The fact that Apply has had (see update) but a single law, (.) <$> u <.> v <.> w = u <.

What is a contravariant functor?

人走茶凉 提交于 2019-11-29 21:24:52
The type blows my mind: class Contravariant (f :: * -> *) where contramap :: (a -> b) -> f b -> f a Then I read this , but contrary to the title, I wasn't any more enlightened. Can someone please give an explanation of what a contravariant functor is and some examples? From a programmer's point of view the essence of functor-ness is being able to easily adapt things. What I mean by "adapt" here is that if I have an f a and I need an f b , I'd like an adaptor that will fit my f a in my f b -shaped hole. It seems intuitive that if I can turn an a into a b , that I might be able to turn a f a