<div dir="ltr"><div>Hi Bob,</div><div><br></div><div>I can&#39;t see how this helps my situation though because V.find expects the predicate to be pure. My predicate requires &quot;IO&quot; as it calls two functions that dip into the USB device structure.</div>

<div><br></div><div>Have I missed something here???</div><div><br></div><div>Thanks</div><div>Sean</div><div><br></div><div><br><div><div class="gmail_extra"><div class="gmail_quote">On 3 March 2013 18:44, Bob Ippolito <span dir="ltr">&lt;<a href="mailto:bob@redivi.com" target="_blank">bob@redivi.com</a>&gt;</span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">I don&#39;t know what the performance would look like, but you can freeze the mutable Vector and then just use find. Here&#39;s an example:<div>

<br></div><div><div>import qualified Data.Vector.Mutable as M</div>
<div>import qualified Data.Vector as V</div><div>main :: IO ()</div><div>main = do</div><div>  v &lt;- M.replicate 10 &#39;a&#39;</div><div>  M.write v 2 &#39;b&#39;</div><div>  V.freeze v &gt;&gt;= print . V.find (==&#39;b&#39;)</div>


</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Mar 3, 2013 at 10:28 AM, 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style: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:&#39;courier new&#39;,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:&#39;courier new&#39;,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:&#39;courier new&#39;,monospace"><b><a name="13d3192bc25957b9_13d31842cc1fa5a3_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" 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></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></div></div></div></div>