title: Most idiomatic way to write batchesOf size seq in F#
body: <p>I'm trying to learn F# by rewriting some C# algorithms I have into idiomatic F#. </p>
  
  <p>One of the first functions I'm trying to rewrite is a batchesOf where:</p>
  
  <pre><code>[1..17] |&gt; batchesOf 5
  </code></pre>
  
  <p>Which would split the sequence into batches with a max of five in each, i.e:</p>
  
  <pre><code>[[1; 2; 3; 4; 5]; [6; 7; 8; 9; 10]; [11; 12; 13; 14; 15]; [16; 17]]
  </code></pre>
  
  <p>My first attempt at doing this is kind of ugly where I've resorted to using a mutable <strong>ref</strong> object after running into errors trying to use <strong>mutable</strong> type inside the closure. Using <strong>ref</strong> is particularly unpleasant since to dereference it you have to use the <strong>!</strong> operator which when inside a condition expression can be counter intuitive to some devs who will read it as <strong>logical not</strong>. Another problem I ran into is where Seq.skip and Seq.take are not like their Linq aliases in that they will throw an error if <strong>size</strong> exceeds the size of the sequence. </p>
  
  <pre><code>let batchesOf size (sequence: _ seq) : _ list seq =
      seq {
          let s = ref sequence
          while not (!s |&gt; Seq.isEmpty)  do
              yield !s |&gt; Seq.truncate size |&gt; List.ofSeq
              s := System.Linq.Enumerable.Skip(!s, size)
      }
  </code></pre>
  
  <p>Anyway what would be the most elegant/idiomatic way to rewrite this in F#? Keeping the original behaviour but preferably without the <strong>ref</strong> mutable variable.</p>