functor

how to find duplicates in std::vector<string> and return a list of them?

走远了吗. 提交于 2019-12-30 17:27:07
问题 So if I have a vector of words like: Vec1 = "words", "words", "are", "fun", "fun" resulting list: "fun", "words" I am trying to determine which words are duplicated, and return an alphabetized vector of 1 copy of them. My problem is that I don't even know where to start, the only thing close to it I found was std::unique_copy which doesn't exactly do what I need. And specifically, I am inputting a std::vector<std::string> but outputting a std::list<std::string> . And if needed, I can use

Perform argument substitution on nested boost::bind without composition

若如初见. 提交于 2019-12-30 05:16:30
问题 Suppose I have a function which takes a nullary functor as an argument: void enqueue( boost::function<void()> & functor ); I have another function which takes an int and does something internally: void foo( int a); I would like to nest, but not compose, these together so that I get a functor with the signature: boost::function<void(int)> functor Which when called with a value - say 4 - performs the following: enqueue( boost::bind(&foo, 4) ) My first attempt was the following: boost::function

What's the relationship between profunctors and arrows?

戏子无情 提交于 2019-12-29 19:58:50
问题 Apparently, every Arrow is a Strong profunctor. Indeed ^>> and >>^ correspond to lmap and rmap . And first' and second' are just the same as first and second . Similarly every ArrowChoice is also Choice. What profunctors lack compared to arrows is the ability to compose them. If we add composition, will we get an arrow? In other words, if a (strong) profunctor is also a category, is it already an arrow? If not, what's missing? 回答1: What profunctors lack compared to arrows is the ability to

Serialize C++ functor

徘徊边缘 提交于 2019-12-29 09:06:13
问题 Can you save the function body of a C++ lambda/functor? For example, say you have light0->lightFunction = []( real tEl, real pAz ) -> Vector { return Vector( // red is up lobe std::max<real>( 0., 5*cos(tEl)-4 ), // green lower lobe std::max<real>( 0., -4*sin(tEl-PI)*cos(pAz-2.5)-3), 0. ) ; } ; And you want to save the function body, so you can load it later (instead of always having to hard code it). Can you do it? 回答1: This lambda doesn't have state (not a closure), so it's an ordinary

Making (a, a) a Functor

主宰稳场 提交于 2019-12-29 04:13:27
问题 How can I make (a, a) a Functor without resorting to a newtype ? Basically I want it to work like this: instance Functor (a, a) where fmap f (x, y) = (f x, f y) But of course that's not a legal way to express it: Kind mis-match The first argument of `Functor' should have kind `* -> *', but `(a, a)' has kind `*' In the instance declaration for `Functor (a, a)' What I really want is a type-level function like this: \a -> (a, a) (invalid syntax). So a type alias, perhaps? type V2 a = (a, a)

Why does the 2-tuple Functor instance only apply the function to the second element?

北战南征 提交于 2019-12-28 05:57:22
问题 import Control.Applicative main = print $ fmap (*2) (1,2) produces (1,4) . I would expect it it to produce (2,4) but instead the function is applied only to the second element of the tuple. Update I've basically figured this out almost straight away. I'll post my own answer in a minute.. 回答1: Let me answer this with a question: Which output do you expect for: main = print $ fmap (*2) ("funny",2) You can have something as you want (using data Pair a = Pair a a or so), but as (,) may have

reference_wrapper Referencing Primitive

别等时光非礼了梦想. 提交于 2019-12-25 08:15:53
问题 I was under the impression that I could use reference_wrapper to generate a functor that would return the object passed into the reference_wrapper ctor. But this isn't working. Am I doing it wrong? If so is there a better way to accomplish this? I can write a lambda, it just seems like I shouldn't have to. #include <iostream> #include <functional> using namespace std; void funPtrPrinter( function< int( void ) > output ) { cout << output() << endl; } int main( void ) { int thirteen = 13; auto

TR1 function multicast

╄→尐↘猪︶ㄣ 提交于 2019-12-25 05:19:11
问题 How would you implement multicast for TR1 functors? I have my callback slots implemented like void setCallback(std::tr1::function<void (std::string)> cb) { this->callback = cb; } but need to pass more than one callback in one of them. I don't want to go into more complex solutions like observer, as this is the only case I need multicast so far. I also cannot use Boost.Signals (as suggested here), because I cannot use Boost. I don't need to explicitly handle disabling callback when subscriber

Thread and interfaces C++

[亡魂溺海] 提交于 2019-12-24 21:33:30
问题 I have some issue to create different threads using interfaces and factory: I have two interfaces that are derived (here by one class but eventually more..). I use a factory to create an object of the desired derived class. As I run them within different threads, I used what the factories return me to give as parameter of the thread constructor. #include <iostream> #include <thread> class Base { public: virtual ~Base () {} virtual void operator () () = 0; }; class Derived : public Base {

Functor instance for Data.AVL

≡放荡痞女 提交于 2019-12-24 13:13:15
问题 I would like to define a functor instance for Data.AVL.Indexed.Tree . This seems to be tricky because of the way the type Key⁺ of the indices storing the upper and lower bounds of the tree "depend on" the type (or family of types) of the values in the tree. What I want to do is implement fmap below: open import Relation.Binary open import Relation.Binary.PropositionalEquality module Temp {k ℓ} {Key : Set k} {_<_ : Rel Key ℓ} (isStrictTotalOrder : IsStrictTotalOrder _≡_ _<_) where open import