functor

Generating functors with iterator behavior

戏子无情 提交于 2021-02-17 15:47:07
问题 I have a question, which very likely has been asked like this before, because I think what I want is something that a considerable amount of people would want. However I could not come up with any way of expressing it that would return what I wanted in search (not google, not here). So maybe the answer here is just a single term used to describe what I mean. What I want to implement is something that roughly does the following: It can take a functor struct/class and generate a sequence of

Returning a derived class of a virtual class in C++

断了今生、忘了曾经 提交于 2021-02-11 12:58:52
问题 here's my problem. I have a template abstract class RandomVariable with pure virtual function operator()() template<T> class RandomVariable<T> { public: virtual T operator()() = 0; /* other stuff */ protected: T value; } I also have a template abstract class Process with pure virtual function operator()() template<T> class Process<T> { public: typedef std::pair<double, T> state; typedef std::list<state> result_type; virtual result_type operator()() = 0; /* other stuff */ protected: result

Returning a derived class of a virtual class in C++

我的梦境 提交于 2021-02-11 12:58:07
问题 here's my problem. I have a template abstract class RandomVariable with pure virtual function operator()() template<T> class RandomVariable<T> { public: virtual T operator()() = 0; /* other stuff */ protected: T value; } I also have a template abstract class Process with pure virtual function operator()() template<T> class Process<T> { public: typedef std::pair<double, T> state; typedef std::list<state> result_type; virtual result_type operator()() = 0; /* other stuff */ protected: result

Convert complex term to List of lists and then back to term with modified functor

落花浮王杯 提交于 2021-02-11 12:28:50
问题 you can use =.. to convert simple terms. ?- x(a,b,c) =.. A. A = [x, a, b, c]. what about complex terms : x(a,b(c,d)) ==> [x,a,[b,c,d]] x(a,b(c,q(d))) ==> [x,a,[b,c,[q,d]]] then as separate task i want to re-generate the terms with changed functor : x(a,b(c,d)) ==> [x,a,[b,c,d]] ==> y(a,f(c,d)) 回答1: deep_univ(X, Y) :- X =.. [H|Rest], ( Rest = [] -> Y = X ; maplist(deep_univ, Rest, ExpRest), Y=[H|ExpRest] ). rev_univ([H|Rest], Y) :- maplist(rev_univ, Rest, RestT), Y =.. [H|RestT]. rev_univ(H, H

Convert complex term to List of lists and then back to term with modified functor

不羁岁月 提交于 2021-02-11 12:27:35
问题 you can use =.. to convert simple terms. ?- x(a,b,c) =.. A. A = [x, a, b, c]. what about complex terms : x(a,b(c,d)) ==> [x,a,[b,c,d]] x(a,b(c,q(d))) ==> [x,a,[b,c,[q,d]]] then as separate task i want to re-generate the terms with changed functor : x(a,b(c,d)) ==> [x,a,[b,c,d]] ==> y(a,f(c,d)) 回答1: deep_univ(X, Y) :- X =.. [H|Rest], ( Rest = [] -> Y = X ; maplist(deep_univ, Rest, ExpRest), Y=[H|ExpRest] ). rev_univ([H|Rest], Y) :- maplist(rev_univ, Rest, RestT), Y =.. [H|RestT]. rev_univ(H, H

Combining predicates in a functional way

扶醉桌前 提交于 2021-02-10 17:53:14
问题 Does Boost Hana provide a way to combine predicates with logical operators? I'm referring to something roughly like this constexpr auto both = [](auto&& f, auto&& g){ return [&f,&g](auto&& x){ return f(x) && g(x); }; }; that could be used like this: int main() { std::vector<int> v{1,2,3,4,5,6,7,8,9,10}; auto less_than_7 = hana::reverse_partial(std::less_equal<int>{}, 7); auto more_than_3 = hana::reverse_partial(std::greater_equal<int>{}, 3); auto r = ranges::views::remove_if(v, both(less_than

How do I make a functor out of an arbitrary function?

为君一笑 提交于 2021-02-10 16:21:16
问题 I have a bunch of functions which I want to use as functors (that is, use types instead of pass pointers to the functions, or any other kind of data). Is there an elegant/idiomatic/standard way of doing this with the standard library, or the standard library + Boost? Perhaps using bind() somehow? Or should I go with something simplistic (well, kind of simplistic) such as: template<typename Function, Function& F, typename... Parameters> struct functor { using function_type = Function; using

functors from partially applied function type

微笑、不失礼 提交于 2021-02-10 10:10:34
问题 there is a quesion in Programming in Haskell that says: Complete the following declaration: instance Functor ((->) a) where Now as Functor Thing has a type definition of: instance Functor Thing where --fmap::(a -> b) -> Thing a -> Thing b I was wondering if this reduction makes sense: instance Functor ((->) a) where -- fmap::(a -> b) -> ((->) a) a -> ((->) a) b -- therefore -- fmap::(a -> b) -> a -> a -> (a -> b) -- therefore -- fmap::b -> b -- update --- I missed brackets, it should have

Haskell: Flaw in the description of applicative functor laws in the hackage Control.Applicative article?: it says Applicative determines Functor

烂漫一生 提交于 2021-02-08 12:30:49
问题 I think I found a flaw in the hackage article for Control.Applicative. As a description of the applicative functor laws, it says: class Functor f => Applicative f where A functor with application, providing operations to embed pure expressions ( pure ), and sequence computations and combine their results ( <*> ). A minimal complete definition must include implementations of these functions satisfying the following laws: identity pure id <*> v = v composition pure (.) <*> u <*> v <*> w = u <*>

Understanding functor laws: is this a functor?

断了今生、忘了曾经 提交于 2021-02-08 08:48:18
问题 The following code is written in javascript. This question involves an attempt to dive into some category theory, maybe a haskeller or someone more familiar with the mathematical aspects of this question can help me out? I'm trying to wrap my head around the idea that a functor is a mapping between categories that preserves structure. More specifically - according to my understanding - a functor in a programming language is an endofunctor. By this it is meant that functors in programming