local Observable = require 'rx.observable'

--- Returns a new Observable that produces the values from the original with duplicates removed.
-- @returns {Observable}
function Observable:distinct()
    return Observable.create(function(observer)
        local values = {}

        local function onNext(x)
            if not values[x] then
                observer:onNext(x)
            end

            values[x] = true
        end

        local function onError(e)
            return observer:onError(e)
        end

        local function onCompleted()
            return observer:onCompleted()
        end

        return self:subscribe(onNext, onError, onCompleted)
    end)
end
