[Haskell-cafe] Advice needed on how to improve some code

Jeff jeff at datalinktech.com.au
Thu Apr 16 06:37:03 UTC 2015


Thanks Tom, David and Claude for your replies.



> On 16 Apr 2015, at 4:03 pm, Tom Ellis <tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk> wrote:
> 
> The first thing you should do is define
> 
>    parseDeviceGPSDataOf constructor parser setField =
>       ( \pl' -> let pld = P.payloadData pl' in
>                  if testBit mdm ( fromEnum constructor )
>                    then
>                      parser >>=
>                      ( \s -> return ( pl' { P.payloadData = setField pld (Just s) } } ) )
>                    else
>                      return pl' )
> 
> and your chain of binds will become
> 
>    setgpsData pld = pld { P.gpsData = Just s }
>    ...
> 
>    parseDeviceDataOf D.GPS parseDeviceGPSData setgpsData >>=
>    parseDeviceDataOf D.GSM parseDeviceDSMData setgsmData >>=
>    parseDeviceDataOf D.COT parseDeviceCOTData setcotData >>=
>    ...
> 
> Then I would probably write
> 
>    deviceSpecs = [ (D.GPS, parseDeviceGPSData, setgpsData)
>                  , (D.GSM, parseDeviceDSMData, setgsmData)
>                  , (D.COT, parseDeviceCOTData, setcotData) ]
> 
> and turn the chain of binds into a fold.
> 


I’ll do as you have suggested Tom.  Thanks.

Jeff



More information about the Haskell-Cafe mailing list