Re: The game of life

2009-06-02 Thread Will Coleda
 I think that's something that perl6 could do better then APL.

If anyone would like to channel this APL energy into APL-on-parrot,
I'm sure it could some attention since the parrot 1.0 release. =)

http://parAPLegic.googlecode.com/

The original implementation was basically a proof of concept to
exercise some of the compiler tools and show that unicode on parrot
kind of worked.

If there's any interest, I can devote a few tuits to it this week.

-- 
Will Coke Coleda


Re: The game of life

2009-06-01 Thread John M. Dlugosz

yary not.com-at-gmail.com |Perl 6| wrote:

 http://www.dlugosz.com/Perl6/web/APL.html



The rho example is interesting, though it doesn't compile in current Rakudo.

1 2 3 ⍴¨ 10 would natively be written in Perl 6 as 10 »xx« (1,2,3).

perl6 complains about non-dwimmyness. 10 xx (1,2,3) gives
(10),(10,10),(10,10,10) , I think that matches the rho example.

  
Ah, I seem to remember something about which way the arrows point with a 
scalar.  I'll look it up.




So let’s implement the APL ⌿ operator which works on the columns of a matrix.

I think that's something that perl6 could do better then APL. From
what I understand, APL has some operators that work on columns and
other similar ones that work on rows, p6 has an opportunity to take
the direction/dimension as an argument. Which is something I'm
thinking about...
  

Good point.  Even infix operators can take adjectives!

Can the same effect be achieved with slices?

--John



Re: The game of life

2009-05-31 Thread John M. Dlugosz

yary not.com-at-gmail.com |Perl 6| wrote:

That was a big part of it... I'm glad Mark posted the APL snippet
because it got me to finally read up on the language that's been at
the back of my mind. Plus it's useful for p6 language discussion. APL
(and a successor, J) may still have a few tricks to lend.
  


Have you come across my Meditations on Perl taken from concepts in APL 
http://www.dlugosz.com/Perl6/web/APL.html?  
http://www.dlugosz.com/Perl6/web/APL.html


Thinking about who needs loops? was inspirational to me.

And it inspired the need for a column-reduce meta syntax.  It would be 
difficult to make macros for each op that don't step on each other, so 
an extensible way really needs to be inspired.




Re: The game of life

2009-05-31 Thread John M. Dlugosz

yary not.com-at-gmail.com |Perl 6| wrote:


Though I'm not quite sure that S03 covers some of the different-dimension cases.
my @table=
 (1,2,3;
  4,5,6); # is this the syntax to create 2d array?
  
No, the Capture on the right, although a Capture of Captures, will be 
flattened in the list context provided by list assignment.


   my @table= [1,2,3],[4,5,6];

will give you a Perl5-like array containing arrays for elements.

To give a true multi-dimensional array, you must declare it.  Although 
the lengths don't have to be fixed, you at least need the number of 
dimensions (which could involve ** so the dimensionality could be variable).


   my @table[2;3];

I pondered over feeding it another list of items in my APL essay.  I 
figured an iterator over the 2-D array should visit the elements in 
order.  So, use that to reshape the simple list.


--John




Re: The game of life

2009-05-31 Thread Daniel Ruoso
Em Qui, 2009-05-28 às 00:26 -0500, John M. Dlugosz escreveu:
 Mark J. Reed markjreed-at-gmail.com |Perl 6| wrote:
  Perhaps Perl 6 should not aspire to the expressiveness of APL. :)  As
  nice as it is that you can write Conway's Life in a one-liner(*), I
  think that a little verbosity now and then is a good thing for
  legibility
  (*) life ←{↑1 ω⌵.^3 4=+/,‾1 0 1◦.ϕ⊂ω}
 So how would that translate to Perl 6?  Both as an exact translation, 
 and how to do it better in a Perl way?

I didn't try to translate it, but rather I simply tried to implement it
in a way rakudo already runs So... here's my shot!

CODE
class Game {
  # XXX: rakudo doesn't know how to initialize arrays properly yet.
  has $.seed;
  has $.width;
  has $.height;
  multi method generation(Int $generation where 0) {
return @($.seed);
  }
  multi method generation(Int $generation) {
my @previous = self.generation($generation - 1);
my @new;
# XXX: rakudo doesn't autovivify arrays properly yet.
@new.push: [] for ^$.width;
for ^$.width X ^$.height - $x, $y {
  # XXX: there's an extra set of parens for rakudo
  my $value = [+] map { @previous[$^i][$^j] },
 ((grep { 0 = $_  $.width }, ($x-1)..($x+1))
  X
  (grep { 0 = $_  $.height }, ($y-1)..($y+1)));

  if @previous[$x][$y]  2 = ($value - 1) = 3 {
@new[$x][$y] = 1;
  } elsif !...@previous[$x][$y]  $value == 3 {
@new[$x][$y] = 1;
  } else {
@new[$x][$y] = 0;
  }
}
return @new;
  }
}

my $game = Game.new(
 :width(15), :height(15),
 :seed([
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 ],
  [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 ]
 ]));

my @g = $game.generation(4);
say .perl for @g;
/CODE


I guess my mindset is too much imperative for me to think in a more
elegant solution...

daniel



Re: The game of life

2009-05-30 Thread yary
On Thu, May 28, 2009 at 5:58 PM, John M. Dlugosz
2nb81l...@sneakemail.com wrote:
 I came upon a copy of A Programming Language in a similar way.  My Dad
 passed it on from a co-worker.  I don't recall how young I was, but it was a
 very interesting read.  Perhaps this attracts youngsters because of the
 strange letters?

That was a big part of it... I'm glad Mark posted the APL snippet
because it got me to finally read up on the language that's been at
the back of my mind. Plus it's useful for p6 language discussion. APL
(and a successor, J) may still have a few tricks to lend.

One APL feature already supported in p6 is the ability to use
operations on vectors of any dimension, even mixing dimensions.
Hyperoperators let us add two arrays easily, or multiply every element
in one array by a scalar, etc. (I haven't yet figured out
multi-dimensional arrays in Rakudo, are they implemented?)

Though I'm not quite sure that S03 covers some of the different-dimension cases.
my @table=
 (1,2,3;
  4,5,6); # is this the syntax to create 2d array?
my @row=4,0,1;
my @column=
(2;
 -1);

my @added_to_each_row = @row + @table; # is that (4,3,4 ; 8,5,7) ?
my @col_products = @column * @table; # is that (2,4,6 ; -4,-5,-6) ?

Which also brings up a small point in S09, is it OK to declare an
array with 0 elements in some directions, eg
my @month_calendar[7;5]; # seven days across, five weeks down
my @week[7;0];# one line across in the calendar. Same as @week[7]
my @Sundays[0;5]; # one line down. Different from @Sundays[5]

Another concept mentioned in the APL wiki page are reduction operators
that reduce the dimensionality of an array by one. For a
one-dimensional array, APLs and Perl6's reductions are the same- [+]
1,2,3 = 6 - run the op through all values, return a single value.

But in APL, you can reduce the addition operator on a 2-dimensional
array either by columns or by rows and get a 1-dimensional column or
row of sums. Or take a 3D array and get a rectangle of sums.
Presumably APL also lets you reduce a multi-dimensional vector down to
a grand total as well. I'm not very far in the p6 synopsis so I don't
know if that's a built-in possibility, but I didn't see it mentioned
in S03.

Less general is an APL operator (actually different ops for different
directions, IMO should be a single operator) that rotates vectors, eg
rotate left 2 transforms 1,2,3,4,5 = 3,4,5,1,2- and it also works
for arrays of any dimension. For some reason APL has 2 (or more?)
operators for rotating vectors, depending on which direction, eg
left-right vs up-down. I'd expect that a rotate operator would take a
vector saying the direction  magnitude of the rotation. Maybe APL
does do that, maybe perl6 does too, I'm not very far in the synopses.

It's been fun reading... if you are curious start with the APL
wikipedia page http://en.wikipedia.org/wiki/APL_(programming_language)
- install fonts as needed, they're also referenced from there. Then
move on to the links posted earlier about the game of life, they do a
great job of explaining the one-liners.
http://aplwiki.com/GameOfLife - short version, one generation, more
descriptive variable names
http://catpad.net/michael/apl/ - longer version, which, instead of
using a loop, makes a string copy of the short version N times and
'eval's that.

The wiki page for J, Iverson's APL successor
(http://en.wikipedia.org/wiki/J_programming_language ) has an
educational quicksort 1-liner:
quicksort=: (($:@(#[) , (=#[) , $:@(#[)) ({~ ?...@#)) ^: (1#)
which I think can be transformed into a more readable perl6 one-liner.
May try it out sometime...

J's wiki has a page of examples that makes me think, if I want to get
started in that language, there's a ton of good examples:
http://www.jsoftware.com/jwiki/Essays - enviable! One of the
programmer virtues... I'd like to see the better perl6 code collected
in a wiki too.


Re: The game of life

2009-05-28 Thread yary
If anyone wants to try tackling this, a longer APL one-liner is
referenced on the APL wikipedia page and discussed in length here:

http://catpad.net/michael/apl/

As an aside, APL was the first computer language I was exposed to.
When I was around 7 years old my aunt (who lived in Boston near MIT,
Harvard) had a computer scientist friend who gave her the APL: A
Programming Language book, after she bragged to him about a smart
nephew who liked typewriters... I liked all the symbols and sort of
understood a few bits of it...


Re: The game of life

2009-05-28 Thread yary
And a link explaining the shorter one-liner:

http://aplwiki.com/GameOfLife


Re: The game of life

2009-05-28 Thread Uri Guttman
 y == yary  not@gmail.com writes:

  y If anyone wants to try tackling this, a longer APL one-liner is
  y referenced on the APL wikipedia page and discussed in length here:

  y http://catpad.net/michael/apl/

  y As an aside, APL was the first computer language I was exposed to.
  y When I was around 7 years old my aunt (who lived in Boston near MIT,
  y Harvard) had a computer scientist friend who gave her the APL: A
  y Programming Language book, after she bragged to him about a smart
  y nephew who liked typewriters... I liked all the symbols and sort of
  y understood a few bits of it...

you must not realize that we have our own conway (part of the p6 design
cabal) in the perl world who has coded up in perl5 something that should
not have been released on a unsuspecting world. it is a monster called
selfgol and it not only plays life, it does much more. here is a wiki
entry about it. read the code at your own peril. if this gets translated
to p6 (and probably become much shorter too), i suspect the heavens will
fall, the sun will burn out and python will take over the universe! :)

http://www.perlfoundation.org/perl5/index.cgi?selfgol

from that page:

* Its a quine
(typo - should be It's - uri)
* Turns other programs into quines
* Plays Conway's Game of Life
* Animates a marquee banner

It does so without importing any modules, and without using a single if,
unless, while, until, for, foreach, goto, next, last, redo, map, or
grep.

uri

-- 
Uri Guttman  --  u...@stemsystems.com    http://www.sysarch.com --
-  Perl Code Review , Architecture, Development, Training, Support --
- Free Perl Training --- http://perlhunter.com/college.html -
-  Gourmet Hot Cocoa Mix    http://bestfriendscocoa.com -


Re: The game of life

2009-05-28 Thread John M. Dlugosz

yary not.com-at-gmail.com |Perl 6| wrote:

If anyone wants to try tackling this, a longer APL one-liner is
referenced on the APL wikipedia page and discussed in length here:

http://catpad.net/michael/apl/

As an aside, APL was the first computer language I was exposed to.
When I was around 7 years old my aunt (who lived in Boston near MIT,
Harvard) had a computer scientist friend who gave her the APL: A
Programming Language book, after she bragged to him about a smart
nephew who liked typewriters... I liked all the symbols and sort of
understood a few bits of it...

  
I came upon a copy of A Programming Language in a similar way.  My Dad 
passed it on from a co-worker.  I don't recall how young I was, but it 
was a very interesting read.  Perhaps this attracts youngsters because 
of the strange letters?