functor

Using non-const expression as template parameter

非 Y 不嫁゛ 提交于 2019-12-06 02:35:43
This is a follow up on How do I get the argument types of a function pointer in a variadic template class? I have this struct to access the arguments of a variadic template: template<typename T> struct function_traits; template<typename R, typename ...Args> struct function_traits<std::function<R(Args...)>> { static const size_t nargs = sizeof...(Args); typedef R result_type; template <size_t i> struct arg { typedef typename std::tuple_element<i, std::tuple<Args...>>::type type; }; }; And I access the type of an argument of Args with typedef function<void(Args...)> fun; std::cout << std::is

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

╄→гoц情女王★ 提交于 2019-12-06 02:12:24
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? Very confused, need your advice. Thanks. It helps to know a little category theory. A category is just a

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

南楼画角 提交于 2019-12-06 00:20:50
This question already has an answer here: Functors when should I use them whats their intended use [closed] 5 answers 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. 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 for you. These generated classes (for delegates) are derived from MulticastDelegate which in turn derives from Delegate . In short,

Passing a complex function variants as arguments

帅比萌擦擦* 提交于 2019-12-05 21:59:55
Say I have the following template function: template <class T> void apply(const vector<complex<T> >& in, vector<T>& out, T (*f)(complex<T>)) { out.resize(in.size()); for(size_t i = 0; i < in.size(); ++i) out[i] = f(in[i]); } You can see, I just want to apply a function to a vector of complex data, and store the results into a vector of real data. I figure this should be good for a whole list of function: abs, norm, real, imag, etc. My problem is, how do I pass a function in? I have tried variants of apply(in, out, abs) supplying different templates to abs with no luck. I am pretty sure the

What does Haskell call the Hom Functor/Monad?

心不动则不痛 提交于 2019-12-05 16:34:45
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 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 by importing Control.Monad.Reader or Control.Monad.Instances . 来源: https://stackoverflow.com/questions

Template functor cannot deduce reference type

我只是一个虾纸丫 提交于 2019-12-05 16:25:54
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 error here return 0; } How can I get deduction to work? You can invoke the function like this: f<int&>(i,

Template functors vs functions

僤鯓⒐⒋嵵緔 提交于 2019-12-05 16:08:12
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? There are two main reasons: The first is, as pythonic metaphor noted, partial specialization is only valid for

How to show that a monad is a functor and an applicative functor?

旧城冷巷雨未停 提交于 2019-12-05 11:52:04
问题 Monads are known to be theoretically a subset of functors and specifically applicative functors, even though it's not indicated in Haskell's type system. Knowing that, given a monad and basing on return and bind , how to: derive fmap , derive <*> ? 回答1: Well, fmap is just (a -> b) -> f a -> f b , i.e. we want to transform the monadic action's result with a pure function. That's easy to write with do notation: fmap f m = do a <- m return (f a) or, written "raw": fmap f m = m >>= \a -> return

Relationship between fmap and bind

只愿长相守 提交于 2019-12-05 10:23:51
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? 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 = liftM f x = do { v <- x; return (f v) } = x >>= return . f duplode Functor instances are unique , in the

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

折月煮酒 提交于 2019-12-05 10:08: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> #include <iostream> struct A { int i = 0; bool operator()(int a, int b) { ++i; return a < b; } }; int