functor

What's the difference between a function and a functor in Haskell? Only definition?

房东的猫 提交于 2019-12-07 16:48:04
问题 In Haskell, when writing a function, it means we map something(input) to another thing(output). I tried LYAH to understand the definition of Functor: seems just the same like a normal Functor. Is there any restriction that a function could be called a Functor? Is Functor allowed to have I/O or any other side effect? If in Haskell, "everthing is a function", then what's the point of introducing the "Functor" concept? A restricted version of function, or an enhancement version of a function?

Do functors have an equivalent in C#? [duplicate]

扶醉桌前 提交于 2019-12-07 14:00:10
问题 This question already has answers here : Functors when should I use them whats their intended use [closed] (5 answers) Closed 6 years ago . Is there an equivalent to Functors in C#? C# has Func<,>, delegates and anonymous methods but aren't all of these pointers to a method? The C++ Functor is a class and not a pointer to a method. 回答1: C# has Func<,>, delegates and anonymous methods but aren't all of these pointers to a method? No. Even C# delegates are classes, implemented by the compiler

What does Haskell call the Hom Functor/Monad?

谁都会走 提交于 2019-12-07 11:05:07
问题 I'd like to use it in my code and would rather not duplicate it, but since it involves only massively generic words like "function" or "composition" I can't find it by searching. To be completely specific, I'm looking for instance Functor (x->) where fmap f p = f . p 回答1: This is the basic reader (or environment) monad, usually referred to as ((->) e) . (This is (e ->) written as a partially applied function instead of as a section; the latter syntax is problematic to parse.) You can get it

Template functor cannot deduce reference type

拟墨画扇 提交于 2019-12-07 08:35:44
问题 I've got a functor f, which takes a function func and a parameter t of the same type as func. I cannot pass g to f because of compilation error (no matching function for call to f(int&, void (&)(int&)) ). If g would take non-reference parameter g(int s), compilation finishes. Or if I manually specify template parameter f<int&>(i, g) , compilation also finishes. template<typename T> void f(T t, void (*func)(T)) {} void g(int& s) {} int main(int, char*[]) { int i = 7; f(i, g); // compilation

Template functors vs functions

荒凉一梦 提交于 2019-12-07 08:28:47
问题 I have been looking at some of the Boost source code and noticed they implement templated functions by using a functor instead of a plain function? Is there a reason for this? For example: template<typename Foo, typename Bar> struct functor { Bar operator()(const Foo& foo) { return foo.as_bar(); } }; as opposed to: template<typename Foo, typename Bar> Bar func(const Foo& foo) { return foo.as_bar(); } The only advantage I can come up with is it allows classes to inherit the function? 回答1:

Relationship between fmap and bind

梦想与她 提交于 2019-12-07 06:10:45
问题 After looking up the Control.Monad documentation, I'm confused about this passage: The above laws imply: fmap f xs = xs >>= return . f How do they imply that? 回答1: Control.Applicative says As a consequence of these laws, the Functor instance for f will satisfy fmap f x = pure f <*> x The relationship between Applicative and Monad says pure = return (<*>) = ap ap says return f `ap` x1 `ap` ... `ap` xn is equivalent to liftMn f x1 x2 ... xn Therefore fmap f x = pure f <*> x = return f `ap` x =

Is this legal to avoid set from creating actual copies of Comparator object

亡梦爱人 提交于 2019-12-07 05:42:29
问题 In such a code: Comparator comp(3); set<string, Comparator> s1(comp); set<string, Comparator> s2(comp); set<string, Comparator> s3(comp); set<string, Comparator> s4(comp); the actual instance of the Comparator (namely comp) is copied at each creation of a set object as the cpp reference states The container keeps an internal copy of alloc and comp, which are used to allocate storage and to sort the elements throughout its lifetime. So we were wondering if this is legal in C++ #include <set>

Error: passing const xxx as this argument of xxx discards qualifiers

 ̄綄美尐妖づ 提交于 2019-12-07 04:04:17
问题 I'm having an issue porting my functor from windows to linux. (a functor to pass to stl::map for strict-weak ordering) The original is as follows: struct stringCompare{ // Utilized as a functor for stl::map parameter for strings bool operator() (string lhs, string rhs){ // Returns true if lhs < rhs if(_stricmp(lhs.c_str(), rhs.c_str()) < 0) return true; else return false; } }; As linux doesnt support _stricmp but uses strcasecmp instead, I changed it to: struct stringCompare{ bool operator()

Is There a Indirection Functor?

别说谁变了你拦得住时间么 提交于 2019-12-07 00:28:27
问题 I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist. So given the code: const auto vals = { 0, 1, 2, 3 }; vector<const int*> test(size(vals), nullptr); iota(begin(test), end(test), data(vals)); transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; }); Live Example I was hoping that there was a functor that I could use instead of

Detecting function object (functor) and lambda traits

拜拜、爱过 提交于 2019-12-06 22:15:49
问题 How can I detect the return type and parameter types of nullary and unary function pointers, std::function objects, and functors (including lambdas)? Boost's function_traits and functional traits don't quite get me there out of the box, but I'm open to supplementing or replacing them. I could do something like this: namespace nsDetail { class Dummy { Dummy(); }; } template<class Fn> struct FnTraits; template<class R> struct FnTraits<R(*)()> { typedef nsDetail::Dummy ParamType; typedef R