Idioms in J move over to Mathematica nicely.

p = Partition[{1, 5, 1, 7, 4, 1, 4, 2, 5, 2, 5, 7, 6, 2, 7, 8, 8, 5}, 2]

  {{1, 5}, {1, 7}, {4, 1}, {4, 2}, {5, 2}, {5, 7}, {6, 2}, {7, 8}, {8, 5}}


Join[p, Reverse[p, 2]]

   {{1, 5}, {1, 7}, {4, 1}, {4, 2}, {5, 2}, {5, 7}, {6, 2}, {7, 8}, {8, 5},

   {5,1}, {7, 1}, {1, 4}, {2, 4}, {2, 5}, {7, 5}, {2, 6}, {8, 7}, {5, 8}}


That 2 in the argument to Reverse is a level spec so in effect I am asking
to reverse all the items of p.

Sort[Join[p, Reverse[p, 2]]]


{{1, 4}, {1, 5}, {1, 7}, {2, 4}, {2, 5}, {2, 6}, {4, 1}, {4, 2}, {5, 1},

{5, 2}, {5, 7}, {5, 8}, {6, 2}, {7, 1}, {7, 5}, {7, 8}, {8, 5}, {8, 7}}


We can jazz it up too with a little infix and postfix syntax:


 p ~Join~ Reverse[p, 2] // Sort // TableForm


1 4

1 5

1 7

2 4

2 5

2 6

4 1

4 2

5 1

5 2

5 7

5 8

6 2

7 1

7 5

7 8

8 5

8 7


?

?



 Roger wrote:

> Much of the code in the previous J programs/expressions has to do with
> transforming between representations.  The following is a fairer comparison
> between the WL program and an equivalent J program.
>
> Society is table of pairs, each pair an integer representing a person's
> name and another representing a person in the first person's social
> network.  Thus:
>
>    p=: _2 ]\ 1 5 1 7 4 1 4 2 5 2 5 7 6 2 7 8 8 5
>    p
> 1 5
> 1 7
> 4 1
> 4 2
> 5 2
> 5 7
> 6 2
> 7 8
> 8 5
>
> To modify p so that anyone in another's network includes that person in
> their network:
>
>    p , |."1 p
> 1 5
> 1 7
> 4 1
> 4 2
> 5 2
> 5 7
> 6 2
> 7 8
> 8 5
> 5 1
> 7 1
> 1 4
> 2 4
> 2 5
> 7 5
> 2 6
> 8 7
> 5 8
>
> If you want it sorted:
>
>    /:~ p , |."1 p
> 1 4
> 1 5
> 1 7
> 2 4
> 2 5
> 2 6
> 4 1
> 4 2
> 5 1
> 5 2
> 5 7
> 5 8
> 6 2
> 7 1
> 7 5
> 7 8
> 8 5
> 8 7
>
> Therefore:
>
> WL:
>
> symSoc = society /.
>  {a_Integer, b_} :> {a, Union[b, Cases[society, {x_, {___, a, ___}} :> x]]}
>
>
> J:
>
> p,|."1 p
> /:~p,|."1 p
>
> ?
> ?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to