functor

How can it be useful to overload the “function call” operator?

百般思念 提交于 2019-11-26 16:06:43
问题 I recently discovered that in C++ you can overload the "function call" operator, in a strange way in which you have to write two pair of parenthesis to do so: class A { int n; public: void operator ()() const; }; And then use it this way: A a; a(); When is this useful? 回答1: This can be used to create "functors", objects that act like functions: class Multiplier { public: Multiplier(int m): multiplier(m) {} int operator()(int x) { return multiplier * x; } private: int multiplier; }; Multiplier

An example of a Foldable which is not a Functor (or not Traversable)?

我只是一个虾纸丫 提交于 2019-11-26 15:44:38
问题 A Foldable instance is likely to be some sort of container, and so is likely to be a Functor as well. Indeed, this says A Foldable type is also a container (although the class does not technically require Functor , interesting Foldable s are all Functor s). So is there an example of a Foldable which is not naturally a Functor or a Traversable ? (which perhaps the Haskell wiki page missed :-) ) 回答1: Here's a fully parametric example: data Weird a = Weird a (a -> a) instance Foldable Weird

passing functor as function pointer

走远了吗. 提交于 2019-11-26 13:04:55
问题 I\'m trying to use a C library in a C++ app and have found my self in the following situation (I know my C, but I\'m fairly new to C++). On the C side I have a collection of functions that takes a function pointer as their argument. On the C++ side I have objects with a functor which has the same signature as the function pointer needed by the C function. Is there any way to use the C++ functor as a function pointer to pass to the C function? 回答1: You cannot directly pass a pointer to a C++

Help with understanding a function object or functor in Java

和自甴很熟 提交于 2019-11-26 12:09:16
问题 Can someone explain what a functor is and provide a simple example? 回答1: A function object is just that. Something which is both an object and a function. Aside: calling a function object a "functor" is a serious abuse of the term: a different kind of "functors" are a central concept in mathematics, and one that has a direct role in computer science (see "Haskell Functors"). The term is also used in a slightly different way in ML, so unless you are implementing one of these concepts in Java

F# changes to OCaml [closed]

▼魔方 西西 提交于 2019-11-26 11:48:01
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . F# is derived from OCaml, but what major items are missing or added? Specifically I\'m curious as to whether the resources available for learning OCaml are also useful to someone who wants to learn F#. 回答1: The main differences are that F# does not support: functors OCaml-style objects polymorphic variants the

demote boost::function to a plain function pointer

与世无争的帅哥 提交于 2019-11-26 11:21:29
want to pass boost::bind to a method expecting a plain function pointer (same signature). typedef void TriggerProc_type(Variable*,void*); void InitVariable(TriggerProc_type *proc); boost::function<void (Variable*, void*)> triggerProc ... InitVariable(triggerProc); error C2664: 'InitVariable' : cannot convert parameter 1 from 'boost::function<Signature>' to 'void (__cdecl *)(type *,void *)' I can avoid storing a boost::function and just pass the bound functor directly, but then I get similar error: error C2664: 'blah(void (__cdecl *)(type *,void *))' : cannot convert parameter 1 from 'boost::

Why are Promises Monads?

天大地大妈咪最大 提交于 2019-11-26 11:02:28
问题 I\'ve been learning about functional programming and have come across Monads, Functors and Applicatives. From my understanding the following definitions apply: a) ( A=>B ) => C[A] => C[B] | Functor b) ( A=>C[B] ) => C[A] => C[B] | Monad c) ( C[A=>B] ) => C[A] => C[B] | Applicative (reference: https://thedet.wordpress.com/2012/04/28/functors-monads-applicatives-can-be-so-simple/) Furthermore, I understand a Monad is a special case of a Functor. As in, it applies a function that returns a

In Functional Programming, what is a functor?

▼魔方 西西 提交于 2019-11-26 10:04:36
问题 I\'ve come across the term \'Functor\' a few times while reading various articles on functional programming, but the authors typically assume the reader already understands the term. Looking around on the web has provided either excessively technical descriptions (see the Wikipedia article) or incredibly vague descriptions (see the section on Functors at this ocaml-tutorial website). Can someone kindly define the term, explain its use, and perhaps provide an example of how Functors are

Why can&#39;t I define a function inside another function?

泪湿孤枕 提交于 2019-11-26 09:34:58
问题 This is not a lambda function question, I know that I can assign a lambda to a variable. What\'s the point of allowing us to declare, but not define a function inside code? For example: #include <iostream> int main() { // This is illegal // int one(int bar) { return 13 + bar; } // This is legal, but why would I want this? int two(int bar); // This gets the job done but man it\'s complicated class three{ int m_iBar; public: three(int bar):m_iBar(13 + bar){} operator int(){return m_iBar;} };

How do functors work in haskell?

妖精的绣舞 提交于 2019-11-26 06:15:07
问题 I\'m trying to learn Haskell and I\'m through all the basics. But now I\'m stuck, trying to get my head around functors. I\'ve read that \"A functor transforms one category into another category\". What does this mean? I know it\'s a lot to ask, but could anyone give me a plain english explanation of functors or maybe a simple use case ? 回答1: A fuzzy explanation would be that a Functor is some sort of container and an associated function fmap that allows you to alter whatever is contained,