[Jprogramming] Index replacement

Dan Bron jf at dbron.net
Sat Nov 25 04:13:07 HKT 2006


JC,

Short answer:

	   k   =:  (&[) ("_1) (#/.~@:]`) (`({~ ~.)) (`:6) ~
	   l   =:  k (`(~.@:[)) (`]) }
	   
	   f   =:  ^&2  
	   v   =:  i.10
	   ind =:  3 5 3
	   
	   ind f l v
	0 1 2 81 4 25 6 7 8 9
	   
	   
A couple of points.  You say the result should be  r=:1 2 81 4 25 6 7 8 9  but:

	(A)  (#r) is  9  whereas  (#v)  is 10 , so I'm assuming you forgot 
	     the first element,  0  .

	(B)  You say  f  is  2^y  but  2^5  is  32  ,  not 25  , so I assume 
	     you meant y^2  (y-squared).  To that end, you might want to 
	     say  f=:  *:  rather  than  f=:  ^&2  .

Since you asked for "elegant", I decided to provide a tacit solution, to prove that such can done in the language.  If I were
writing this with intent of making it production code, I would probably make it explicit.  Explicit code, while not as beautiful
or cool, tends to be easier to read and maintain, and in complicated examples like this, usually faster.  For example, the tacit
code takes  ~.x  twice, which, in explicit code, could be calculated once and stored in a local name.

-Dan



More information about the Programming mailing list