local Observable = require "rx.observable"
local util = require "rx.util"

--- Returns a new Observable that produces the values of the original transformed by a function.
-- @arg {function} callback - The function to transform values from the original Observable.
-- @returns {Observable}
function Observable:map(callback)
    return Observable.create(function(observer)
        callback = callback or util.identity

        local function onNext(...)
            return util.tryWithObserver(observer, function(...)
                return observer:onNext(callback(...))
            end, ...)
        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
