How do monads make my work easier? Show me some cool code

I like to read code snippets about concepts that I don’t understand. Are there any fragments that show monads in all their glory? More importantly, how can I use monads to make my work easier.

I use jQuery heavily. This is one great monad app that I know of.

+5
source share
5 answers

Like others, I think this question is too general. I think most of the answers (like mine) will give examples of something neat using one specific monad. The real power of monads is that as soon as you understand them as an abstraction, you can apply this knowledge to any new monads that you encounter (and there are many of them in Haskell). This, in turn, means that you can easily understand what the new code is doing and how to use it, because you already know the interface and some rules that determine its behavior.

Anyway, here is an example of using the List monad from a test run script I wrote:

runAll :: IO ()
runAll = do
  curdir <- getCurrentDirectory
  sequence $ runTest <$> srcSets <*> optExeFlags <*> optLibFlags
  setCurrentDirectory curdir

Technically, I use the applicative interface, but you can just change <*>to apfrom Control.Monad if that bothers you.

, runTest "srcSets", "optExeFlags" "optLibFlags", . , , , C (3 ).

+3

- , , " , ". , . , Haskell, , , .

xmonad. , , , - - . . , , :

handleMessage' :: AlmostFull a -> SomeMessage -> Int -> Maybe (AlmostFull a)
handleMessage' l@(AlmostFull ratio delta t) m winCount =
  case winCount of
    -- keep existing Tall layout, maybe update ratio
    0 -> finalize (maybeUpdateRatio $ fromMessage m) (Just t)
    1 -> finalize (maybeUpdateRatio $ fromMessage m) (Just t)

    -- keep existing ratio, maybe update Tall layout
    _ -> finalize (Just ratio) (pureMessage t m)
  where
    finalize :: Maybe Rational -> Maybe (Tall a) -> Maybe (AlmostFull a)
    finalize ratio t = ratio >>= \ratio -> t >>= \t ->
      return $ AlmostFull ratio delta t

    maybeUpdateRatio :: Message -> Maybe Rational
    maybeUpdateRatio (Just Shrink) = Just (max 0 $ ratio-delta)
    maybeUpdateRatio (Just Expand) = Just (min 1 $ ratio+delta)
    maybeUpdateRatio _             = Nothing

, ( X, , ) - 0 1 , AlmostFull , . f. Just , , . ; Tall, 2 . Just Tall, , Nothing.

finalize - ; ratio ( ), t ( Tall) Maybe. , Nothing, Nothing .

, Maybe, , , , - , Nothing.

+2

, - " ". , , ​​ (Maybe), (Writer), / (IO), (State), ( [a]), (Parsec, ReadP) .

. ,

+2

Haskell Information Flow. , Monads Haskell.

http://www.cse.chalmers.se/~russo/seclib.htm

+1

, . , .

, - , , . Nothing (, , ), , , .

, , , . , ReaderT. find, , both, , , oneOf, , , t . :

import Control.Monad
import Control.Monad.Reader

find a = ReaderT (lookup a)
both a b = liftM2 (++) a b
oneOf = mplus

search = both (find 1) ((find 2) `oneOf` (find 3)) 
         `oneOf` both (find 4) (find 5)

:

(runReaderT search) [(1,"a"),(3,"c"),(4,"d"),(5,"g")] --> Just "ac"

(runReaderT search) [(6,"a")] --> Nothing

, , , . , , search_a search_b, , :

 do a <- search_a
    b <- search_b
    return (merge a b)

, , liftM2 merge search_a search_b.

+1
source

All Articles