What is the correct way to handle (deeply nested) functors?

I have the following simple code

import Data.String.Regex import Data.Array last <$> match someRegex " 1" 

Where

 match someRegex " 1" 

returns something like

 Just ([Just (" 1"),Just (" "),Just ("1")]) 

and

 last <$> match someRegex " 1" 

returns something like

 Just (Just (Just (" 1"))) 

Now I have a deeply enclosed Maybe. This makes it difficult to work with (even using functors). I wrote myself a couple of helper functions, but I'm kind of unhappy with this. It somehow doesn't seem right.

 extract j = do case j of Nothing -> Nothing Just a -> a extract2 jj = extract $ extract jj 

And then using this,

 extract2 $ last <$> match someRegex " 1" 

Is there a better / idiomatic way to do such things in Purescript / Haskell?

+5
source share
1 answer

Perhaps you are looking for a join function:

http://pursuit.purescript.org/packages/purescript-control/0.3.0/docs/Control.Bind#d:join

join collapses two layers of the structure to one level, combining any effects. In the case of Maybe this means that the resulting value will not be Nothing only if both layers were not Nothing .

+9
source

All Articles