functor

Is every Alternative Monad Filterable?

谁说胖子不能爱 提交于 2020-04-10 08:06:08
问题 The category of sets is both cartesian monoidal and cocartesian monoidal. The types of the canonical isomorphisms witnessing these two monoidal structures are listed below: type x + y = Either x y type x × y = (x, y) data Iso a b = Iso { fwd :: a -> b, bwd :: b -> a } eassoc :: Iso ((x + y) + z) (x + (y + z)) elunit :: Iso (Void + x) x erunit :: Iso (x + Void) x tassoc :: Iso ((x × y) × z) (x × (y × z)) tlunit :: Iso (() × x) x trunit :: Iso (x × ()) x For the purposes of this question I

Is every Alternative Monad Filterable?

|▌冷眼眸甩不掉的悲伤 提交于 2020-04-10 08:05:27
问题 The category of sets is both cartesian monoidal and cocartesian monoidal. The types of the canonical isomorphisms witnessing these two monoidal structures are listed below: type x + y = Either x y type x × y = (x, y) data Iso a b = Iso { fwd :: a -> b, bwd :: b -> a } eassoc :: Iso ((x + y) + z) (x + (y + z)) elunit :: Iso (Void + x) x erunit :: Iso (x + Void) x tassoc :: Iso ((x × y) × z) (x × (y × z)) tlunit :: Iso (() × x) x trunit :: Iso (x × ()) x For the purposes of this question I

Are all fixed size containers strong monoidal functors, and/or vice versa?

北城余情 提交于 2020-03-21 11:38:05
问题 The Applicative typeclass represents lax monoidal functors that preserve the cartesian monoidal structure on the category of typed functions. In other words, given the canonical isomorphisms witnessing that (,) forms a monoidal structure: -- Implementations left to the motivated reader assoc_fwd :: ((a, b), c) -> (a, (b, c)) assoc_bwd :: (a, (b, c)) -> ((a, b), c) lunit_fwd :: ((), a) -> a lunit_bwd :: a -> ((), a) runit_fwd :: (a, ()) -> a runit_bwd :: a -> (a, ()) The typeclass and its laws

IDE thinks functor is a constructor?

被刻印的时光 ゝ 提交于 2020-01-25 06:56:10
问题 I am trying to write an implementation of primms algorithm for a DSA class. There are some nuances to make the project a little trickier (some points cannot be reached by others based on the 'terrain' at that location) so I made a functor to get distances (edge weights). My functor looks like this class primms_distance{ double operator ()(const primms_vertex &a, const primms_vertex &b){ //ommitted for University honor code purposes } }; However, I later do the following (again simplified for

In OCaml using Base, how do you construct a set with elements of type `int * int`?

陌路散爱 提交于 2020-01-24 17:09:12
问题 In F#, I'd simply do: > let x = Set.empty;; val x : Set<'a> when 'a : comparison > Set.add (2,3) x;; val it : Set<int * int> = set [(2, 3)] I understand that in OCaml, when using Base, I have to supply a module with comparison functions, e.g., if my element type was string let x = Set.empty (module String);; val x : (string, String.comparator_witness) Set.t = <abstr> Set.add x "foo";; - : (string, String.comparator_witness) Set.t = <abstr> But I don't know how to construct a module that has

In OCaml using Base, how do you construct a set with elements of type `int * int`?

一个人想着一个人 提交于 2020-01-24 17:08:29
问题 In F#, I'd simply do: > let x = Set.empty;; val x : Set<'a> when 'a : comparison > Set.add (2,3) x;; val it : Set<int * int> = set [(2, 3)] I understand that in OCaml, when using Base, I have to supply a module with comparison functions, e.g., if my element type was string let x = Set.empty (module String);; val x : (string, String.comparator_witness) Set.t = <abstr> Set.add x "foo";; - : (string, String.comparator_witness) Set.t = <abstr> But I don't know how to construct a module that has

can't initialize functor objects when passing derived class in C++

巧了我就是萌 提交于 2020-01-23 02:15:07
问题 This question stems from a previous question I asked here. I cannot use any external libraries or the C++ 11 spec. Meaning I can't use std::bind, std::function, boost::bind,boost::function etc. I have to write it myself. The issue is the following: Consider the code: EDIT Here is a complete program that exhibits the problem as requested: #include <map> #include <iostream> class Command { public: virtual void executeCommand() = 0; }; class Functor { public: virtual Command * operator()()=0; };

When are stateless class functors useful in place of a c style function?

若如初见. 提交于 2020-01-21 04:50:12
问题 I've found some good examples of functors on SO like this one, and all the convincing examples seem to use state in the class that defines operator() . I came across an example in a book that defines the function call operator without having state, and I can't help but feel like this is an awkward usage, and that a normal style function pointer, would be better than using operator() in every way here - less code, less variables (you have to instantiate the comparators), its probably more

confused about function as instance of Functor in haskell

試著忘記壹切 提交于 2020-01-20 02:17:06
问题 the type of fmap in Functor is: fmap :: Functor f => (a -> b) -> f a -> f b it looks like ,first apply function (a -> b) to the parameter of f a to create a result of type b, then apply f to it, and result is f b using Maybe a for example : fmap show (Just 1) result is : Just "1" same as saying: Just (show 1) but when (->) is used as a Functor (in Control.Monad.Instances) import Control.Monad.Instances (fmap show Just) 1 result is : "Just 1" that is, Just is apply first, then show is applied.

confused about function as instance of Functor in haskell

孤街醉人 提交于 2020-01-20 02:15:53
问题 the type of fmap in Functor is: fmap :: Functor f => (a -> b) -> f a -> f b it looks like ,first apply function (a -> b) to the parameter of f a to create a result of type b, then apply f to it, and result is f b using Maybe a for example : fmap show (Just 1) result is : Just "1" same as saying: Just (show 1) but when (->) is used as a Functor (in Control.Monad.Instances) import Control.Monad.Instances (fmap show Just) 1 result is : "Just 1" that is, Just is apply first, then show is applied.