
It's been about a year since I used Haskell and I'm rusty. I think I knew how to do this once, but I need a reminder. I've got some functions: getDeviceInfo :: Int -> IO DeviceInfo name :: DeviceInfo -> String I want to do something like func :: IO () func = do ds <- mapM getDeviceInfo [0..10] mapM_ (print . name) ds Is there a way to combine 'getDeviceInfo', 'name', and 'print' in one line? Dennis

Try this:
mapM (\x -> getDeviceInfo x >>= print . name) [0..10]
2011/7/1 Dennis Raddle
It's been about a year since I used Haskell and I'm rusty. I think I knew how to do this once, but I need a reminder.
I've got some functions:
getDeviceInfo :: Int -> IO DeviceInfo
name :: DeviceInfo -> String
I want to do something like
func :: IO () func = do ds <- mapM getDeviceInfo [0..10] mapM_ (print . name) ds
Is there a way to combine 'getDeviceInfo', 'name', and 'print' in one line?
Dennis
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Fri, Jul 1, 2011 at 10:48 PM, Alexey G
Try this:
mapM (\x -> getDeviceInfo x >>= print . name) [0..10]
Though they're not very well known, Control.Monad now contains "composition" monad operators, so you can just write :
mapM_ (getDeviceInfo >=> print . name) [0..10]
which is pretty elegant (to my eyes at least). (>=>) :: (a -> m b) -> (b -> m c) -> a -> m c -- Jedaï

On Sat, Jul 2, 2011 at 9:55 AM, Chaddaï Fouché
On Fri, Jul 1, 2011 at 10:48 PM, Alexey G
wrote: Try this:
mapM (\x -> getDeviceInfo x >>= print . name) [0..10]
mapM_ (getDeviceInfo >=> print . name) [0..10]
Or maybe, to be more consistent order-wise :
mapM_ (print . name <=< getDeviceInfo) [0..10]
(>=>) :: (a -> m b) -> (b -> m c) -> a -> m c
Oops, forgot the constraint : (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c) -- Jedaï

On Jul 1, 2011, at 1:08 PM, Dennis Raddle wrote:
It's been about a year since I used Haskell and I'm rusty. I think I knew how to do this once, but I need a reminder.
I've got some functions:
getDeviceInfo :: Int -> IO DeviceInfo
name :: DeviceInfo -> String
I want to do something like
func :: IO () func = do ds <- mapM getDeviceInfo [0..10] mapM_ (print . name) ds
Is there a way to combine 'getDeviceInfo', 'name', and 'print' in one line?
I end up with either: join . fmap (mapM_ (print . name)) $ mapM getDeviceInfo [1..10] or mapM getDeviceInfo [1..10] >>= mapM_ (print . name) You can move the call to name into the mapM using (fmap name . getDeviceInfo) but I left them separate to match your code better.
participants (4)
-
Alexey G
-
Chaddaï Fouché
-
Dennis Raddle
-
Sean Perry