I was playing around with perl 6. I was interested in exploring array rotations. The boring way is to use perl 6 ‘rotate’ function.

A more fun way is to see if I can make perl 6 look more like APL. The Phi(?) symbol in APL is used to perform a rotation. So let’s define it. Furthmore, the [& … ] operator can be used to infix a function. So here’s how you can fake APL:

my @a1 := 4, 5 ,6;
say(@a1.rotate(1)); # (5 6 4)
# or equivalently
#
# Φ in vim is C-k F* (Unicode U+03A6)
sub Φ ($n, @arr) { @arr.rotate($n); }
say 1 [&Φ] @a1 ; # also (5 6 4)

There might be more elegant ways of defining infix operators, such as hacking the perl 6 grammar, but I haven’t reached that far yet.

If you think rotating by 1 is a useful idea, then you can define a sub for that too:

sub Φ1(@arr) { Φ(1, @arr); };
say (Φ1 @a1); # also (5 6 4)

Maybe there are better ways to do currying, I’m not sure. Some of the documentation for P6 are over a decade old, so it’s difficult to know if it is obsolete.

Kinda fun, and it will be interesting to see if one could implement a chunk of the APL language in P6.

From what very little I’ve seen of P6, it seems that Haskell wins at least a little bit on infixing and currying.

Anyway, it was just a bit of fun. If anyone can come up with a better implementation (which shouldn’t be hard), then I’d be interested in hearing about it.

**Update 15-Dec-2016**

I discovered that P6 has an infix operator. So you can now define phi as

sub infix:<Φ> ($n, @arr) { @arr.rotate($n); }

and make it work as expected:

say 1 Φ @a1 ;

Awsome!

**Update 17-Dec-2016**

The above code is available as a gist. As a bonus, I have added the “÷” the operator. I hadn’t figured out how to create anonymous labmda functions yet, so the implementation is crude. It could also be improved by accepting a scalar as a denominator, too.Presumably that’s a job for generics.

### Like this:

Like Loading...

*Related*

## About mcturra2000

Computer programmer living in Scotland.

The following comments were made on Twitter by https://twitter.com/raiph_mellor

raiph_mellor @raiph_mellor 1h1 hour ago

@mcturra2000 Curry with `.assuming` eg `my \L = 4,5,6; sub infix: (\n, \a) { a.rotate(n) }; my &Φ1 = &infix:.assuming(1); say Φ1 L`

0 replies 0 retweets 0 likes

raiph_mellor @raiph_mellor 1h1 hour ago

@mcturra2000 Or less: `my \l = 4,5,6; sub infix: (\n, \a) { a.rotate(n) }; say 1 Φ a` # also (5 6 4)