functor

C++ std::transform vector of pairs->first to new vector

喜你入骨 提交于 2019-12-18 12:52:06
问题 Sorry for a little bit beginner question. There are vector and vector of pairs typedef std::vector <int> TItems; typedef std::vector < std::pair <int, int> > TPairs; Is there any way to transform all first items in pair to another vector in one step int main () { TItems items; TPairs pairs; pairs.push_back (std::make_pair(1,3)); pairs.push_back (std::make_pair(5,7)); std::transform( items.begin(), items.end(), items.begin(), comp ( &pairs ) ); return 0; } How to design a functor? class comp {

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

眉间皱痕 提交于 2019-12-18 11:51:33
问题 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

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

老子叫甜甜 提交于 2019-12-18 10:54:48
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 4 years ago . 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

Why do we have map, fmap and liftM?

≯℡__Kan透↙ 提交于 2019-12-18 10:14:56
问题 map :: (a -> b) -> [a] -> [b] fmap :: Functor f => (a -> b) -> f a -> f b liftM :: Monad m => (a -> b) -> m a -> m b Why do we have three different functions that do essentially the same thing? 回答1: map exists to simplify operations on lists and for historical reasons (see What's the point of map in Haskell, when there is fmap?). You might ask why we need a separate map function. Why not just do away with the current list-only map function, and rename fmap to map instead? Well, that’s a good

Why should I use applicative functors in functional programming?

我怕爱的太早我们不能终老 提交于 2019-12-18 10:09:10
问题 I'm new to Haskell, and I'm reading about functors and applicative functors. Ok, I understand functors and how I can use them, but I don't understand why applicative functors are useful and how I can use them in Haskell. Can you explain to me with a simple example why I need applicative functors? 回答1: Applicative functors are a construction that provides the midpoint between functors and monads, and are therefore more widespread than monads, while more useful than functors. Normally you can

Functor instance for a GADT with type constraint

时光毁灭记忆、已成空白 提交于 2019-12-18 04:08:06
问题 Today I wanted to investigate if it is possible to construct a data type in such a way, that it does not store the data of the type of its type signature, but another representation of it. So, here is my attempt of an GADT which has a type constructor of type a , but a data constructor of type ByteString . {-# LANGUAGE GADTs #-} import Data.ByteString.Char8 import Data.Serialize data Serialized a where MkSerialized :: (Serialize a) => ByteString -> Serialized a Now I can define a decode'

Where to define C++ class member template function and functors that instantiate it?

本小妞迷上赌 提交于 2019-12-18 03:39:08
问题 I have a class Foo which is used in a small standalone project. It has a class definition in Foo.h with the implementation for the class' member functions in an implementation file Foo.cpp. First question - one of the member functions of class Foo is a template method Foo::doSomething(), is it correct that the implementation of this method should appear with the declaration of the function in Foo.h ? The template parameter which Foo::doSomething() will be instantiated with is one of two

Accept any kind of callable and also know argument type

主宰稳场 提交于 2019-12-17 23:12:07
问题 I'm not sure if it's possible, so that's what I want to find out. I'd like to create a function which accepts any kind of functor/callable object, but I want to know what the argument type is. ( but not enforce it ) So, this one captures all but doesn't give me the type of the argument: template < typename T > void optionA( T ); This one captures most, and has the type of the argument template < typename T > void optionB( std::function< void(T) > ); But this one doesn't allow lambdas, so

Can ML functors be fully encoded in .NET (C#/F#)?

心不动则不痛 提交于 2019-12-17 21:56:50
问题 Can ML functors be practically expressed with .NET interfaces and generics? Is there an advanced ML functor use example that defies such encodings? Answers summary : In the general case, the answer is NO. ML modules provide features (such as specification sharing via signatures [1]) that do not directly map to .NET concepts. However, for certain use cases the ML idioms can be translated. These cases include not only the basic Set functor [2], but also the functorial encoding of monads [3],

Sets, Functors and Eq confusion

对着背影说爱祢 提交于 2019-12-17 17:30:04
问题 A discussion came up at work recently about Sets, which in Scala support the zip method and how this can lead to bugs, e.g. scala> val words = Set("one", "two", "three") scala> words zip (words map (_.length)) res1: Set[(java.lang.String, Int)] = Set((one,3), (two,5)) I think it's pretty clear that Set s shouldn't support a zip operation, since the elements are not ordered. However, it was suggested that the problem is that Set isn't really a functor, and shouldn't have a map method.