<div dir="ltr">Hello,<div><br></div><div>I'd like to add the <span style="background-color:rgb(39,40,34);color:rgb(166,226,46);font-family:"Fira Code";font-size:12px;white-space:pre">mapAccumL</span> function to the <span style="background-color:rgb(39,40,34);color:rgb(102,217,239);font-family:"Fira Code";font-size:12px;font-style:italic;white-space:pre">vector</span> package.</div><div><br></div><div>Specifically the <span style="color:rgb(102,217,239);font-family:"Fira Code";font-size:12px;font-style:italic;white-space:pre;background-color:rgb(39,40,34)">Data.Vector.Storable</span> module, but it would also be useful other vector modules.</div><div><br></div><div>This is my attempt at an implementation:</div><div><br></div><div><div style="color:rgb(248,248,242);background-color:rgb(39,40,34);font-family:"Fira Code";font-size:12px;line-height:18px;white-space:pre"><div style="line-height:18px"><div>{-# <span style="color:rgb(249,38,114)">LANGUAGE</span> ScopedTypeVariables #-}</div><br></div><div><span style="color:rgb(166,226,46)">mapAccumL</span> <span style="color:rgb(249,38,114)">::</span> <span style="color:rgb(249,38,114)">forall</span> a b c. (<span style="color:rgb(102,217,239);font-style:italic">Storable</span> b, <span style="color:rgb(102,217,239);font-style:italic">Storable</span> c)</div><div>  <span style="color:rgb(249,38,114)">=></span> (a <span style="color:rgb(249,38,114)">-></span> b <span style="color:rgb(249,38,114)">-></span> (a, c))</div><div>  <span style="color:rgb(249,38,114)">-></span> a</div><div>  <span style="color:rgb(249,38,114)">-></span> <span style="color:rgb(102,217,239);font-style:italic">DVS</span>.<span style="color:rgb(102,217,239);font-style:italic">Vector</span> b</div><div>  <span style="color:rgb(249,38,114)">-></span> (a, <span style="color:rgb(102,217,239);font-style:italic">DVS</span>.<span style="color:rgb(102,217,239);font-style:italic">Vector</span> c)</div><div>mapAccumL f a vb <span style="color:rgb(249,38,114)">=</span> <span style="color:rgb(174,129,255)">DVS</span><span style="color:rgb(249,38,114)">.</span>createT <span style="color:rgb(249,38,114)">$</span> <span style="color:rgb(249,38,114)">do</span></div><div>  vc <span style="color:rgb(249,38,114)"><-</span> <span style="color:rgb(174,129,255)">DVSM</span><span style="color:rgb(249,38,114)">.</span>unsafeNew (<span style="color:rgb(174,129,255)">DVS</span><span style="color:rgb(249,38,114)">.</span>length vb)</div><div>  a' <span style="color:rgb(249,38,114)"><-</span> go <span style="color:rgb(174,129,255)">0</span> a vc</div><div>  return (a', vc)</div><div>  <span style="color:rgb(249,38,114)">where</span> go <span style="color:rgb(249,38,114)">::</span> <span style="color:rgb(102,217,239);font-style:italic">Int</span> <span style="color:rgb(249,38,114)">-></span> a <span style="color:rgb(249,38,114)">-></span> <span style="color:rgb(102,217,239);font-style:italic">DVS</span>.<span style="color:rgb(102,217,239);font-style:italic">MVector</span> s c <span style="color:rgb(249,38,114)">-></span> <span style="color:rgb(102,217,239);font-style:italic">ST</span> s a</div><div>        go i a0 vc <span style="color:rgb(249,38,114)">=</span> <span style="color:rgb(249,38,114)">if</span> i <span style="color:rgb(249,38,114)"><</span> <span style="color:rgb(174,129,255)">DVS</span><span style="color:rgb(249,38,114)">.</span>length vb</div><div>          <span style="color:rgb(249,38,114)">then</span> <span style="color:rgb(249,38,114)">do</span></div><div>            <span style="color:rgb(249,38,114)">let</span> (a1, c1) <span style="color:rgb(249,38,114)">=</span> f a0 (<span style="color:rgb(174,129,255)">DVS</span><span style="color:rgb(249,38,114)">.</span>unsafeIndex vb i)</div><div>            <span style="color:rgb(174,129,255)">DVSM</span><span style="color:rgb(249,38,114)">.</span>unsafeWrite vc i c1</div><div>            go (i <span style="color:rgb(249,38,114)">+</span> <span style="color:rgb(174,129,255)">1</span>) a1 vc</div><div>          <span style="color:rgb(249,38,114)">else</span> return a0</div><div>{-# <span style="color:rgb(249,38,114)">INLINE</span> mapAccumL #-}</div></div></div><div><br></div><div>The implementation should obey the following law:</div><div><br></div><div><div style="color:rgb(248,248,242);background-color:rgb(39,40,34);font-family:"Fira Code";font-size:12px;line-height:18px;white-space:pre"><div><div style="line-height:18px"><div><span style="color:rgb(249,38,114)">import</span> <span style="color:rgb(249,38,114)">qualified</span> Data.List                         <span style="color:rgb(249,38,114)">as</span> L</div><div><span style="color:rgb(249,38,114)">import</span> <span style="color:rgb(249,38,114)">qualified</span> Data.Vector.Storable              <span style="color:rgb(249,38,114)">as</span> DVS</div><div><span style="color:rgb(249,38,114)"></span></div></div></div><div><br></div><div>(<span style="color:rgb(174,129,255)">DVS</span><span style="color:rgb(249,38,114)">.</span>toList <span style="color:rgb(249,38,114)"><$></span> <span style="color:rgb(174,129,255)">DVS</span><span style="color:rgb(249,38,114)">.</span>mapAccumL f a (<span style="color:rgb(174,129,255)">DVS</span><span style="color:rgb(249,38,114)">.</span>fromList bs)) <span style="color:rgb(249,38,114)">===</span> <span style="color:rgb(174,129,255)">L</span><span style="color:rgb(249,38,114)">.</span>mapAccumL f a bs</div></div></div><div><br></div><div>Cheers,</div><div><br></div><div>-John</div><div><br></div></div>