MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Apply function to parts of a list


On Mar 6, 3:08 am, guerom00 <guero... at gmail.com> wrote:
> Right now, I write :
>
> MapAt[2*#&,data,Table[{i,1},{i,1,Length[data]}]]
>
> It's OK but I isn't there another way to achieve this as I find this rather "involved" for such a simple thing ?

Hello,

I also found this problematic, so I made a little helper function:

myMapAt::usage="myMapAt[f,expr,{i,j,\[Ellipsis]}] applies function f
to the parts of expr at positions specified by {i,j,\[Ellipsis]}.
Unlike MapAt, the position specification may include List, Span and
All.";
myMapAt[f_,x_,pos_List] := MapAt[f,x, If[FreeQ[Unique[dummy]@@pos,All|
List|Span], pos, Cases[Array[{##}&,Take[Dimensions[x],Length[pos]]],
List@@Append[(Unique[dummy]@@pos) /. {All->HoldPattern[_],
{ii__}:>Alternatives[ii], Span[ii__]:>Alternatives@@(Range@@({ii}/.All-
>Max[Dimensions[x]]))}, HoldPattern[___]],\[Infinity]]]]
myMapAt[f_,x_,pos_]:=myMapAt[f,x,{pos}]

The usage you want would then be

myMapAt[2 # &, data, {All, 1}]

Hope that helps. Bug reports appreciated.


  • Prev by Date: Re: Apply function to parts of a list
  • Next by Date: Re: 64-bit problems with Mac version
  • Previous by thread: Re: Apply function to parts of a list
  • Next by thread: Re: Apply function to parts of a list