I would probably go (untested):<br><br>    ...<br>    usbDevs &lt;- ...<br>    matches &lt;- findM (isTarget foo) $ V.toList usbDevs<br>    ...<br>    where<br>      findM :: Monad m =&gt; (a -&gt; m Boolean) -&gt; [a] -&gt; m (Maybe a)<br>
      findM _ [] = return Nothing<br>      findM f (x:xs) = do<br>        b &lt;- f x<br>        return $ if b<br>          then Just x<br>          else findM f xs<br><br>I can almost guarantee you there is a findM already out there somewhere to use, but hayoo is down right now so I can&#39;t search for it.<br>
<br><div class="gmail_quote">On Sun, Mar 3, 2013 at 1:28 PM, emacstheviking <span dir="ltr">&lt;<a href="mailto:objitsu@gmail.com" target="_blank">objitsu@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr"><div>I now have a working USB application that can find, locate and switch LED-s on and off on my Hexwax board, for which I thank all those that helped me out last week.<br><br>I am now trying to &quot;Haskell-ise&quot; my code i.e. make it less amateurish with respect to some of its inner workings in a constant drive for inner cleanliness and warm fuzziness etc.<br>


<br>When attempting to find the device, I use the System.USB.getDevices function which returns me IO (Vector Device), a list of everything that&#39;s currently plugged in and available and I then use Data.Vector.filterM like so:<br>


<br><b><span style="font-family:courier new,monospace">handleFor ctx (cVendor, cProd) = do<br>    usbDevs &lt;- getDevices ctx<br>    matches &lt;- V.filterM (isTarget (cVendor, cProd)) usbDevs<br>    case V.null matches of<br>


      True  -&gt; return Nothing<br>      False -&gt; return $ Just $ matches!<span></span><span></span></span></b><br><br><span style="font-family:courier new,monospace"><b>isTarget :: (Word16, Word16) -&gt; Device -&gt; IO Bool<br>


isTarget (vid, pid) dev = do<br>  getDeviceDesc dev &gt;&gt;= \info -&gt;<br>    return $ (deviceVendorId info, deviceProductId info) == (vid, pid)<br></b></span><br><br>but... that is not as efficient as it could be because I could have N devices and then I just throw all but the first. Tut tut. Could do better. If I knew how... well I kind of do but I can&#39;t figure it out by myself yet!<br>


<br></div>In the Data.Vector there is &quot;Data.Vector.find&quot; which is *exactly* what I want with small dent in the bodywork, the predicate function is pure:<br><br><div><span style="font-family:courier new,monospace"><b><a name="13d3184a1cebf575_v:find">find</a> ::  (a -&gt; <a href="http://hackage.haskell.org/packages/archive/base/4.6.0.0/doc/html/Data-Bool.html#t:Bool" target="_blank">Bool</a>) -&gt; <a href="http://hackage.haskell.org/packages/archive/vector/0.10.0.1/doc/html/Data-Vector.html#t:Vector" target="_blank">Vector</a> a -&gt; <a href="http://hackage.haskell.org/packages/archive/base/4.6.0.0/doc/html/Data-Maybe.html#t:Maybe" target="_blank">Maybe</a> a<br>


</b></span><br></div><div>So my question is, how do I make it work? I know (but don&#39;t yet feel comfortable with) about liftM and all that but in this case I can&#39;t see how and where it would work. I &quot;know&quot; (like Spiderman knows there is danger) that it&#39;s crying out for something and the return type is perfect too as it would just match.<br>


<br>SO...how can I do it chaps?<br><br></div><div>And as usual... .any comments, style notes, idiomatic pointers(!) etc. are always welcome.<br><br></div><div>Thanks,<br>Sean Charles.<br><br></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br>