<div dir="ltr">I think you're looking for `mapMaybe` :-)<div><br></div><div><br></div><div> - Lyndon</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, May 31, 2015 at 11:30 AM, <span dir="ltr"><<a href="mailto:briand@aracnet.com" target="_blank">briand@aracnet.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
A simple example of something I was trying to do but it had some questions along the lines of "the best way to do this".<br>
<br>
Given a list<br>
<br>
l = [a]<br>
<br>
and an a-list<br>
<br>
alist = [ (a,b) ]<br>
<br>
the idea is to find all the items in l which are in alist and then create a list [b]<br>
<br>
so the overall function should be<br>
<br>
[a] -> [ (a,b) ] -> [b]<br>
<br>
the solution is straightforward:<br>
<br>
l1 = filter (\x -> isJust (lookup x alist)) l<br>
l2 = map (\x -> fromJust (lookup x alist)) l1<br>
<br>
`fromJust` used in the construction of l2 won't fail, because only the elements for which the lookup succeeded are in l1.<br>
<br>
This would be something called `filterMap` but I couldn't find such a function in the list library, but it seems like there just has to be one defined in a library somewhere.<br>
<br>
the above seems clumsy, i'm wondering how to make it "more pretty".<br>
<br>
generally i was also wondering if the above construction is as inefficient as it looks because of the double-lookup, or would the compiler actually be able to optimize that code into something more efficient ? this code is not being used on large sets of data so efficiency doesn't matter, I'm just curious.<br>
<br>
Thanks,<br>
<br>
Brian<br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>