[Haskell-cafe] Proper way to write this

Donn Cave donn at drizzle.com
Mon Dec 26 00:41:07 EST 2005


Quoth Pupeno <pupeno at pupeno.com>:
| I have this piece of code:

What do you think about this?

may_ = maybe (return ()) (\ _ -> f)

stopDaytimeServer dts = do
  may_ (killServer (ss dts)) (streamPort dts)
  may_ (killServer (ds dts)) (dgramPort dts)

| PS: I have a worse case:

I don't think it will be too much worse.  I would not try to
combine the struct updates, in the "both" case -- it doesn't buy
you anything, and pulls you into duplication you don't want.

	Donn Cave, donn at drizzle.com

| stopDaytimeServer dts =
|   case dts of
|     DaytimeServer {streamPort = Just _, dgramPort = Just _} -> do
|       killServer (ss dts)
|       killServer (ds dts)
|     DaytimeServer {streamPort = Just _, dgramPort = Nothing} -> do
|       killServer (ss dts)
|     DaytimeServer {streamPort = Nothing, dgramPort = Just _} -> do
|       killServer (ds dts)

| -- | Having a 'DaytimeServer', run it according to its specifications.
| runDaytimeServer :: DaytimeServer -> IO DaytimeServer
| runDaytimeServer dts =
|   case dts of
|     DaytimeServer {streamPort = Just _, dgramPort = Just _} -> do
|       ss <- runDgramDaytimeServer dts
|       ds <- runStreamDaytimeServer dts
|       return dts{ss = ss, ds = ds}
|     DaytimeServer {streamPort = Just _, dgramPort = Nothing} -> do
|       ss <- runStreamDaytimeServer dts
|       return dts{ss = ss}
|     DaytimeServer {streamPort = Nothing, dgramPort = Just _} -> do
|       ds <- runDgramDaytimeServer dts
|       return dts{ds = ds}


More information about the Haskell-Cafe mailing list