Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what can i use instead of `case of` to reduce the code?

fmap ( \eachSheet -> case (eachSheet ^. sProperties) of
  Just sheetProperties -> case (sheetProperties ^. sTitle) of
    Just sheets -> (sheetProperties ^. sSheetId)    
    Nothing -> Nothing
  Nothing -> Nothing ) listOfSheets

any better way to this then case matching

like image 570
Samuel D'costa Avatar asked Dec 05 '22 11:12

Samuel D'costa


1 Answers

The particular pattern

     case f foo of
       Just bla -> case g bla of
          Just muh -> h muh
       Nothing -> Nothing

is chararacteristic of the Maybe monad. That instance is defined as

instance Monad Maybe where
  return = Just
  Nothing >>= _ = Nothing
  Just x >>= f = f x

So you can rewrite the above case construction to

    f foo >>= \bla -> g bla >>= \muh -> h muh

which can be made more readable by either using do syntax

    do
      bla <- f foo
      muh <- g bla
      h muh

or eta-reduction and the Kleisli-composition operator

     (f >=> g >=> h) foo

In your example, the latter is actually not possible because the innermost function doesn't use sheets but sheetProperties again, but do notation can still be used.

like image 179
leftaroundabout Avatar answered Jan 13 '23 02:01

leftaroundabout