Re: How would I do this in Perl6? #1

2004-09-16 Thread Michele Dondi
On Tue, 14 Sep 2004, Matt Diephouse wrote:

 Or in Perl 5, which has to use 2 subs to have the same interface (code
  ^
  ^

Huh?!?

  #!/usr/bin/perl -l
  
  use strict;
  use warnings;
  
  sub extp ($@) {
  my $t=shift;
  @$t ? return @_ :
pack shift @$t, extp $t, @_;
  }
  
  sub extup ($@) {
  my $t=shift;
  @$t ? return @_ :
unpack shift @$t, (extup $t, @_);
  }
  
  print for extup [qw/L* w u/], 
extp [qw/u w L*/], 1..5;
  
  __END__


-- 
 4 words: it-was-a-joke!
4 words: he-has-no-clue!!
- Uri Guttman on clpmisc (slightly edited)


Re: How would I do this in Perl6? #1

2004-09-16 Thread Michele Dondi
On Thu, 16 Sep 2004, Michele Dondi wrote:

 On Tue, 14 Sep 2004, Matt Diephouse wrote:
 
  Or in Perl 5, which has to use 2 subs to have the same interface (code
 ^^^
 ^^^

 Huh?!?
[code snipped]

Oops! Sorry, I hadn't noticed that! To be honest I have not checked what
the interface you proposed actually was. The way I've written it is how
I'd do it *in any case*. But then the point is *not* writing a sub to do
that, for it is a priori obvious that the are tons of WTDI:  I'd be rather
interested in the possibility of doing it *the way I proposed* in the OP,
which I find conceptually appealing.


Michele
-- 
'Most any OS will also support either read(2) or some equivalent. Most
support read(2) directly: Win32 does, though it also has its own set
of functions, in the classic Microsoft fashion of not doing a thing
well once when you can do it badly five times.
- Ben Morrow in clpmisc, Re: Perl's read() vs. sysread()


Re: How would I do this in Perl6? #1

2004-09-16 Thread Michele Dondi
On Tue, 14 Sep 2004, Larry Wall wrote:

 : So what I would like to do is (i) Cmap the list of templates to a list 
 : of curried closures in which the first parameter is fixed to each given 
 : template, (ii) Creduce this list by means of right pipe binop
 : ( C ==  ) with a starting value (leftmost) of, say, @input.
 
 It probably depends on just how we implement infix:==.  Looking for
 a righthand argument that is curried to have all of its arguments
 except for a list is one way to look at it.  But all our currying so
 far is explicit, and == in some sense wants to auto-curry its right
 side to return some function with a missing list, even if it isn't
 written that way, which means it's perhaps acting more like a macro
 than a first-class function.  But just because the actual binary ==

Well, then maybe == is not the right infix operator. Mathematically one 
has the binary associative operation of composition. Will Perl6 provide 
such an operator? It seems that == (or == for a more traditional 
approach) could not be the right one since *in this context* it would pass 
its right argument to its left one, and not the output of the former as 
wanted ()...

Also, mathematically, if one has a function of, say, two variables,

   f : A x B - C

it's common to indicate the function obtained by fixing one, say the 
first, of them by

   f(a,-) : B - C

So similarly Perl may support a similar syntax by means of suitable 
placeholder variables (obviously in a different sense than the already 
existing one!)

 arguments of each == autocurry their own righthand arguments.  I suspect
 there's a way to write it, since we defined the splat list as just a
 funky named parameter.  Probably something like
 
 my $result =
   (@input ==
   reduce operator:==,
  map { *pack.assuming(:template($_)) }, @templates
   );

So it seems that currying will be allowed only on named parameters, won't 
it?!?

Well, I guess this won't matter much here, but I've the *impression* that 
Perl6 rich parameter passing mechanism will be *too* powerful (to be 
flexible enough).
 
 Mind you, I wouldn't write such code anywhere I expected anyone with
 less than a PhD to stumble across, even if I ain't got one myself.

Well I do *not* have a PhD myself, but it seems I have an inclination for 
functional languages-like constructs.

And maybe I'm simply a strange guy, but a solution along the lines of that 
quoted above would be the most clear satisfying for me, for it makes 
obvious what is done in turn: (i) the list of templates is mapped to a 
list of functions, (ii) this list is reduce()d by means of composition of 
functions, (iii) the resulting function is evalutated on @input. And 
there's been no need to introduce or use intermediate variables (apart 
the default one, $_).


Michele
-- 
 [are there] Any areas of mathematical knowledge or skills that we lost?
Yes, but I don't remember what they are.
- Kevin Foltinek on sci.math [edited]


Re: How would I do this in Perl6? #1

2004-09-14 Thread Aaron Sherman
On Tue, 2004-09-14 at 06:45, Michele Dondi wrote:
[... snip ...]
 Now I want to take a list of templates, say $t1, ... $tn and get the 
 result of
 
   $result = pack $tn, ... pack $t2, pack $t1, @input;

Assuming Perl 6 has a pack, which it may not:

for @t {
$result = pack $_, ($result // @input);
}

no?

-- 
 781-324-3772
 [EMAIL PROTECTED]
 http://www.ajs.com/~ajs



Re: How would I do this in Perl6? #1

2004-09-14 Thread Michele Dondi
On Tue, 14 Sep 2004, Aaron Sherman wrote:

 Assuming Perl 6 has a pack, which it may not:

I know: this is one of the reasons why I wrote (hopefully *clearly*
enough) that I was just using it as an example. A user defined pack()
whoud do just the same here, anyway.

   for @t {
   $result = pack $_, ($result // @input);
   }

It's hard to believe, but surprising as it may be, I could have thought of 
a (ton, possibly, of) solution(s) along those lines (the C// op is 
cool though).

The point here is that I find it conceptually appealing, elegant and smart 
to be able to use functions as primitive data types as in functional 
languages. I'm not saying that I want to enforce only such constructs, but 
I'd like to be allowed to use them. So the question is: even being aware 
of more traditional solutions of the kind of that cited above, will it 
be possible to adopt one similar to that hinted to in my previous post? 
And if so, then how would it probably look like?

 no?

Yes...

...So what?!?


Michele
-- 
It's not considered polite in my circles to tell others that
they are being impolite, unless the others are your children.  
Looks like I'm not very polite, doesn't it?
- Mike Prager on comp.text.tex, thread usenet news group style


Re: How would I do this in Perl6? #1

2004-09-14 Thread Matt Diephouse
On Tue, 14 Sep 2004 12:45:17 +0200 (CEST), Michele Dondi
[EMAIL PROTECTED] wrote:
 Now I want to take a list of templates, say $t1, ... $tn and get the
 result of
 
   $result = pack $tn, ... pack $t2, pack $t1, @input;
 
 without actually writing the whole thing. To my knowledge and great
 pleasure Perl6 will support currying and a builtin reduce/fold operator.

You could do this with a recursive function (They're your friends. Really.).

sub extended_pack( [EMAIL PROTECTED] =  ($t1, ... , $tn), [EMAIL PROTECTED] ) {
# start with the last template so we can use tail recursion
@input = pack pop(@templates), @input;
# if there aren't any more templates, we're done
return @input if not @templates;
return extended_pack(@templates, [EMAIL PROTECTED]);
}

Or in Perl 5, which has to use 2 subs to have the same interface (code
untested):

sub extended_pack {
my (@input)   = @_;
my @templates = ($t1, ..., $tn);
return extended_pack_rec([EMAIL PROTECTED], [EMAIL PROTECTED]);
}

sub extended_pack_rec {
my ($input, $templates) = @_;
@$input = pack pop(@$templates), @$input;
return @$input if not @$templates;
# special goto magic for tail recursion
@_ = ($input, $templates);
goto extended_pack_rec;
}

I prefer Perl 6.

-- 
matt