local Rx = require 'Rx'

local AnonymousObservable = Rx.AnonymousObservable
local SchedulerArgument = Rx.internals.SchedulerArgument

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

return function(start, count, scheduler)

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

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

    scheduler = SchedulerArgument.validateWithDefault('Rx.Observable.range',
                                                      'currentThread', scheduler)

    return AnonymousObservable(function(observer)

        return scheduler:scheduleRecursive(0, function(i, recurseFn)

            if i < count then
                observer:onNext(start + i)
                recurseFn(i + 1)
            else
                observer:onCompleted()
            end

        end)

    end)

end
