Selecting single result of function application to list

Hello, Apologies the simpleton question but I would like to know how it is done in Haskell. I have a list of values, and I am applying a function to each of these elements. The result is a Maybe. I would like to return the first occurrence which is not a Nothing. I am considering something like: selectOne f = take 1 . filter (\e -> case e of Just _ -> True _ -> False ) . map f I this how it is done? TIA, Hugo F.

Here's how I'd do it:
import Data.Maybe (catMaybes)
list = ["hi", "blah", "foo"]
firstJust = head . catMaybes
selectOne f = firstJust . map f
myFunction :: String -> Maybe Int
myFunction = undefined
main = print $ selectOne myFunction list
catMaybes will take a list of Maybe a and reduce it to a list of a,
throwing out all the Nothings.
As you'll learn from working with Maybe a lot, if you're casing off of a
maybe value, there's probably a better way to do it. Functions like
"catMaybes" and "maybe" and especially the Monad instance of Maybe are
really helpful for avoiding this ugly branching logic.
On Thu, Nov 3, 2011 at 9:07 AM, Hugo Ferreira
I am considering something like:
selectOne f = take 1 . filter (\e -> case e of Just _ -> True _ -> False ) . map f
-- Michael Xavier http://www.michaelxavier.net LinkedIn http://www.linkedin.com/pub/michael-xavier/13/b02/a26

On 11/03/2011 04:26 PM, Michael Xavier wrote:
Here's how I'd do it:
import Data.Maybe (catMaybes)
list = ["hi", "blah", "foo"]
firstJust = head . catMaybes
selectOne f = firstJust . map f
myFunction :: String -> Maybe Int myFunction = undefined
main = print $ selectOne myFunction list
catMaybes will take a list of Maybe a and reduce it to a list of a, throwing out all the Nothings.
As you'll learn from working with Maybe a lot, if you're casing off of a maybe value, there's probably a better way to do it. Functions like "catMaybes" and "maybe" and especially the Monad instance of Maybe are really helpful for avoiding this ugly branching logic.
Thanks Michael. I am planning to compose functions that return Maybe so I will, as you pointed out, be looking at the Maybe Monad. Rgards, Hugo F.
On Thu, Nov 3, 2011 at 9:07 AM, Hugo Ferreira
mailto:hmf@inescporto.pt> wrote: I am considering something like:
selectOne f = take 1 . filter (\e -> case e of Just _ -> True _ -> False ) . map f
-- Michael Xavier http://www.michaelxavier.net LinkedIn http://www.linkedin.com/pub/michael-xavier/13/b02/a26

On Thu, 03 Nov 2011 16:07:01 +0000, Hugo Ferreira
Hello,
Apologies the simpleton question but I would like to know how it is done in Haskell. I have a list of values, and I am applying a function to each of these elements. The result is a Maybe. I would like to return the first occurrence which is not a Nothing.
You can use catMaybes from Data.Maybe: import Data.Maybe selectOne f = head . catMaybes . map f Note that is a partial function; it will crash if f returns Nothing for each element. Cheers, Daniel

On Thursday 03 November 2011, 17:07:01, Hugo Ferreira wrote:
Hello,
Apologies the simpleton question but I would like to know how it is done in Haskell. I have a list of values, and I am applying a function to each of these elements. The result is a Maybe. I would like to return the first occurrence which is not a Nothing.
I am considering something like:
selectOne f = take 1 . filter (\e -> case e of Just _ -> True _ -> False ) . map f
I this how it is done?
In Data.Maybe, there's catMaybes :: [Maybe a] -> [a] mapMaybe :: (a -> Maybe b) -> [a] -> [b] listToMaybe :: [a] -> Maybe a maybeToList :: Maybe a -> [a] Your selectOne f is take 1 . catMaybes . map f or take 1 . mapMaybe f Alternatively, you could use the MonadPlus class from Control.Monad, which provides mzero, mplus and msum, with Maybe's MonadPlus instance selectOne f = maybeToList . msum . map f

There's also newtype First wraping a Maybe in Data.Monoid, in which you can wrap your Maybes, mconcat them and get the first Just value. Like:
mconcat $ map First [Nothing, Just "first", Nohing, Just "last", Nothing] First {getFirst = Just "first"}
On Thu, Nov 3, 2011 at 6:07 PM, Hugo Ferreira
Hello,
Apologies the simpleton question but I would like to know how it is done in Haskell. I have a list of values, and I am applying a function to each of these elements. The result is a Maybe. I would like to return the first occurrence which is not a Nothing.
I am considering something like:
selectOne f = take 1 . filter (\e -> case e of Just _ -> True _ -> False ) . map f
I this how it is done?
TIA, Hugo F.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Markus Läll

Thanks to Daniel Fischer, Daniel Schoepe and Markus Lall for the answers. Will have to look this over more carefully. Rgds, Hugo F. On 11/03/2011 05:27 PM, Markus Läll wrote:
There's also newtype First wraping a Maybe in Data.Monoid, in which you can wrap your Maybes, mconcat them and get the first Just value. Like:
mconcat $ map First [Nothing, Just "first", Nohing, Just "last", Nothing] First {getFirst = Just "first"}
On Thu, Nov 3, 2011 at 6:07 PM, Hugo Ferreira
wrote: Hello,
Apologies the simpleton question but I would like to know how it is done in Haskell. I have a list of values, and I am applying a function to each of these elements. The result is a Maybe. I would like to return the first occurrence which is not a Nothing.
I am considering something like:
selectOne f = take 1 . filter (\e -> case e of Just _ -> True _ -> False ) . map f
I this how it is done?
TIA, Hugo F.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (5)
-
Daniel Fischer
-
Daniel Schoepe
-
Hugo Ferreira
-
Markus Läll
-
Michael Xavier