Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here are two quick solutions for you that are both flexible. One is easy to read, but only slightly faster than your proposed solution. The other is quite fast, but is a bit cryptic to read. And note that both of my proposed algorithms will work for lists of anything, not just numeric ordered lists.</p> <p>Here is the "easy-to-read" one. Call by <code>n_length_chunks(List,Chunksize)</code>. For example, to get a list of chunks 2 long, call <code>n_length_chunks(List,2)</code>. This works for chunks of any size, ie, you could call <code>n_length_chunks(List,4)</code> to get <code>[[1,2,3,4],[5,6,7,8],...]</code></p> <pre><code>n_length_chunks([],_) -&gt; []; n_length_chunks(List,Len) when Len &gt; length(List) -&gt; [List]; n_length_chunks(List,Len) -&gt; {Head,Tail} = lists:split(Len,List), [Head | n_length_chunks(Tail,Len)]. </code></pre> <p>The <em>much faster one</em> is here, but is definitely harder to read, and is called in the same way: <code>n_length_chunks_fast(List,2)</code> (I've made one change to this compared with the one above, in that it pads the end of the list with <code>undefined</code> if the length of the list isn't cleanly divisible by the desired chunk length.</p> <pre><code>n_length_chunks_fast(List,Len) -&gt; LeaderLength = case length(List) rem Len of 0 -&gt; 0; N -&gt; Len - N end, Leader = lists:duplicate(LeaderLength,undefined), n_length_chunks_fast(Leader ++ lists:reverse(List),[],0,Len). n_length_chunks_fast([],Acc,_,_) -&gt; Acc; n_length_chunks_fast([H|T],Acc,Pos,Max) when Pos==Max -&gt; n_length_chunks_fast(T,[[H] | Acc],1,Max); n_length_chunks_fast([H|T],[HAcc | TAcc],Pos,Max) -&gt; n_length_chunks_fast(T,[[H | HAcc] | TAcc],Pos+1,Max); n_length_chunks_fast([H|T],[],Pos,Max) -&gt; n_length_chunks_fast(T,[[H]],Pos+1,Max). </code></pre> <p>Tested on my (really old) laptop:</p> <ul> <li>Your proposed solution took about 3 seconds.</li> <li>My slow-but-readable one was slightly faster and takes about 1.5 seconds (still quite slow)</li> <li>My fast version takes about 5 milliseconds.</li> <li>For completeness, Isac's solution took about 180 milliseconds on my same machine.</li> </ul> <p>Edit: wow, I need to read the complete question first. Oh well I'll keep here for posterity if it helps. As far as I can tell, there's not a good way to do this using list comprehensions. Your original version is slow because each iteration of <code>sublist</code> needs to traverse the list each time to get to each successive <code>X</code>, resulting in complexity just under O(N^2).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload