[Jprogramming] Request for code critique '&. Hello'
Fred Bone
Fred.Bone at dial.pipex.com
Sat Feb 11 01:56:52 HKT 2006
On 9 Feb 2006 at 17:00, John Randall said:
[...]
> I am thinking of a case like
>
> if. c1 y. do. a1 y.
> elseif. c2 y. do. a2 y.
> elseif. c3 y. do. a3 y.
> elseif. 1 do. default y.
> end.
>
> I can rewrite this as
>
> ((default`a3 at .c3)`a2 at .c2)`a1 at .c1
>
> but it is very easy to go wrong.
I'm very much a newcomer to J, but is it not possible to approach this
using the I. function?
c1 =. >&5 NB. some arbitrary condition functions
c2 =. >&3
c3 =. <&0
a1 =. *: NB. and some arbitrary action functions
a2 =. >:
a3 =. <:
default =. ]
(so: if > 5, square;
elseif > 3, increment;
elseif < 0, decrement;
else identity)
{.I.(c1,c2,c3,c4,1:) 4 NB. which function?
1
(a1`a2`a3`default @. 1) 4 NB. function 1 is increment
5
NB. Now combine the expressions
(a1`a2`a3`default @. ({. at I.@(c1,c2,c3,1:) )) 4
5
(a1`a2`a3`default @. ({. at I.@(c1,c2,c3,1:) )) 6
36
(a1`a2`a3`default @. ({. at I.@(c1,c2,c3,1:) )) 2
2
(a1`a2`a3`default @. ({. at I.@(c1,c2,c3,1:) )) _3
_4
Of course it looks horrible ... especially if you want it to handle a
vector argument:
(a1`a2`a3`default @. ({. at I.@(c1,c2,c3,1:) ))"(0) 6 4 3 _3
36 5 3 _4
Or have I completely misunderstood what you're trying to do?
More information about the Programming
mailing list