functor

Function pointers in embedded systems, are they useful?

巧了我就是萌 提交于 2019-11-29 04:17:28
In an interview they asked me if using function pointers would be beneficial (in terms of speed) when writing code for embedded systems? I had no idea on embedded system so could not answer the question. Just a cloudy or vague answer. So what are the real benefits? Speed, readability, maintenance,cost? I think perhaps Viren Shakya's answer misses the point that the interviewer was trying to elicit. In some constructs the use of a function pointer may speed up execution. For example, if you have an index, using that to index an array of function pointers may be faster than a large switch. If

Functor instance for a GADT with type constraint

ぃ、小莉子 提交于 2019-11-29 04:13:35
Today I wanted to investigate if it is possible to construct a data type in such a way, that it does not store the data of the type of its type signature, but another representation of it. So, here is my attempt of an GADT which has a type constructor of type a , but a data constructor of type ByteString . {-# LANGUAGE GADTs #-} import Data.ByteString.Char8 import Data.Serialize data Serialized a where MkSerialized :: (Serialize a) => ByteString -> Serialized a Now I can define a decode' function in the following way: decode' :: (Serialize a) => Serialized a -> a decode' (MkSerialized bs) =

Why is `pure` only required for Applicative and not already for Functor? [duplicate]

帅比萌擦擦* 提交于 2019-11-29 02:54:21
问题 This question already has answers here : Why Functor class has no return function? (4 answers) Closed 4 months ago . Reading this Wikibook about Haskell and Category Theory basics, I learn about Functors: A functor is essentially a transformation between categories, so given categories C and D, a functor F : C -> D maps any object A in C to F(A), in D. maps morphisms f : A -> B in C to F(f) : F(A) -> F(B) in D. ... which sounds all nice. Later an example is provided: Let's have a sample

How (fmap . fmap) typechecks

五迷三道 提交于 2019-11-29 02:12:05
问题 I have been going through a article(http://comonad.com/reader/2012/abstracting-with-applicatives/) and found the following snippet of code there: newtype Compose f g a = Compose (f (g a)) deriving Show instance (Functor f, Functor g) => Functor (Compose f g) where fmap f (Compose x) = Compose $ (fmap . fmap) f x How does actually (fmap . fmap) typechecks ? Their types being: (.) :: (a -> b) -> (r -> a) -> (r -> b) fmap :: (a -> b) -> f a -> f b fmap :: (a -> b) -> f a -> f b Now from here I

Making (a, a) a Functor

痴心易碎 提交于 2019-11-28 21:19:11
How can I make (a, a) a Functor without resorting to a newtype ? Basically I want it to work like this: instance Functor (a, a) where fmap f (x, y) = (f x, f y) But of course that's not a legal way to express it: Kind mis-match The first argument of `Functor' should have kind `* -> *', but `(a, a)' has kind `*' In the instance declaration for `Functor (a, a)' What I really want is a type-level function like this: \a -> (a, a) (invalid syntax). So a type alias, perhaps? type V2 a = (a, a) instance Functor V2 where fmap f (x, y) = (f x, f y) I would think this would work, but it doesn't. First I

confused about function as instance of Functor in haskell

 ̄綄美尐妖づ 提交于 2019-11-28 21:14:57
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. in another example ,result is same: fmap (*3) (+100) 1 result is 303 why not *3 first, then +100? the

Can ML functors be fully encoded in .NET (C#/F#)?

六月ゝ 毕业季﹏ 提交于 2019-11-28 17:18:33
Can ML functors be practically expressed with .NET interfaces and generics? Is there an advanced ML functor use example that defies such encodings? Answers summary : In the general case, the answer is NO. ML modules provide features (such as specification sharing via signatures [ 1 ]) that do not directly map to .NET concepts. However, for certain use cases the ML idioms can be translated. These cases include not only the basic Set functor [ 2 ], but also the functorial encoding of monads [ 3 ], and even more advanced uses of Haskell, such as finally tagless interpreters [ 4 , 5 ]. Practical

What is a contravariant functor?

巧了我就是萌 提交于 2019-11-28 17:06:49
问题 The type blows my mind: class Contravariant (f :: * -> *) where contramap :: (a -> b) -> f b -> f a Then I read this, but contrary to the title, I wasn't any more enlightened. Can someone please give an explanation of what a contravariant functor is and some examples? 回答1: From a programmer's point of view the essence of functor-ness is being able to easily adapt things. What I mean by "adapt" here is that if I have an f a and I need an f b , I'd like an adaptor that will fit my f a in my f b

Differences between functors and endofunctors

ぃ、小莉子 提交于 2019-11-28 16:40:12
问题 Can someone explain in simple terms the difference between the two? I'm not fully understanding the part where monads are endofunctors versus being just functors. 回答1: A functor may go from one category to a different one, an endofunctor is a functor for which start and target category are the same. Same as with endomorphisms versus morphisms. Now, why must monads be endofunctors? There is the famous quote that "Monads are just monoids in the category of endofunctors". Fortunately, somebody

Let Haskell functors sink in.

喜你入骨 提交于 2019-11-28 15:21:05
问题 Learn You a Haskell has an example about functors. I can read LYAH, and text, and figure out what is supposed to happen -- but I don't know enough to write something like this. I'm finding this problem often in Haskell. instance Functor (Either a) where fmap f (Right x) = Right (f x) fmap f (Left x) = Left x However, I'm confused.. Why doesn't this comple instance Functor (Either a) where fmap f (Right x) = Right (x) fmap f (Left x) = Left (f x) If f isn't being used in the top definition,