
So I had a function that would leave me with a Nothing or a Just, and I wanted to use the result of this function to lookup in a Map. So something like this was very easy to do. h> let list = Data.Map.fromList [(1,2),(3,4),(5,6)] h> Just 4 >>= flip Data.Map.lookup list Nothing h> Just 3 >>= flip Data.Map.lookup list Just 4 Now, however, my source function gives me an Either. I ended up writing this function to let me chain Either with Data.Map.lookups: (>>?=) :: (Either a b, a) -> (b -> Maybe c) -> (Either a c,a) (>>?=) (Left x,e) _ = (Left x,e) (>>?=) (Right y,e) f = case f y of Nothing -> (Left e,e) Just z -> (Right z,e) But I can't help thinking I'm reinventing the wheel here somehow. There must be a better way, right? Thanks in advance for any pointers. Alan

Hi Alan, Check out these two functions, they point you towards how to write more idiomatic code. They are called smart destructors, google that term for more information: λ> :i maybe maybe :: b -> (a -> b) -> Maybe a -> b -- Defined in `Data.Maybe' λ> :i either either :: (a -> c) -> (b -> c) -> Either a b -> c -- Defined in `Data.Either'

On 5/4/2014 10:42 AM, Dan Serban wrote:
Hi Alan,
Check out these two functions, they point you towards how to write more idiomatic code. They are called smart destructors, google that term for more information:
λ> :i maybe maybe :: b -> (a -> b) -> Maybe a -> b -- Defined in `Data.Maybe' λ> :i either either :: (a -> c) -> (b -> c) -> Either a b -> c -- Defined in `Data.Either' _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
I'm following this thread, and the only "smart destructors" I turned up with Google is this post: http://stackoverflow.com/questions/10161009/input-checks-in-haskell-data-con... The Haskellwiki has a mention but it's a blank page.

On Mon, May 5, 2014 at 7:34 AM, John M. Dlugosz
I'm following this thread, and the only "smart destructors" I turned up with Google is this post: <http://stackoverflow.com/ questions/10161009/input-checks-in-haskell-data-constructors>
I think parent was thinking of smart constructors and mixed them up with catamorphisms, which, partly due to the mystique of category theory and how seemingly only smart people get it, can be seen as smart destructors. The thing to note is that they aren't smart in the same way as smart constructors are. Catas are just dumb destructors, dumb in the same way as plain vanilla "dumb" constructors. The catas for lists, Maybe, and Either are respectively, foldr, maybe, either. There's a body of literature on deriving catas automatically. Look under generics or (older) polytypic programming. -- Kim-Ee

You might also consider looking at the errors package on hackage. specifically hush and note for converting between Either and Maybe. They are defined herehttp://hackage.haskell.org/package/errors-1.2.1/docs/Control-Error-Util.html . Ben

Thanks Dan and Ben – these are exactly what I was looking for. Alan From: Beginners [mailto:beginners-bounces@haskell.org] On Behalf Of Benjamin Edwards Sent: 05 May 2014 10:30 To: beginners@haskell.org Subject: Re: [Haskell-beginners] Using Either and Maybe You might also consider looking at the errors package on hackage. specifically hush and note for converting between Either and Maybe. They are defined here http://hackage.haskell.org/package/errors-1.2.1/docs/Control-Error-Util.html . Ben
participants (5)
-
Alan Buxton
-
Benjamin Edwards
-
Dan Serban
-
John M. Dlugosz
-
Kim-Ee Yeoh