functor

bind can be composed of fmap and join, so do we have to use monadic functions a -> m b?

隐身守侯 提交于 2019-12-08 19:12:19
问题 I don't use Haskell a lot, but I understand the concept of Monads. I had been confused by Kleisli triple, and the category, however, fmap and join Although Haskell defines monads in terms of the return and bind functions, it is also possible to define a monad in terms of return and two other operations, join and fmap . This formulation fits more closely with the original definition of monads in category theory. The fmap operation, with type (t → u) → M t → M u , takes a function between two

Is (fmap f) the same as (f .) if f is a function of type a->b?

给你一囗甜甜゛ 提交于 2019-12-08 17:39:43
问题 I am trying to implement a Functor instance of data ComplicatedA a b = Con1 a b | Con2 [Maybe (a -> b)] For Con2, my thought process was the fmap needs to be something like fmap f (Con2 xs) = Con2 (map f' xs) then I need to have a list map function f' like Maybe (a -> x) -> Maybe (a -> y) Since Maybe is a Functor, I can write f' like fmap ((a->x) -> (a->y)) In order to get ((a->x) -> (a->y)) , I thought I could just do fmap (x->y) which is the same as (fmap f) So my sulotion was instance

Why there isn't a Functor instance for Kleisli in Control.Arrow?

可紊 提交于 2019-12-08 17:00:49
问题 While trying to familiarize myself with Control.Arrow, I have noticed that the Kleisli newtype would seem to admit a Functor instance, something like: instance Monad m => Functor (Kleisli m a) where fmap f (Kleisli k) = Kleisli $ liftM f . k Is there a reason why this instance isn't provided? Does it exist in some package as an orphan instance? 回答1: Every arrow can be made into a valid Functor by defining fmap f a = a >>> arr f However it's not possible to declare a Functor to be a superclass

Java equivalent of .NET Action<T> and Func<T,U>, etc [duplicate]

谁说胖子不能爱 提交于 2019-12-08 15:09:22
问题 This question already has answers here : Java's equivalents of Func and Action (8 answers) Closed 6 years ago . Are there any standard generic "callback" or "function/method" types in Java, like System.Action<T> or System.Func<T,U> in .NET? In my concrete case, I need a class that wraps a method that takes one (generic) parameter of type T and returns nothing (i.e. void ). Yes, it's easy enough to create such a class/interface for myself, but I'd prefer a standard library class if there is

Haskell fmap functor

半城伤御伤魂 提交于 2019-12-08 07:39:34
问题 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

Using a non-static class member inside a comparison function

自作多情 提交于 2019-12-08 07:06:46
问题 I'm currently developing a syntaxic analyser class that needs, at a point of the code, to sort structs holding info about operators. Each operator has a priority, which is user-defined through public member functions of my analyser class. Thus, when sorting, I need my sorting function to order elements based on the priority of the corresponding operator. I'm using the following code to compare elements: bool parser::op_comp(const op_info& o1, const op_info& o2) { op_def& op1 = operators[o1.op

Java F-Bound types with generics

怎甘沉沦 提交于 2019-12-08 04:18:46
问题 Is there any way to express f-bound types in java where at the call site, a generic response is returned? interface Functor<T extends Functor<T>> public <B> T<B> map(Function<A, B> fn); // won't compile because types don't match I can use f-bound types if the type never changes, but in the case of map, I need a new type. Is there a way to express this in java? What I am really looking for is any way that I can get something like higher kinds even though I know javac doesn't support higher

How to use Functor instances with Fix types

谁说我不能喝 提交于 2019-12-08 01:57:39
问题 Let's say I want to have a very generic ListF data type: {-# LANGUAGE GADTs, DataKinds #-} data ListF :: * -> * -> * where Nil :: List a b Cons :: a -> b -> List a b Now I can use this data type with Data.Fix to build an f-algebra import qualified Data.Fix as Fx instance Functor (ListF a :: * -> *) where fmap f (Cons x y) = Cons x (f y) fmap _ Nil = Nil sumOfNums = Fx.cata f (Fx.Fix $ Cons 2 (Fx.Fix $ Cons 3 (Fx.Fix $ Cons 5 (Fx.Fix Nil)))) where f (Cons x y) = x + y f Nil = 0 But how I can

Writing a generic functor instance across type constructors?

旧街凉风 提交于 2019-12-07 18:18:17
问题 I'm learning basic type classes and have written my own implementation of functor for my type Test a (behaves just like Maybe ): data Test a = Test a | Emp class FC c a where t :: (a -> b) -> c a -> c b instance FC Test a where t f (Test a) = Test (f a) t f (Emp) = Emp instance FC Maybe a where t f (Just a) = Just (f a) t f (Nothing) = Nothing Is it possible to implement something like: instance FC c where t f (c v) = c (f v) Error: Parse error in pattern: c In other words, abstract away the

How do C++ functor constructors get called when used with for_each or std::transform

牧云@^-^@ 提交于 2019-12-07 18:14:41
问题 I've never used c++ functors before and so I'm just trying to understand how they work. e.g. suppose we have this functor class class MultiplyBy { private: int factor; public: MultiplyBy(int x) : factor(x) { } int operator () (int other) const { return factor * other; } }; Using it like this is clear to me: MultiplyBy mult_3(3); int x = mult_3(100); Obviosuly the constructor of MultiplyBy is being called with the argument 3. But in the following case, how is the constructor being called with