<div dir="ltr">David,<div><br></div><div>Hoogle doesn't appear to have any matches for "findM" that I could find. Your code is pretty close to what I came up with this morning except yours is clever with monads and mine was just boring recursing through the list till I hit a match. This solution of yours looks like it is in the spirit of what I think I saw in my mind so I am going to study it very hard and understand it!</div>
<div style><br></div><div style>Sometimes you just have to grind it out!</div><div style>Thanks.<br></div><div><br></div><div><br></div><div><div class="gmail_extra"><div class="gmail_quote">On 3 March 2013 19:46, David McBride <span dir="ltr"><<a href="mailto:toad3k@gmail.com" target="_blank">toad3k@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I would probably go (untested):<br><br> ...<br> usbDevs <- ...<br> matches <- findM (isTarget foo) $ V.toList usbDevs<br>
...<br> where<br> findM :: Monad m => (a -> m Boolean) -> [a] -> m (Maybe a)<br>
findM _ [] = return Nothing<br> findM f (x:xs) = do<br> b <- 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't search for it.<br>
<br><div class="gmail_quote">On Sun, Mar 3, 2013 at 1:28 PM, emacstheviking <span dir="ltr"><<a href="mailto:objitsu@gmail.com" target="_blank">objitsu@gmail.com</a>></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 "Haskell-ise" 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'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 <- getDevices ctx<br> matches <- V.filterM (isTarget (cVendor, cProd)) usbDevs<br> case V.null matches of<br>
True -> return Nothing<br> False -> return $ Just $ matches!<span></span><span></span></span></b><br><br><span style="font-family:courier new,monospace"><b>isTarget :: (Word16, Word16) -> Device -> IO Bool<br>
isTarget (vid, pid) dev = do<br> getDeviceDesc dev >>= \info -><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't figure it out by myself yet!<br>
<br></div>In the Data.Vector there is "Data.Vector.find" 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="13d31cc175938c63_13d3184a1cebf575_v:find">find</a> :: (a -> <a href="http://hackage.haskell.org/packages/archive/base/4.6.0.0/doc/html/Data-Bool.html#t:Bool" target="_blank">Bool</a>) -> <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 -> <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't yet feel comfortable with) about liftM and all that but in this case I can't see how and where it would work. I "know" (like Spiderman knows there is danger) that it'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" target="_blank">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>
<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></div></div></div>