function y=map(f,x,dim)
% map - Map function over collection
%
% map :: (A->B), collection(A) -> collection(B).
%
% collection(A) ::= seq A | {D->A} | [D->A].
%
% This implementation handles cell arrays and ordinary arrays.
% Sequences should be taken care of by data/map

if isempty(x)
	y=x;
elseif iscell(x),
	y=cellmap(f,x);
elseif isnumeric(x)
	if nargin<3, y=reshape(f(x),size(x));
	elseif dim==1, y=maprows(f,x);
	elseif dim==2, y=mapcols(f,x);
	else error(sprintf('Cannot map over dimension %d',dim));
	end
else
	error(sprintf('Cannot map over objects of class %s',class(x)));
end
