function RxBrioUtils.map(m)return Rx.map(function(...)local W=select("\035",...)local h={}local o if W==1 then if Brio.isBrio(...)then table.insert(h,...)o=(...):GetPackedValues()else o={[1]=...}end else o={}for m,W in pairs({...})do if Brio.isBrio(W)then table.insert(h,W)o[m]=W:GetValue()else o[m]=W end end o.n=W end local D=table.pack(m(table.unpack(o,1,o.n)))local s={}for m=1,D.n,1 do local W=D[m]if Brio.isBrio(W)then table.insert(h,W)s[m]=W:GetValue()else s[m]=W end end return BrioUtils.first(h,table.unpack(s,1,s.n))end)end function RxBrioUtils._mapResult(m)return function(...)local W=select("\035",...)if W==0 then return BrioUtils.withOtherValues(m)elseif W==1 then if Brio.isBrio(...)then return BrioUtils.first({m;...},(...):GetValue())else return BrioUtils.withOtherValues(m,...)end else local h={m}local o={}for m,W in pairs({...})do if Brio.isBrio(W)then table.insert(h,W)o[m]=W:GetValue()else o[m]=W end end return BrioUtils.first(h,unpack(o,1,W))end end end

function RxBrioUtils.map(project)
	return Rx.map(function(...)
		local n = select("#", ...)
		local brios = {}
		local args

		if n == 1 then
			if Brio.isBrio(...) then
				table.insert(brios, (...))
				args = (...):GetPackedValues()
			else
				args = {[1] = ...}
			end
		else
			args = {}
			for index, item in pairs({...}) do
				if Brio.isBrio(item) then
					table.insert(brios, item)
					args[index] = item:GetValue() -- we lose data here, but I think this is fine
				else
					args[index] = item
				end
			end
			args.n = n
		end

		local results = table.pack(project(table.unpack(args, 1, args.n)))
		local transformedResults = {}
		for i=1, results.n do
			local item = results[i]
			if Brio.isBrio(item) then
				table.insert(brios, item) -- add all subsequent brios into this table...
				transformedResults[i] = item:GetValue()
			else
				transformedResults[i] = item
			end
		end

		return BrioUtils.first(brios, table.unpack(transformedResults, 1, transformedResults.n))
	end)
end

function RxBrioUtils._mapResult(brio)
	return function(...)
		local n = select("#", ...)
		if n == 0 then
			return BrioUtils.withOtherValues(brio)
		elseif n == 1 then
			if Brio.isBrio(...) then
				return BrioUtils.first({brio, (...)}, (...):GetValue())
			else
				return BrioUtils.withOtherValues(brio, ...)
			end
		else
			local brios = { brio }
			local args = {}

			for index, item in pairs({...}) do
				if Brio.isBrio(item) then
					table.insert(brios, item)
					args[index] = item:GetValue() -- we lose data here, but I think this is fine
				else
					args[index] = item
				end
			end

			return BrioUtils.first(brios, unpack(args, 1, n))
		end
	end
end
