functor

What is the optimal way to use additional data fields in functors in Thrust?

烈酒焚心 提交于 2019-12-12 02:57:27
问题 What is the proper (or optimal) way to use some constant data in functors used in thrust algorithms like thrust::transform ? The naive way I used was simply allocate required arrays inside the functor's operator() method, like this: struct my_functor { __host__ __device__ float operator()(thrust::tuple<float, float> args) { float A[2][10] = { { 4.0, 1.0, 8.0, 6.0, 3.0, 2.0, 5.0, 8.0, 6.0, 7.0 }, { 4.0, 1.0, 8.0, 6.0, 7.0, 9.0, 5.0, 1.0, 2.0, 3.6 }}; float x1 = thrust::get<0>(args); float x2 =

Using a functor in find_if [closed]

那年仲夏 提交于 2019-12-11 22:59:25
问题 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 last year . I was wondering how would I use a template functor as an argument for find_if. I'm not sure about the syntax. For example, suppose a functor that deletes a product from a multimap of products. To do that, I have to "scan" the multimap, find the product (using my equal functor) and delete it. Here's my 'equal'

std::sort functor one line

瘦欲@ 提交于 2019-12-11 10:24:29
问题 I have declared a functor and made a call so std::sort with that functor as a parameter. Code: struct { bool operator() (const CString& item1, const CString& item2){ return MyClass::Compare( Order(_T("DESC")), item1, item2); } }Comparer; std::sort(AllObjects.GetData(), AllObjects.GetData() + AllObjects.GetSize(), Comparer); Simple question: can I do this in one line? 回答1: If your compiler supports c++11, you can use a lambda std::sort(AllObjects.GetData(), AllObjects.GetData() + AllObjects

Template function accepting callable functors with X parameters

こ雲淡風輕ζ 提交于 2019-12-11 07:35:50
问题 I'm writing a hosted C++ program that runs user-written C-code compiled on the fly. It's absolutely vital that certain typical exceptions are caught from the C-code and processed/ignored. To do this, I'm calling the C code from within a structured exception handling block. Due to the nature and semantics of this block (and where it's called from), I've separated the actual calling to it's own function: template <typename ret_type, class func> static ret_type Cstate::RunProtectedCode(func

fmap print value doesn't print anything

谁说胖子不能爱 提交于 2019-12-11 06:45:07
问题 Why does the following doesn't print anything: λ> fmap print (pure 2) Whereas something like this works: λ> fmap id (pure 2) 2 回答1: Follow the types: fmap print (pure 2) :: Applicative f => f (IO ()) fmap id (pure 2) :: (Num b, Applicative f) => f b Lets replace f with IO : fmap print (pure 2) :: IO (IO ()) -- #1 fmap id (pure 2) :: (Num b) => IO b -- #2 Now you can clearly see that #2 is an action with a numeric result, whereas #1 is an action with another action as a result. Also, GHCi has

Why is a -> a not a functor?

一笑奈何 提交于 2019-12-11 06:43:42
问题 Specifically referring to https://bartoszmilewski.com/2015/04/07/natural-transformations/ Author says "This is not a functor". I can define fmap :: (a -> b) -> (a -> a) -> (b -> b) as fmap f aa = id , which seems to adhere to the functor laws. I don't mean why it's not explicitly part of the Functor typeclass in X language, I just mean why it wouldn't be acknowledged as a functor. 回答1: In the context of Haskell, I think you're talking about newtype Endo a = Endo (a -> a) (using a newtype to

Function wrapper via (function object) class (variadic) template

两盒软妹~` 提交于 2019-12-11 05:54:50
问题 C++ I'm trying to implement a function wrapper via a (function object) class (variadic) template. The class has as its only data member a function pointer that is initialized by or assigned the function pointer it is wrapping. The parametrized constructor takes a function pointer and initializes the member by it. The operator() method takes argument(s) (or none) and calls the wrapped function with them. At least that's the idea. I get many errors, which I mark with comments. VC11 (with the

Functor fmap, pattern match function values, haskell

社会主义新天地 提交于 2019-12-11 05:29:14
问题 I have the following type, and would like to make it a Functor: newtype SubsM a = SubsM {runSubsM :: Context -> Either Error (a, Env)} So far i got this instance Functor SubsM where fmap f (SubsM a) = SubsM (\s->(Right((f a),(fst s)))) I get an error because a is not the expected type, my question is how do i pattern match a on the left-hand side? 回答1: You can pattern match on the Either Error (a, Env) with case : instance Functor SubsM where fmap f (SubsM cf) = SubsM $ \c -> case (cf c) of

stl remove_if with class member function result

天大地大妈咪最大 提交于 2019-12-11 04:57:36
问题 I have a object container, list; and class Foo have a member function id() return an integer identifier. Now I want to use stl algorithm remove_if to remove some objects whose id is less than a value. I don't want to provide a function for id compare , If it is possible for me to write one line code with STL but boost to implement it. class Foo{ public: unsigned id() const {return id_;} ... private: unsigned id_ ... }; list<Foo> foo_list; std::remove_if(foo_list.begin(), foo_list.end(), ???);

Can I Write Relational Operators in Terms of Arithmetic Operations?

佐手、 提交于 2019-12-11 01:56:01
问题 So I have a fairly complex function: template <typename T> void foo(const int param1, const int param2, int& out_param) Given int bar , const int arg1 , and const int arg2 the function will be called with either: foo<plus<int>>(arg1, arg2, bar) or foo<minus<int>>(arg1, arg2, bar) Internally the function is rather complex but I am doing different relational operators based on the type of functor that was passed as a template parameter. In the case of plus I need to do: arg1 > arg2 bar > 0 bar