functor

How are Functors useful?

最后都变了- 提交于 2019-12-03 10:01:19
问题 We know that any generic type F[_] with map method, which complies to some laws, is a functor . For instance, List[_] , Option[_] , and F[A] = Env => A are functors. I am just wondering if this functor abstraction is meaningful. How can I use the fact that they are functors ? Could you show an example of non-trivial computation, which would use the map and be actually useful ? 回答1: One of the biggest benefits of concepts like functions is that there are generic constructions that allow you to

the equivalence between applicative functor and monad

这一生的挚爱 提交于 2019-12-03 08:47:13
People say monads are an extension of applicative functors, but I don't see that. Let's take an example of applicative functor: (<*>) :: f(a->b) -> f a -> f b [(+3)] <*> [2,3,4] Now, I also expect I can do the same thing as monad, it means I can apply 2 parameters: a context contains a function, and another context to get a context. But for monad, I can't. All I need is to write an ugly function like this: [2,3,4] >>= (\x->[x+3]) Yes, of course, you can say that [(+3)] is equivalent to [\x->(x+3)] . But at least, this function is in context. Finally, I don't see the equivalence or extension

Make Data Type of Kind * -> * That's Not a Functor

社会主义新天地 提交于 2019-12-03 08:21:54
问题 Brent Yorgey's Typeclassopedia gives the following exercise: Give an example of a type of kind * -> * which cannot be made an instance of Functor (without using undefined ). Please tell me what " cannot be made an instance of Functor " means. Also, I'd appreciate an example, but perhaps as a spoiler so that you can, please, guide me to the answer. 回答1: Let's talk about variances. Here's the basic notion. Consider the type A -> B . What I want you to imagine is that such a type is similar to

C++ weird syntax spotted in Boost template parameters

对着背影说爱祢 提交于 2019-12-03 07:00:14
I was having a look at the "Function" class documentation in Boost, and stumbled across this: boost::function<float (int x, int y)> f; I must admit this syntax is highly confusing for me. How can this be legal C++ ? Is there any trick under the hood ? Is this syntax documented anywhere? [Edit] This is an answer to the author's original, unedited question which was actually two questions. I must admit this syntax is highly confusing for me. How can this be legal C++ ? :) Is there any trick under the hood ? Is this syntax documented anywhere ? This is perfectly legal and it's actually not too

Understanding how Either is an instance of Functor

做~自己de王妃 提交于 2019-12-03 05:29:53
问题 In my free time I'm learning Haskell, so this is a beginner question. In my readings I came across an example illustrating how Either a is made an instance of Functor : instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x Now, I'm trying to understand why the implementation maps in the case of a Right value constructor, but doesn't in the case of a Left ? Here is my understanding: First let me rewrite the above instance as instance Functor (Either a) where

How are functors in Haskell related to functors in category theory?

一个人想着一个人 提交于 2019-12-03 04:53:39
问题 For as far as I understand, a functor is a mapping between two categories, for example from objects in to objects in where and are categories. In Haskell there is Hask in which the objects are Haskell types and the morphisms are Haskell functions. However, the Functor type class has a function fmap which maps between these types (which are thus objects and not categories themselves): fmap :: (a -> b) -> f a -> f b f a and f b are both objects in Hask . Does this mean every instance of Functor

Haskell Functor implied law

你说的曾经没有我的故事 提交于 2019-12-03 04:45:13
Typeclassopedia says: "A similar argument also shows that any Functor instance satisfying the first law (fmap id = id) will automatically satisfy the second law as well. Practically, this means that only the first law needs to be checked (usually by a very straightforward induction) to ensure that a Functor instance is valid." If this is the case, why do we even mention the second functor law? Law 1: fmap id = id Law 2: fmap (g . h) = (fmap g) . (fmap h) While I can't give a proof, I believe what this is saying is that due to parametricity , the type system enforces the second law as long as

Why do several of the standard operators not have standard functors?

跟風遠走 提交于 2019-12-03 02:28:34
We have: std::plus ( + ) std::minus ( - ) std::multiplies ( * ) std::divides ( / ) std::modulus ( % ) std::negate ( - ) std::logical_or ( || ) std::logical_not ( ! ) std::logical_and ( && ) std::equal_to ( == ) std::not_equal_to ( != ) std::less ( < ) std::greater ( > ) std::less_equal ( <= ) std::greater_equal ( >= ) We don't have functors for: & (address-of) * (dereference) [] , bitwise operators ~ , & , | , ^ , << , >> ++ (prefix/postfix) / -- (prefix/postfix) sizeof static_cast / dynamic_cast / reinterpret_cast / const_cast c style casts new / new[] / delete / delete[] all of the member

In C++ what does it mean for a compiler to “inline” a function object?

扶醉桌前 提交于 2019-12-03 02:03:22
In the wikipedia article about function objects it says such objects have performance advantages when used with for_each because the compiler can "inline" them. I'm a bit foggy on exactly what this means in this context... or any context I'm embarrassed to say. Thanks for any help! The last parameter of for_each template is a functor . Functor is something that can be "called" using the () operator (possibly with arguments). By defintion, there are two distinctive kinds of functors: Ordinary non-member functions are functors. Objects of class type with overloaded () operator (so called

Would you please explain OCaml functors to me? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-03 01:49:21
Possible Duplicate: In Functional Programming, what is a functor? I don't know much about OCaml, I've studied F# for some time and quite understand it. They say that F# misses functor model, which is present in OCaml. I've tried to figure out what exactly functor is, but wikipedia and tutorials didn't help me much. Could you please illuminate that mystery for me? Thanks in advance :) EDIT: I've caught the point, thx to everyone who helped me. You can close the question as exact duplicate of: In Functional Programming, what is a functor? If you come from an OOP universe, then it probably helps