How to get a deep-wrapping handle in Haskell?

Occasionally I find myself mapped to a lot of functors, such as some parser for optional value sets:

 - parse a rectangular block of characters to a map of
-- coordinate to the character, or Nothing for whitespace
parseRectangle :: Parser (Map (Int, Int) (Maybe Char))

data Class = Letter | Digit | Other

classify :: Char -> Class

parseClassifiedRectangle :: Parser (Map (Int, Int) (Maybe Class ))
parseClassifiedRectangle = fmap (fmap (fmap classify)) parseRectangle

What are some good ways to surround nested fmaps? Usually it’s not as clear as here, I ended up adding fmaps until the code type check. The simple code ended up being a mess of fmap boilerplate, what I really want to express is “raise this function to the proper depth and apply it to the included Type”.

Some ideas, so far I haven’t found any particularly satisfactory ideas:

> define fmap2 ::(Functor f,Functor g)=> (a –> B) –> g(fa) –> g(fb) and friends
> define specific helpers, such as mapMaybeMap ::(a –> b) –> map k (may be a) –> Map k (maybe b)
>Introduce a newtype wrapper for the functor stack, and create those instances of Functor, such as newtype MaybeMapParser a = Parser(Map(Int,Int)(maybe a))

Has anyone else encountered this problem in a large code base? Is this even a problem? How do you solve it?

Let me break the deadlock on this interesting question, it seems difficult for people to answer this question. The problem may be boiled down to a style problem rather than any problem, so there is a lack of answers.

My method is as follows:

parseClassifiedRectangle :: Parser (Map (Int, Int) (Maybe Class))
parseClassifiedRectangle = doClassify <$> parseRectangle
where
doClassify = Map.map (fmap classify)

I try to use< $> is used for the top-level functor, and saves fmap for internal functors; although this is not always good in practice.

I used local named binding. But even if you leave doClassify as f, It sometimes helps to clarify the high-level view of what happened: “In terms of the value of parsing, we are doing something, please see below.” I don’t know what the efficiency issue of making bindings is.

I also used a specific instance of fmap for the Map instance. This helps me locate in the stack and provide a waypoint for the final fmap.

Hope this helps.

< /div>

Occasionally I find myself mapped to a lot of functors, such as some parser for optional value sets:

- -parse a rectangular block of characters to a map of
-- coordinate to the character, or Nothing for whitespace
parseRectangle :: Parser (Map (Int, Int) (Maybe Char))

data Class = Letter | Digit | Other

classify :: Char -> Class

parseClassifiedRectangle :: Parser (Map (Int, Int) (Maybe Class) )< br />parseClassifiedRectangle = fmap (fmap (fmap classify)) parseRectangle

What are some good ways to surround nested fmaps? Usually it’s not as clear as here, I ended up adding fmaps until the code type check. The simple code ended up being a mess of fmap boilerplate, what I really want to express is “raise this function to the proper depth and apply it to the included Type”.

Some ideas, so far I haven’t found any particularly satisfactory ideas:

> define fmap2 ::(Functor f,Functor g)=> (a –> B) –> g(fa) –> g(fb) and friends
> define specific helpers, such as mapMaybeMap ::(a –> b) –> map k (may be a) –> Map k (maybe b)
>Introduce a newtype wrapper for the functor stack, and create those instances of Functor, such as newtype MaybeMapParser a = Parser(Map(Int,Int)(maybe a))

Has anyone else encountered this problem in a large code base? Is this even a problem? How do you solve it?

Let me break the deadlock on this interesting question, it seems difficult for people to answer this question. This question may boil down to a question of style rather than any question, so Lack of answers.

My method is as follows:

parseClassifiedRectangle :: Parser (Map (Int, Int) (Maybe Class))
parseClassifiedRectangle = doClassify <$> parseRectangle
where
doClassify = Map.map (fmap classify)

I try to use <$> for the top-level Functor and for the internal functor Save the fmap; although this is not always great in practice.

I used the local named binding. But even leaving doClassify as f, it sometimes helps to clarify the high level of what is happening Viewpoint: “In terms of the value of parsing, we are doing something, please refer to the content below.” I don’t know what is the efficiency of making bindings.

I also used fmap for the Map instance This helps me locate in the stack and provide a waypoint for the final fmap.

Hope this helps.

Leave a Comment

Your email address will not be published.