Split a list into sized chunks, but not counting items failing predicate Motivation: Sometimes certain items in a list don't count towards your totals. For example, counting plane passengers in rows, where babies sit on a parent's laps.

Challenge: write a program to split a list of items into chunks. Each chunk (except possibly the last) is the same size, where size is defined as the number of items passing a predicate function. 

Rules:


Your program must take


a list of items
a positive integer chunk size
a predicate function (takes an item, and returns true or false)

You must return the input list split into chunks
Each chunk is a list of items
Overall the items must remain in the same order, with none disgarded
The number of items passing predicate in each chunk (except possibly the last) should match the input chunk size.
items failing the predicate should not count toward this size
Items failing the predicate are 


still included in the output chunks
allocated to the earliest chunk, in the case that a chunk is "full" but the next items are ones failing the predicate


thus the final chunk may not consist solely of items failing the predicate


The final chunk may be of size less than the chunk size because all the items have been accounted for.


Non exhaustive examples:

The simplest example is to consider 1s and 0s, where the predicate function is x ==&gt; x &gt; 0. In this case, the sum of each chunk must match the chunk size.


items: [], size: 2, predicate: x &gt; 0 --> either [] or [[]]
items: [0, 0, 0, 0, 0, 0], size: 2, predicate: x &gt; 0 --> [[0, 0, 0, 0, 0, 0]]
items: [0, 1, 1, 0], size: 2, predicate: x &gt; 0 --> [[0, 1, 1, 0]]
items: [0, 1, 1, 0, 1, 0, 0], size: 2, predicate: x &gt; 0 --> [[0, 1, 1, 0], [1, 0, 0]]
items: [0, 1, 0, 0, 1, 0, 1, 1, 0], size: 2, predicate: x &gt; 0 --> [[0, 1, 0, 0, 1, 0], [1, 1, 0]]


And let's finish with the plane passengers where babies sit on a parent's lap example. A for adult, b for baby, plane row is 3 seats wide, adult always listed before their baby:


items: [A, b, A, b, A, A, A, b, A, b, A, A, b], size: 3, predicate: x =&gt; x == A --> [[A, b, A, b, A], [A, A, b, A, b], [A, A, b]]
Brachylog, 37 bytes

hW&amp;t~c.k{↰₂ˢl}ᵐ;WxĖ∧.bhᵐ↰₂ᵐ∧.t↰₂ˢl≤W∧


Try it online!

I was pleasantly surprised to find that this - pretty much a restatement of the question - successfully terminates and produces correct output. 

Assumes the predicate is present as predicate 2 below this code. Outputs a list of lists ("chunks"), or false for an empty input. 

Explanation:

hW&amp;               % First input is W, the expected "weight" of each chunk
                  %  (i.e. the number of items passing predicate in each chunk)

t                 % Take the second input, the list of items
 ~c.              % Output is a partition of this list
    k{    }ᵐ      % For each partition (chunk) except the last, 
      ↰₂ˢ         %   Select the items in the chunk that pass the predicate
         l        %   Get the length of that
                  % (So now we have the list of the "weights" of each chunk)
            ;Wx   % Remove the input expected weight from this list, and 
               Ė  %  the result of this should be empty.
                  %  This verifies that the list of weights is either 
                  %  composed of all W-values, or is empty (when input is [0 0 0] for eg.)

    ∧.bhᵐ↰₂ᵐ      % And, the first element of each chunk (except the first) should
                  %  pass the predicate. This is another way of saying:
                  %  "Items failing the predicate are allocated to the earliest chunk"

    ∧.t↰₂ˢl≤W     % And, the final chunk (which we haven't constrained so far)
                  %  should have weight ≤ the input expected weight
                  %  This disallows putting everything in the final chunk and calling it a day!

    ∧             % (no further constraints on output)