functor

Can Nullable be used as a functor in C#?

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-22 08:32:33
问题 Consider the following code in C#. public int Foo(int a) { // ... } // in some other method int? x = 0; x = Foo(x); The last line will return a compilation error cannot convert from 'int?' to 'int' which is fair enough. However, for example in Haskell there is Maybe which is a counterpart to Nullable in C#. Since Maybe is a Functor I would be able to apply Foo to x using fmap . Does C# have a similar mechanism? 回答1: We can implement such functionality ourselves: public static class FuncUtils

Can Nullable be used as a functor in C#?

荒凉一梦 提交于 2020-08-22 08:32:10
问题 Consider the following code in C#. public int Foo(int a) { // ... } // in some other method int? x = 0; x = Foo(x); The last line will return a compilation error cannot convert from 'int?' to 'int' which is fair enough. However, for example in Haskell there is Maybe which is a counterpart to Nullable in C#. Since Maybe is a Functor I would be able to apply Foo to x using fmap . Does C# have a similar mechanism? 回答1: We can implement such functionality ourselves: public static class FuncUtils

How does <*> derived from pure and (>>=)?

烈酒焚心 提交于 2020-05-26 03:30:06
问题 class Applicative f => Monad f where return :: a -> f a (>>=) :: f a -> (a -> f b) -> f b (<*>) can be derived from pure and (>>=) : fs <*> as = fs >>= (\f -> as >>= (\a -> pure (f a))) For the line fs >>= (\f -> as >>= (\a -> pure (f a))) I am confused by the usage of >>= . I think it takes a functor f a and a function, then return another functor f b . But in this expression, I feel lost. 回答1: Lets start with the type we're implementing: (<*>) :: Monad f => f (a -> b) -> f a -> f b (The

Making a datatype an instance of Functor to map on a field which is of parametric type

落花浮王杯 提交于 2020-05-16 04:07:51
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

Making a datatype an instance of Functor to map on a field which is of parametric type

允我心安 提交于 2020-05-16 04:07:13
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

Making a datatype an instance of Functor to map on a field which is of parametric type

守給你的承諾、 提交于 2020-05-16 04:06:12
问题 Follow up on this question about Learn You a Haskell for Great Good . The author, at the end of Chapter 8 declares this datatype (slighly simplified, I hope it's fine) data Barry t k p = BarryV p (t k) deriving (Show) and then makes it an instance of Functor instance Functor (Barry a b) where fmap f (BarryV x y) = BarryV (f x) y then concluding There we go! We just mapped the f over the first field. Yes. The first. So my question is: what if I want to map over, say, the second field? Actually

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