functor

Functor instance for generic polymorphic ADTs in Haskell?

烈酒焚心 提交于 2019-12-10 12:38:42
问题 When it comes to applying category theory for generic programming Haskell does a very good job, for instance with libraries like recursion-schemes . However one thing I'm not sure of is how to create a generic functor instance for polymorphic types. If you have a polymorphic type, like a List or a Tree, you can create a functor from (Hask × Hask) to Hask that represents them. For example: data ListF a b = NilF | ConsF a b -- L(A,B) = 1+A×B data TreeF a b = EmptyF | NodeF a b b -- T(A,B) = 1+A

Const and non-const functors

你离开我真会死。 提交于 2019-12-10 03:14:43
问题 This seems like something that ought to be frequently asked and answered, but my search-fu has failed me. I'm writing a function which I want to accept a generic callable object of some kind (including bare function, hand-rolled functor object, bind , or std::function ) and then invoke it within the depths of an algorithm (ie. a lambda). The function is currently declared like this: template<typename T, typename F> size_t do_something(const T& a, const F& f) { T internal_value(a); // do some

Functors in Ocaml

时光毁灭记忆、已成空白 提交于 2019-12-10 01:21:17
问题 I am having a bit of a problem with a functor (and it's resultant type). Below, I have a Set functor that uses an Ordered type. I actually used the set.ml that comes with ocaml for some guidance, but I seem to be doing everything ahhem right. I created an Ordered module with integers and applied it to the Set functor to get the last module on this code sample, IntSet. The next line fails, when I try to insert an integer. I get the following type error: Error: This expression has type int but

Must I implement Applicative and Functor to implement a Monad

三世轮回 提交于 2019-12-09 17:21:34
问题 I'm trying to implement a Monad instance. As a simpler example, assume the following: data Maybee a = Notheeng | Juust a instance Monad Maybee where return x = Juust x Notheeng >>= f = Notheeng Juust x >>= f = f x fail _ = Notheeng This should be the standard implementation of Maybe as far as I know. However, this doesn't compile, because the compiler complains: No instance for (Applicative Maybee) and similarly he wants a Functor instance once the Applicative is given. So: Simple question:

Using STL algorithms, is it better to pass a function pointer or a functor?

拥有回忆 提交于 2019-12-09 15:25:17
问题 Which of these 2 methods is better and why? Method 1: void fun(int i) { //do stuff } ... for_each(a.begin(), a.end(), fun); Method 2: class functor { public: void operator()(int i); }; ... for_each(a.begin(), a.end(), functor()); Edit: Should have formulated it this way, in what situation is one of the above method preferable to the other? Thanks a lot! 回答1: Functors may (and will ) be trivially inlined – this isn't done for regular function pointers. Thus, functors have a real performance

What is a good data structure to represent an undirected graph?

一曲冷凌霜 提交于 2019-12-09 11:49:50
问题 I need to construct an undirected graph. I don't need it to do anything too fancy, but ideally it would work like this: structure UDG = UndirectedGraph val g = UDG.empty val g = UDG.addEdges(g, n1, [n2, n4, n7]) (* n1 is connected to n2, n4, and n7 *) val g = UDG.addEdge(g, n2, n3) UDG.connected(g, n2) (* returns [n1, n3] *) Is there a good data structure in SML/NJ to model these relationships? Should I just roll my own? Updates I've gone ahead and tried rolling my own, but I get a type

Contaner for different functions?

泄露秘密 提交于 2019-12-09 09:12:02
问题 I'm trying to implement a container class for different functions where I can hold function pointers and use it to call those functions later. I'll try to discribe my problem more accurate. As example, I have 2 different test functions: int func1(int a, int b) { printf("func1 works! %i %i\n", a, b); return 0; } void func2(double a, double b) { printf("func2 works! %.2lf %.2lf\n", a, b); } and I also have array of variants, which holds function arguments: std::vector<boost::variant<int, double

Use cases for adjunctions in Haskell

匆匆过客 提交于 2019-12-09 05:21:53
问题 I have been reading up on adjunctions during the last couple of days. While I start to understand their importance from a theoretical point of view, I wonder how and why people use them in Haskell. Data.Functor.Adjunction provides an implementation and among its instances are free functor / forgetful functor and curry / uncurry. Again those are very interesting from the theoretical view point but I can't see how I would use them for more practical programming problems. Are there examples of

HowTo sort std::map?

99封情书 提交于 2019-12-09 03:38:36
问题 Here is my map: typedef std::map<int/*security id*/, PositionMonth> PortfolioMonth; where PositionMonth is a structure, ex.: struct PositionMonth { Nav::Shares shares_; Nav::Amount market_value_; PositionMonth(void) {} PositionMonth(const Nav::Amount& market_value , const Nav::Shares& shares) : market_value_(market_value) , shares_(shares) {} }; Question : how to sort std::map by second value key param (by market_value_ , let it be int)? Examples or links? PS. Boost methods not interested!

Haskell fmap functor

≯℡__Kan透↙ 提交于 2019-12-09 01:15:29
I have a problem with functors over queue based on designated algebraic datastructures. data DQueue a = Empty | Enqueue a (DQueue a) deriving (Eq, Show, Read) instance Functor DQueue where fmap f (Enqueue x xs) = Enqueue (f x) $ fmap f xs instance Foldable DQueue where foldr = error "not done" sample1 :: DQueue Int sample1 = Enqueue 5 $ Enqueue 7 $ Enqueue 9 Empty and result should be like that: fmap (+1) sample1 ~?= Enqueue 6 (Enqueue 8 (Enqueue 10 Empty)) foldr (+) 0 sample1 ~?= 24 fmap seems to be logicaly correct but I get an error: Non-exhaustive patterns in function fmap Thank you in