local function notEmpty(array)
    return #array > 0
end

--------------------------------------------------------------------------------

return function(source, count, skip)

    if type(count) ~= 'number' then
        error("Rx.Observable:bufferWithCount: count must be a number", 2)
    end

    if count < 1 or count > (2 ^ 53) then
        error("Rx.Observable:bufferWithCount: count must be a positive integer < 2^53", 2)
    end

    skip = skip or count -- if skip is not provided, default is count
    if type(skip) ~= 'number' then
        error("Rx.Observable:bufferWithCount: skip must be a number", 2)
    end

    if skip < 1 or skip > (2 ^ 53) then
        error("Rx.Observable:bufferWithCount: skip must be a positive integer < 2^53", 2)
    end

    return source:windowWithCount(count, skip)
        :flatMap(function(window)
            return window:toArray()
        end)
        :filter(notEmpty)
end
