Re: reduce metaoperator on an empty list

2005-05-19 Thread Stuart Cook
On 5/19/05, Brad Bowman [EMAIL PROTECTED] wrote:
 Can't the appropriate identity just be prepended?
 
   my @a;
   [+] @a; # 0? exception?
 [+] (0, @a);
 
   [*] @a; # 1? exception?
 [*] (1, @a);
 
   [] @a; # false?
 [] (-Inf, @a);  # ???

Wow, that's actually pretty elegant, and it has the benefit of
explicitly TELLING the reader what you intended to do.

Another option (depending on your situation) is to use 'err' to
replace the resultant 'undef' with a value of your choosing, i.e.

[*]  @coefficients   err 1;
[+]  @scores err 0;
[] @preconditions  err 1;

etc. (assuming I got the precedence right)

I think all these rather nice workarounds, combined with the hairiness
and complexity of trying to come up with default answers, make a
really strong case for undef/fail being the right choice here.


Stuart


hyperoperators and multi-dimensional datastructures

2005-05-19 Thread Anthony Heading
Is there a way to target hyperoperators at different axes of a
multi-dimensional array?  This is an attractive feature of
various APL-like languages, viz. e.g. in J:
   a =. 2 5 $ i. 7  - a simple 2-by-5 array
   a
0 1 2 3 4   - like this
5 6 0 1 2
   +/1 a  - sum reduce over axis 1
10 14
   +/2 a  - sum reduce over axis 2
5 7 2 4 6
Anthony


Re: reduce metaoperator

2005-05-19 Thread Edward Cherlin
On Sat, May 07, 2005 at 010008PM -0700, Larry Wall wrote:

 On the other hand, since we've distinguished hyperops on
 infixes from hyperops on unaries, maybe an infix hyperop in
 unary position just does the thing to itself:

 @squares = * @list;

 which gives us a sum-of-squares that looks like this:

 @sumofsquares = [+] * @list;

 On the gripping hand, I can't think of any other operators I'd
 be likely to want to do that to, 

Ken Iverson had the same problem in the beginning.
http://www.vector.org.uk/typography/pview.htm
Even after tasting the fruits of generalizing the  notation of 
mathematics to the form f/ that permitted the use of functions 
other than addition, it took some time before I recognized the 
advantages of a corresponding generalization of the inner or 
matrix product to allow the use of functions other than addition 
and multiplication.

 so forget it.  Not worth teaching people.  

In languages that have reduce, scan, and inner product, it turns 
out that there are a number of other useful cases besides sum 
and sum of products. Minimax and maximin in game theory, for 
example. Boolean inner products on incidence matrices give a 
result showing which points are connected by paths of length 
two, and higher powers give connectedness on paths of length 
'n', up to the point at which all path-connected components have 
been completely identified. 'And of equals' matches vectors 
(lists) against rows or columns of a table, depending on the 
order of the arguments. There are a number of others in the 
literature, some of them quite common in use. Once reduce, scan, 
and inner product can apply to user-defined functions, the 
possibilities open wide.

Scan is an extension of reduce. It takes every initial segment of 
its vector argument, and does a reduce on it. (For right-to-left 
evaluation as in APL and J, this requires order N squared time 
on non-commutative functions.) J has extended scan to a version 
that operates on final segments. Examples at
file:///usr/share/j504/system/extras/help/dictionary/intro14.htm

This paper applies scans to inner product functions.
http://portal.acm.org/citation.cfm?id=882077
The inner-product scan allows a straight-forward calculation of 
interest-bearing accounts or annuities without a loop in APL.

 Larry 
-- 
Edward Cherlin
Generalist  activist--Linux, languages, literacy and more
A knot! Oh, do let me help to undo it!
--Alice in Wonderland
http//cherlin.blogspot.com


Re: reduce metaoperator on an empty list

2005-05-19 Thread Michele Dondi
On Wed, 18 May 2005, Matt Fowles wrote:
All~
What does the reduce metaoperator do with an empty list?
Interesting. Mathematically an empty sum is zero and an empty product is 
one. Maybe each operator {c,s}hould have an associated method returning 
its neutral element for [] to use it on empty lists, so that it would 
probably return undef on empty lists.

Just my 2 Eurocents,
Michele
--
It was part of the dissatisfaction thing.  I never claimed I was a
nice person.
- David Kastrup in comp.text.tex, Re: verbatiminput double spacing


Re: reduce metaoperator on an empty list

2005-05-19 Thread Michele Dondi
On Wed, 18 May 2005, Rob Kinyon wrote:
1) undef (which may or may not contain an exception), or
2) some unit/identity value that is a trait of the operator,
depending on whether or not people think (2) is actually a good idea.
I would think that the Principle of Least Surprise points to (1),
I don't think so. I, for one, would expect [+]; to return zero and [*]; to 
return 1. But that's only because I {trust,expect} perl(6) to be smart 
enough to DWIM.

Michele
--
\renewcommand\labelitemi{\textcolor{yellow}{\textbullet}}
would change the colour of the first-level bullet, and improve the
document so's i can't see the bullets.
- Robin Fairbairns in comp.text.tex


Complex Arithmetic

2005-05-19 Thread Edward Cherlin
There was a discussion of the principal value of square root on 
this list some time back, making the point that for positive 
real numbers the positive square root is the value of the 
standard function. In the complex plane it is desirable to 
define the principal value at every point so as to produce a 
minimum of surprises, such as sudden jumps at discontinuities. 
However, there must necessarily be a line of discontinuity. 
Similarly for other complex-valued functions, particularly the 
trig and inverse trig functions, some of which are 
infinite-valued. 

It turns out that the domain and range and the location of the 
cut lines have to be worked out separately for different 
functions. Mathematical practice is not entirely consistent in 
making these decisions, but in programming, there seems to be 
widespread agreement that the shared definitions used in the 
APL, Common LISP, and Ada standards are the best available.

Do we want to get into all of this in Perl6?
-- 
Edward Cherlin
Generalist  activist--Linux, languages, literacy and more
A knot! Oh, do let me help to undo it!
--Alice in Wonderland
http://cherlin.blogspot.com


Re: reduce metaoperator on an empty list

2005-05-19 Thread Edward Cherlin
On Wednesday 18 May 2005 17:57, Matt Fowles wrote:
 All~

 What does the reduce metaoperator do with an empty list?

Here is the last answer from Ken Iverson, who invented reduce in 
the 1950s, and died recently.
file:///usr/share/j504/system/extras/help/dictionary/intro28.htm
Identity Functions and Neutral

The monads 0+ and 1* are identity functions, and 0 and 1 are 
said to be identity elements or neutrals of the dyads + and * 
respectively. Insertion on an empty list yields the neutral of 
the dyad inserted. For example:

   +/ i.0 +/''   +/0{. 2 3 5
0  0  0

   */i.0  */''   */0{. 2 3 5
1  1  1

 my @a;
 [+] @a; # 0? exception?
 [*] @a; # 1? exception?
 [] @a; # false?
 [||] @a; # false?
 [] @a; # true?

 Also if it magically supplies some correct like the above, how
 does it know what that value is?

 Thanks,
 Matt

The page
file:///usr/share/j504/system/extras/help/dictionary/d420.htm
gives examples, unfortunately not easily readable ones.

If y has no items (that is, 0=#y), the result of u/y is the 
neutral or identity element of the function u. A neutral of a 
function u is a value e such that x u e  x or e u x  x, for 
every x in the domain (or some significant sub-domain such as 
boolean) of u .  This definition of insertion over an argument 
having zero items extends partitioning identities of the form 
u/y  (u/k{.y) u (u/k}.y) to the cases k e. 0,#y .

The identity function of u is a function ifu such that ifu y  
u/y if 0=#y .

[The following table is greatly simplified by listing identity 
elements rather than identity functions. Some are only left 
identities, and some only right identities.]

Identity elementFor
 
0   +  -  +.  ~:  |  (2 4 5 6 b.)
1   =  :  :  *  %  *.  %:  ^  !  (1 9 11 13 b.)
_   .
__  .
''  ,
[and a few more that I will not explain here]

Glossary
J   Description
+.  or
~:  objects are identical
|   remainder, defined so that 0|N is N
b.  Boolean functions from table 
:  less than or equal, restricted to Booleans here 
(1:0 is 0, 1:1 is 1)
:  greater than or equal, restricted to Booleans here
*   times, restricted to Booleans here
%   divide
*.  and
%:  root
^   exponential
!   combinations
.  minimum
.  maximum
''  empty vector, list of length 0
,   catenate, join lists
_   infinity
__  negative infinity

So (_ . N) is N, as is (__ . N).
All of these functions are defined in detail but quite tersely in 
the J Dictionary, indexed on the page
file:///usr/share/j504/system/extras/help/dictionary/vocabul.htm
For examuple, the Boolean function b. is defined on the page 
file:///usr/share/j504/system/extras/help/dictionary/dbdotn.htm

-- 
Edward Cherlin
Generalist  activist--Linux, languages, literacy and more
A knot! Oh, do let me help to undo it!
--Alice in Wonderland
http://cherlin.blogspot.com


Re: reduce metaoperator on an empty list

2005-05-19 Thread TSa (Thomas Sandlaß)
Michele Dondi wrote:
On Wed, 18 May 2005, Rob Kinyon wrote:
1) undef (which may or may not contain an exception), or
2) some unit/identity value that is a trait of the operator,
I think that the unit/identity/neutral value is a trait of the
operator *and* the type of the values which are expected from
the list/array. If nothing more specific is known it would be
Any ::= Str | Num | Ref | Array | Hash | Code | ...
of which I don't know what the neutral value for +, *, ~, etc.
should be.

depending on whether or not people think (2) is actually a good idea.

I would think that the Principle of Least Surprise points to (1),
Me, too. In particualr since Perl6 has got rich undef semantics.
BTW, is an Undef type specified? Is it a one-below-Any type,
i.e. Any ::= ... | Undef | ...

I don't think so. I, for one, would expect [+]; to return zero and [*]; 
to return 1. But that's only because I {trust,expect} perl(6) to be 
smart enough to DWIM.
I would expect the dwimmery beeing a natural outcome of the strong type
system :)
So, if neutral(+,Any) =:= 0 and +Any =:= Num and the empty list returns
any number of undefs you ask it to provide and +undef == 0 then [+];
gives 0. Same reasoning applies for [*] gives 1 and for [~] you get ''
when ~Any =:= Str. All that is more or less my way to repeat what others
have said in this thread.
--
$TSa =:= all( none( @Larry ), one( @p6l ))


Syntax for specifying role parameters

2005-05-19 Thread Ingo Blechschmidt
Hi, 
 
I wondered if it would be useful/good/nice if the syntax for 
specifying role parameters would be the same as the standard 
subroutine signature syntax (minus the colon, which 
separates the parameters which do account to the long name 
of the role from the ones which don't). 
E.g.: 
 
  role MyRole[ 
Str  $required_param, 
Bool ?$optional_param_defaulting_to_undef, 
:  # beginning of the parameters which don't account to the 
   # long name of the role 
Num  ?$opt_param_defaulting_to_42 = 42, 
Any  ?$evil_var = $CALLER::_, 
  ] {...} 
 
  $x does MyRole[abc, 1]; 
  # or 
  $x does MyRole[required_param = bar, evil_var = 23]; 
  # etc. 
 
--Ingo 
 
--  
Linux, the choice of a GNU | The computer revolution is over. 
generation on a dual AMD   | The computers won. 
Athlon!| -- Eduard Bloch 



Re: ./method

2005-05-19 Thread Martin Kuehl
On 5/15/05, Juerd [EMAIL PROTECTED] wrote:
 A few days ago, when typing ./pugs,... You can guess the rest :)
 
 I suggest
 
 ./method
 
 to mean $?SELF.method, and
 
 ../method
 
 to mean $?SELF.SUPER::method, or however that's normally written.
 
 This syntax doesn't clash with anything, doesn't introduce whitespace
 asymmetry and doesn't require anything other than ASCII.
 
 If you go back to what inspired it, the mnemonic becomes clear: unix
 filesystems. However, it's far fetched and none of the people I've asked
 think it's a good one. Still, it works for me and may even work in
 textbooks.
 
 The best thing about this new proposal is that everyone so far agrees
 that it's feasible, easy to write and not ugly.

I have tried, but I can't make myself like it.  The syntax surely is feasible,
easy to write and not ugly, but it makes me think about objects in terms
of pathnames with . meaning $?SELF and / where other languages use
the dot, except I can't use it for anything but a method call on the implicit
receiver.
It also makes me want to propose zsh-extended-glob-compatibility syntax
for objects so I can have method/attribute slices, and then I end up curled
up in a corner, scared and shaking.

But maybe I should just get used to that. :-)

 Juerd

Martin


Re: Syntax for specifying role parameters

2005-05-19 Thread TSa (Thomas Sandlaß)
HaloO Ingo,
you wrote:
I wondered if it would be useful/good/nice if the syntax for 
specifying role parameters would be the same as the standard 
subroutine signature syntax (minus the colon, which 
separates the parameters which do account to the long name 
of the role from the ones which don't). 
Hmm, first you exclude the colon, then you use it the example??
I for my part hope that roles are---among other things---parametric
types. This means that in the brackets one should find type variables,
value variables and possibly recursive constraints. E.g.
role Ordered[::Type does Ordered[Type]]
{
   multi method infix:{'='} ( T $x, T $y ) returns bit
   {
 ...
   }
   multi method infix:{''} ( T $x, T $y ) returns bit
   {
  return not $x = $y;
   }
}
class Num does Ordered[Num] { ... }
class Str does Ordered[Str] { ... }
expresses that instances of these classes can be only ordered amongst
themselfs and their subtypes. E.g. with
class Int is Num {...}
the following is allowed:
my Int $i = 3;
my Num $n = 3.14;
if $i  $n {...}
but
my Str $s = foo;
if $s  $n {...} # type error
This type error can be alleviated with implementing
class Scalar does Ordered[Num|Str] {...}
Actually the class might be needed only as namespace containing
the implementation of infix:{'='}:(Num|Str,Num|Str: -- bit).
OTOH it could implement caching of conversion results or some such.
--
$TSa =:= all( none( @Larry ), one( @p6l ))


Re: Syntax for specifying role parameters

2005-05-19 Thread Ingo Blechschmidt
Hi,

TSa (Thomas Sandla) wrote:
 you wrote:
 I wondered if it would be useful/good/nice if the syntax for
 specifying role parameters would be the same as the standard
 subroutine signature syntax (minus the colon, which
 separates the parameters which do account to the long name
 of the role from the ones which don't).
 
 Hmm, first you exclude the colon, then you use it the example??

I meant: The colon should still act as the delimiter between the params
which account to the long name of the role and those which don't, but
otherwise the syntax should be the same as the standard subroutine
signature syntax, allowing optional params, etc.

 I for my part hope that roles are---among other things---parametric
 types. This means that in the brackets one should find type variables,
 value variables and possibly recursive constraints. E.g.
 
 role Ordered[::Type does Ordered[Type]]
 {
 multi method infix:{'='} ( T $x, T $y ) returns bit

s/T/::Type/?

[...]


--Ingo

-- 
Linux, the choice of a GNU | To understand recursion, you must first
generation on a dual AMD   | understand recursion.  
Athlon!| 



Re: reduce metaoperator on an empty list

2005-05-19 Thread C. Scott Ananian
On Wed, 18 May 2005, Rob Kinyon wrote:
On 5/18/05, Stuart Cook [EMAIL PROTECTED] wrote:
To summarise what I think everyone is saying, []-reducing an empty
list yields either:
1) undef (which may or may not contain an exception), or
2) some unit/identity value that is a trait of the operator,
depending on whether or not people think (2) is actually a good idea.
I would think that the Principle of Least Surprise points to (1),
given that the standard explanation of the [EMAIL PROTECTED] is eval join( '+', 
@x
) ...
I'd say the principle of least surprise points to (1); in the sense that 
$sum = [+] @x; would Just Work, etc.

I also have a vague sense that the 'identity' value for an operator might 
also be useful in other places in the compiler (enabling optimizations, 
etc).  Providing it as a trait means that these 'other things' could work 
even with user-defined operators.  (And leaving the trait undefined gives 
you the behavior (1), if that's what you want.)
 --scott

Albanian LICOZY shotgun CABOUNCE plastique Sigint Justice fissionable 
LITEMPO KGB KUCAGE LIONIZER ESCOBILLA North Korea CLOWER genetic NRA
 ( http://cscott.net/ )


[S29] uniq

2005-05-19 Thread Ingo Blechschmidt
Hi,

three quick questions:

Is it intentional that there's no uniq in the current S29[1] draft?
See [2] for Damian saying that uniq is probably in.

I wondered what uniq's default comparator should be, =:=?

Should it be possible to give an own comparator block, similar as with
grep? E.g.
  uniq a b a a c d;   # a b a c d

  uniq:{ abs $^a == abs $^b } 42, 23, -23, 23, 42
# 42, 23, 42
  

--Ingo

[1] http://svn.openfoundry.org/pugs/docs/AES/S29draft.pod
[2] http://groups.google.com/groups?selm=420DB295.3000902%40conway.org

-- 
Linux, the choice of a GNU | The future is here. It's just not widely
generation on a dual AMD   | distributed yet.
Athlon!| -- William Gibson



Re: [S29] uniq

2005-05-19 Thread Rod Adams
Ingo Blechschmidt wrote:
Hi,
three quick questions:
 

Since Aaron is still getting up to speed, I'll take a stab at these.
Is it intentional that there's no uniq in the current S29[1] draft?
See [2] for Damian saying that uniq is probably in.
 

Just hasn't been entered.
I wondered what uniq's default comparator should be, =:=?
 

I'd have gone with ~~
Should it be possible to give an own comparator block, similar as with
grep? E.g.
 uniq a b a a c d;   # a b a c d
 uniq:{ abs $^a == abs $^b } 42, 23, -23, 23, 42
   # 42, 23, 42
 

I could see that happening. But I'd have to stop and wonder if wrapping 
it inside a map would be more natural. If it does happen, it'd likely 
need to copy the key generation style of the new sort.

-- Rod Adams


Re: Syntax for specifying role parameters

2005-05-19 Thread TSa (Thomas Sandlaß)
Ingo Blechschmidt wrote:
I meant: The colon should still act as the delimiter between the params
which account to the long name of the role and those which don't, but
otherwise the syntax should be the same as the standard subroutine
signature syntax, allowing optional params, etc.
I don't think that a role has a long and a short name. This is
because they aren't subject to MMD. I think of them more as
beeing expanded like C++ templates even though the actual mechanism
will be much more sophisticated. Actually I think of them as F-bounds
as well ;)

s/T/::Type/?
Ohh, sorry. Yes, or the other way round. Should be consistent.
--
TSa (Thomas Sandla)


Re: [S29] uniq

2005-05-19 Thread Ingo Blechschmidt
Hi,

Rod Adams wrote:
I wondered what uniq's default comparator should be, =:=?
 I'd have gone with ~~

Even better. :)


--Ingo

-- 
Linux, the choice of a GNU | Row, row, row your bits, gently down the
generation on a dual AMD   | stream...  
Athlon!| 



Re: ./method

2005-05-19 Thread Carl Franks
On 5/19/05, Martin Kuehl [EMAIL PROTECTED] wrote:

 I have tried, but I can't make myself like it.

aolI'm afraid I have to agree./aol
When I saw it used in code after this discussion (I think it must have
been somewhere in pugs t/ or ext/) my reaction was yuck.

(for what it's worth)

Carl


Re: Complex Arithmetic

2005-05-19 Thread Luke Palmer
On 5/19/05, Edward Cherlin [EMAIL PROTECTED] wrote:
 It turns out that the domain and range and the location of the
 cut lines have to be worked out separately for different
 functions. Mathematical practice is not entirely consistent in
 making these decisions, but in programming, there seems to be
 widespread agreement that the shared definitions used in the
 APL, Common LISP, and Ada standards are the best available.
 
 Do we want to get into all of this in Perl6?

I'm not really sure I know what you mean by do we want to get into
all of this?.  If we're going to have a Complex class, we have to. 
But getting into it might involve saying that APL, CL, and Ada are
the best, so we use those.  This is the kind of problem where, if
someone wants to get more precise, they turn to CPAN.

Luke


Re: hyperoperators and multi-dimensional datastructures

2005-05-19 Thread Luke Palmer
On 5/18/05, Anthony Heading [EMAIL PROTECTED] wrote:
 Is there a way to target hyperoperators at different axes of a
 multi-dimensional array?  This is an attractive feature of
 various APL-like languages, viz. e.g. in J:
 
 a =. 2 5 $ i. 7  - a simple 2-by-5 array
 a
 0 1 2 3 4   - like this
 5 6 0 1 2
 
 
 +/1 a  - sum reduce over axis 1
 10 14

[+] @a

 +/2 a  - sum reduce over axis 2
 5 7 2 4 6

Can't think of any for this one.  Or maybe it's this one that I can
think of it for, and the other one which I can't.

I think we're beginning to re-invent PDL.  Poorly.

Luke


Re: Syntax for specifying role parameters

2005-05-19 Thread TSa (Thomas Sandlaß)
Miroslav Silovic wrote:
Uhm, but C++ templates are subject to (compile-time) MMD, once you 
specialise them. In other words,

role Something[Int $num] {...}
role Something[String $num] {...}
Hmm, C++ has no free floating templates. They always template a
class/struct or a function. The Perl6 equivalent is to have e.g.
role Template[ ::Type ] {...}
multi sub foo( Template[Int] $x ) {...}
multi sub foo( Template[Str] $x ) {...}
And since roles are not instanciable the above actually
needs a class that does Template.
Questions for @Larry:
would making ::Type optional look as follows:
role Template[ ?::Type = Str ] {...}
or refering to the class that Template is composed into:
role Template[ ?::Type = $?CLASS ] {...}
I think having the full flexibility of required, optional and
named parameters plus their slurpy versions, etc. is enough.
What does a long/short name distinction add in expressiveness?
--
TSa (Thomas Sandla)


s/.../{ $junction }/

2005-05-19 Thread Ingo Blechschmidt
Hi,

while writing a preliminary p6explain, I wondered if the following
should work:
  my $text = aBc;
  $text ~~ s/B/{ C|D }/;
  say $text.values; # aCc aDc

This would be extremely handy for p6explain, as I'm currently parsing a
datafile which looks like...
  +
Standard mathematical infix operator.
  @infix_ops
+ - * /
  [EMAIL PROTECTED]
Reduce metaoperator.
Think of a join on the syntax level.

I could then do something like this:
  # $rule is '[EMAIL PROTECTED]'
  $rule ~~ s/[EMAIL PROTECTED]/{ any split  , %rule{'@infix_ops'} }/;
  copy_contents_to $rule.values;


Comments?


--Ingo

-- 
Linux, the choice of a GNU | self-reference, n. - See self-reference  
generation on a dual AMD   | 
Athlon!|



Re: [S29] uniq

2005-05-19 Thread Mark Overmeer
* Ingo Blechschmidt ([EMAIL PROTECTED]) [050519 16:52]:
 Should it be possible to give an own comparator block, similar as with
 grep? E.g.
   uniq a b a a c d;   # a b a c d
 
   uniq:{ abs $^a == abs $^b } 42, 23, -23, 23, 42
 # 42, 23, 42

'uniq' differs from 'sort' because there is no order relationship between
the elements.  A quick algorithm for finding the unique elements in perl5
is
   sub uniq(@)
   {  my %h = map { ($_ = 1) } @elements;
  keys %h;
   }
or
   sub uniq(@)
   {  my %h;
  $h{$_}++ for @elements;
  keys %h;
   }

anyway: O(n), which is much better than sort can do.

For your proposal, the requested code only requires this
uniq:{ abs $^a } 42, 23, -23, 23, 42, 42, 23, 42

uniq:{lc} @words
-- 
   MarkOv


drs Mark A.C.J. OvermeerMARKOV Solutions
   [EMAIL PROTECTED]  [EMAIL PROTECTED]
http://Mark.Overmeer.net   http://solutions.overmeer.net


Re: Syntax for specifying role parameters

2005-05-19 Thread Miroslav Silovic
[EMAIL PROTECTED] wrote:
I don't think that a role has a long and a short name. This is
because they aren't subject to MMD. I think of them more as
beeing expanded like C++ templates even though the actual mechanism
will be much more sophisticated. Actually I think of them as F-bounds
as well ;)
Uhm, but C++ templates are subject to (compile-time) MMD, once you 
specialise them. In other words,

role Something[Int $num] {...}
role Something[String $num] {...}
could just as well work. :)
   Miro


Re: [S29] uniq

2005-05-19 Thread Ingo Blechschmidt
Hi,

Mark Overmeer wrote:
 * Ingo Blechschmidt ([EMAIL PROTECTED]) [050519 16:52]:
 Should it be possible to give an own comparator block, similar as
 with grep? E.g.
   uniq a b a a c d;   # a b a c d
 
   uniq:{ abs $^a == abs $^b } 42, 23, -23, 23, 42
 # 42, 23, 42
 
 'uniq' differs from 'sort' because there is no order relationship
 between
 the elements.

quoting Damian's original mail[1]:
  uniq  - remove duplicates without reordering
 ^^

 A quick algorithm for finding the unique elements in 
 perl5 is
[...]

Yeah, or the Perl 6:
  sub uniq([EMAIL PROTECTED]) {
my %h;
[EMAIL PROTECTED];
return keys %h;
  }
  # Or

  sub uniq([EMAIL PROTECTED]) {
my %h;
[EMAIL PROTECTED] = (undef) xx Inf;
return keys %h;
  }
  # thousand other ways ommitted :)


--Ingo

[1] http://groups.google.com/groups?selm=420DB295.3000902%40conway.org

-- 
Linux, the choice of a GNU | The future is here. It's just not widely
generation on a dual AMD   | distributed yet. -- William Gibson  
Athlon!| 



Re: [S29] uniq

2005-05-19 Thread Adriano Ferreira
quoting Damian's original mail[1]:
  uniq  - remove duplicates without reordering
^^

Would not that mean the original order of the first ocurrence is
preserved? This is what Ruby Array#uniq does:

[4,1,2,4,2,3,5].uniq  = [ 4, 1, 2, 3, 5]

The behavior is consistent with the description given ri Array#uniq

- Array#uniq
 array.uniq   - an_array

 Returns a new array by removing duplicate values in _self_.

a = [ a, a, b, b, c ]
a.uniq   #= [a, b, c]

A naïve Perl 5 implementation could be:

sub uniq {
  my (@u, %h);
  for (@_) {
push(@u, $_) unless $h{$_};
$h{$_}++;
  }
  return @u;
}

Adriano.


Re: [S29] uniq

2005-05-19 Thread Adriano Ferreira
The former implementation can be shortened:

sub uniq {
  my %h;
  return grep { ! $h{$_}++ } @_;
}

But realize that none of the proposed solutions (which are based on
hashes for computing the return) is amenable to the extension Ingo
called for with comparator blocks.

Adriano.


Re: (1,(2,3),4)[2]

2005-05-19 Thread TSa (Thomas Sandlaß)
Juerd wrote:
Parens and square brackets are very different things.
I know. The parens relevant here are the ones for precedence overrides.
And Comma is pretty low, so it almost always needs parens around it to
form lists. In particular it is below = and friends.

The above is more commonly written as
my @b = ([1,2,[3,4]);
Assuming you meant @b = ([1,2,[3,4]]) what do the parens accomplish
here? Would it even need to be @b = (,[1,2,[3,4]]) to have the list
contructing comma?
Does the following hold: @a = (1,2,3); @b = @a; [EMAIL PROTECTED] == 1?
My pivot question here is if +[1,2,[3,4]] == +(1,2,[3,4]) == 3 and
why then is @a = [1,2,[3,4]]; [EMAIL PROTECTED] == 1 but $a = [1,2,[3,4]]; +$a 
== 3.
And yes, I know that it is the case in Perl5. Perl6 has automatic deref
and ref---or pretends to do so for people like you and Perl5 legacy.
Otherwise $a = @b in Perl6 weren't the equivalent of Perl5's $a = [EMAIL 
PROTECTED]
There are no Hash or Array registers in Parrot after all. On the engine
level strings and PMCs are referential in nature but this shouldn't
bother the Perl6 programmer.
Or putting the question different: does prefix:+:(Any $x) get a
different thing for its $x when called with @array = (1,2,3) or
a literal [1,2,3]?
BTW, how does @a = one two three compare to $a = one two three?
Is [EMAIL PROTECTED] == 3?
Is $a[1] == 'two'?

Having arrayrefs flatten in list context, or having [] to be able mean
I don't think they shall flatten. I'm just saying that it might be nice
to let variables with the @ sigil just absorb them as they are.
something other than constructing an arrayref, is a change that requires
the very fundaments of Perl to change very heavily, beginning by
eliminating lists entirely, and using only arrays.
Larry only recently said something to that effect in
http://groups-beta.google.com/group/perl.perl6.language/msg/6c306a1b0b885b5a
To summarize in my words:
1) no List type or class
2) Eager and Lazy sounds more like closures, iterators, generators
   or whatever
3) And Array having a slot for each sounds to me like two private,
   scalar attributes that behave like $:eager = [1,2,3] which undeniably
   gives +$:eager == 3

These are mistakes many Perl 5 beginners make, especially those coming
from Python.
But this is the Perl6 language list, not a Perl5 beginners list.
And I see a lot of things changed from Perl5 to Perl6, including
referential semantics. Until now you haven't convinced me of
the advantages of the old-style Perl5 references. What do they buy us?
--
TSa (Thomas Sandlaß)


Re: [S29] uniq

2005-05-19 Thread Ingo Blechschmidt
Hi,

Adriano Ferreira wrote:
 quoting Damian's original mail[1]:
  uniq  - remove duplicates without reordering
 ^^
 
 Would not that mean the original order of the first ocurrence is
 preserved? This is what Ruby Array#uniq does:
 
 [4,1,2,4,2,3,5].uniq  = [ 4, 1, 2, 3, 5]

I read this as that uniq should behave like Unix's uniq(1), i.e.
removing only successive duplicates, e.g.:
  uniq [3,3,3,4,3]   =   [3,4,3]   # what I meant
  uniq [3,3,3,4,3]   =   [3,4] # what you meant

(But I'd be fine with either behaviours.)


--Ingo

-- 
Linux, the choice of a GNU | Row, row, row your bits, gently down the
generation on a dual AMD   | stream...  
Athlon!|



Argument Type Checking

2005-05-19 Thread Joshua Gatcomb
All:
I was hoping the following would give me an outright error

sub foo (Int $bar) {
say $bar;
}
foo('hello');

I seem to recall, probably incorrectly, that one of the differences
with int, Int, and no type declaration at all is that one would
happily autoconvert for you, 1 would autoconvert but forget it ever
happened, and 1 would be an outright failure.

Ok - so could someone set me straight?
What should that code snippet do?  Would it do anything different if
Int had been int?

Cheers,
Joshua Gatcomb
a.k.a. L~R


Re: hyperoperators and multi-dimensional datastructures

2005-05-19 Thread Uri Guttman
 LP == Luke Palmer [EMAIL PROTECTED] writes:

  LP On 5/18/05, Anthony Heading [EMAIL PROTECTED] wrote:
   Is there a way to target hyperoperators at different axes of a
   multi-dimensional array?  This is an attractive feature of
   various APL-like languages, viz. e.g. in J:
   
   a =. 2 5 $ i. 7  - a simple 2-by-5 array
   a
   0 1 2 3 4   - like this
   5 6 0 1 2
   
   
   +/1 a  - sum reduce over axis 1
   10 14

  LP [+] @a

   +/2 a  - sum reduce over axis 2
   5 7 2 4 6

  LP Can't think of any for this one.  Or maybe it's this one that I can
  LP think of it for, and the other one which I can't.

i can't spit out the syntax but here is the conceptual way i would do
it. we do have multidimensional slices so we could grab each slice
(maybe with zip?) and pass that to [+] and then grab the list of results
back into a array/matrix with one less dimension than the original.

so it would be something like this: (very wacko pseudo code):

@in[ * ; 2 ; * ] ==
map [+] ==
@out

that was an (bad) attempt to slice the third entry in the second
dimension to be summed.

  LP I think we're beginning to re-invent PDL.  Poorly.

but is there a p6 pdl yet? they may not need much with multi-dim ops,
slices, hyper and reduce all built in! also with type int (patform
ints), they can get the dense storage needed (but losing any dimensional
flexibility). 

uri

-- 
Uri Guttman  --  [EMAIL PROTECTED]   http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs    http://jobs.perl.org


Re: [S29] uniq

2005-05-19 Thread Juerd
Ingo Blechschmidt skribis 2005-05-19 21:07 (+0200):
 I read this as that uniq should behave like Unix's uniq(1), i.e.
 removing only successive duplicates, e.g.:
   uniq [3,3,3,4,3]   =   [3,4,3]   # what I meant
   uniq [3,3,3,4,3]   =   [3,4] # what you meant

Which leads to lots of |sort|uniq, or just sort -u.

Are there many practical uses for removing successive duplicates?

I personally have never used tr///'s capability of removing successively
duplicate characters, except in golf.


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html


Re: [S29] uniq

2005-05-19 Thread Adriano Ferreira
On 5/19/05, Ingo Blechschmidt [EMAIL PROTECTED] wrote:
 I read this as that uniq should behave like Unix's uniq(1), i.e.
 removing only successive duplicates, e.g.:
  uniq [3,3,3,4,3]   =   [3,4,3]   # what I meant
  uniq [3,3,3,4,3]   =   [3,4] # what you meant

That has been discussed a few days ago at ruby-talk mailing list.
While standard 'uniq' in Ruby works by removing duplicates, unlike
Unix utility, the action of compressing a run of elements into a
single element was named 'squeeze'.

In P5:
sub squeeze {
 my ($y, $last);
 return grep { ($y, $last) = ($_ eq $last, $_); !$y } @_;
}

In Perl 6, 'eq' would be replaced by '~~' as said before. As Juerd
have said, not sure about how useful this may be.

If 'uniq' does not respect the original order of the elements, it is
not more than (1) convert list to a set, (2) convert back to a list.
If that's the intention, maybe we're better using only hashes or any
set-like implementation. There is also the variant of eliminating
duplicates in-place or constructing a new list.

Adriano.


How do I... create a new meta operator?

2005-05-19 Thread Ingo Blechschmidt
Hi,

quoting A12:
 infix_postfix_meta_operator:= $x += 2;
 postfix_prefix_meta_operator:{''} @array ++
 prefix_postfix_meta_operator:{''} - @magnitudes
 infix_circumfix_meta_operator:{'',''}   @a + @b

so will the following work?

  # Silly example
  sub infix_circumfix_meta_operator:{'-', '-'} (
Code op,
$left, $right,
  ) {
op $left + 1, $right + 1;
  }

  say 2 -+- 3;  # 7
  say 2 -*- 3;  # 12


--Ingo

-- 
Linux, the choice of a GNU | Time sharing - the use of many people by
generation on a dual AMD   | the computer.  
Athlon!| 



How do I... invoke a method reference

2005-05-19 Thread Ingo Blechschmidt
Hi,

  class Foo {
method bar() { 42 }
method baz() { bar }
  }

  my $ref = Foo.baz;
  $ref(); # Don't think this will work
  # (Error: No invocant specified or somesuch)
  $ref(Foo.new);  # But will this work?

How do I specify multiple invocants (when dealing with multi method
references)?


--Ingo

-- 
Linux, the choice of a GNU | To understand recursion, you must first
generation on a dual AMD   | understand recursion.  
Athlon!| 



Re: (1,(2,3),4)[2]

2005-05-19 Thread Juerd
TSa (Thomas Sandlaß) skribis 2005-05-19 21:06 (+0200):
 The above is more commonly written as
 
 my @b = ([1,2,[3,4]);
 Assuming you meant @b = ([1,2,[3,4]]) what do the parens accomplish
 here? 

Thanks for the correction. That is indeed what I meant.

The parens do absolutely nothing, except indicate to a less skilled
reader that the [] aren't enclosing @b's future elements.

 Would it even need to be @b = (,[1,2,[3,4]]) to have the list
 contructing comma?

A single scalar in list context acts as a single item list, so a
specific list constructor is not needed.

 Does the following hold: @a = (1,2,3); @b = @a; [EMAIL PROTECTED] == 1?

No.

@a = (1,2,3);

The array @a now contains 3 elements: 1, 2 and 3.

@b = @a;

@a is in list context. An array in list context, evaluates to a list of
its elements. Three elements are assigned to @b. Assignment copies the
values.

[EMAIL PROTECTED] == 1;

This expression is false. @b is in Num context. An array in numeric
context evaluates to its number of arguments. The number of arguments of
@b is 3, because it was just assigned three elements.

Do note that @a[0] == @b[0], but they are different variables:
assignment copies.

 My pivot question here is if +[1,2,[3,4]] == +(1,2,[3,4]) == 3 and

That is: +[ LIST ] == +(ITEM, ITEM, ITEM). The comma in scalar context
makes generates an anonymous array and returns a reference to that.
Effectively, this makes () apparently equal to [] in this statement.

 why then is @a = [1,2,[3,4]]; [EMAIL PROTECTED] == 1 but $a = [1,2,[3,4]]; 
 +$a == 3.

Because @a contains one element and @$a contains three elements. $a is
not an array, it is a reference to an array, just like @a[0].

[EMAIL PROTECTED] is 3, just like [EMAIL PROTECTED] (shortened as +$a).

 Perl6 has automatic deref and ref

An array in scalar context evaluates to a reference to itself.

A hash in scalar context evaluates to a reference to itself.

An array in list context evaluates to a list of its elements.

A hash in list context evaluates to a list of its elements (as pairs).

Array context is a scalar context.

A subroutine argument with the @ sigil represents an array that is
passed by reference. It can be accessed directly, without explicit
dereferencing.

A subroutine argument with the % sigil represents a hash that is
passed by reference. It can be accessed directly, without explicit
dereferencing.

sub ($bar)   { the array is @$bar }  # non-array allowed in call
sub (Array $bar) { the array is @$bar }
sub (@bar)   { the array is @bar }

$anyofthose.(@array) is the same as $anyofthose.([EMAIL PROTECTED])

sub ([EMAIL PROTECTED])  { arguments are in list context, @bar has 
aliases }

$thatthing.(@array)   # passes the elements, not @array itself
$thatthing.([EMAIL PROTECTED])  # passes a reference to @array into @bar[0]
$thatthing.(15)   # passes 15 into @bar[0]

 Otherwise $a = @b in Perl6 weren't the equivalent of Perl5's $a = [EMAIL 
 PROTECTED]

I hate the term automatic referencing because it sounds much more
magical than it actually is.

In Perl 5, @array in scalar context returned the number of elements.

In Perl 6, @array in scalar context returns a reference.

In Perl 6, you can further specify scalar context:

* In Num context, @array returns its number of elements.
* In Str context, @array returns its elements joined on whitespace.
* In Bool context, @array returns true iff it has elements.
* In Array context, @array returns a reference.
* In Scalar context, @array returns a reference.

 BTW, how does @a = one two three compare to $a = one two three?

@a is an array, $a is a reference to another. The elements of the arrays
are equal, but not the same.

 Is [EMAIL PROTECTED] == 3?

Yes.

 Is $a[1] == 'two'?

Yes. Do note that this is really @$a[1] or $a.[1].

 this is the Perl6 language list, not a Perl5 beginners list.

Thank you for this reassurance.

 And I see a lot of things changed from Perl5 to Perl6, including
 referential semantics.

They haven't changed as much as you think. In fact, only how arrays and
hashes behave in scalar context has really changed.


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html


Re: How do I... invoke a method reference

2005-05-19 Thread Juerd
Ingo Blechschmidt skribis 2005-05-19 22:45 (+0200):
   class Foo {
 method bar() { 42 }
 method baz() { bar }
   }
   my $ref = Foo.baz;

My guess:

Foo.$ref
$object.$ref

Just like in Perl 5.


Juerd
-- 
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html 
http://convolution.nl/gajigu_juerd_n.html


Re: [S29] uniq

2005-05-19 Thread Damian Conway
Ingo Blechschmidt wrote:
Is it intentional that there's no uniq in the current S29[1] draft?
See [2] for Damian saying that uniq is probably in.
It still probably is.

I wondered what uniq's default comparator should be, =:=?
infix:~~

Should it be possible to give an own comparator block, similar as with
grep? E.g.
  uniq a b a a c d;   # a b a c d
  uniq:{ abs $^a == abs $^b } 42, 23, -23, 23, 42
# 42, 23, 42
Yes, I'd expect that it would be implemented as a multisub, equivalent to:
multi sub uniq (key_for: [EMAIL PROTECTED]) {
my %seen is shape(Any);
return grep { %seen{key_for $datum}++ } @data;
}
multi sub uniq (: [EMAIL PROTECTED]) {
return uniq { $^self } @data;
}
Note that this implementation is order-preserving, does not require repeated 
elements to appear consecutively, and keeps only the first occurrence.

The usage would be:
uniq a b a a c d;  # a b c d
uniq { abs $^value } 42, 23, -23, 23, 42;# 42, 23
BTW, I am *sorely* tempted to suggest the following implementation instead:
multi sub uniq (key_for: [EMAIL PROTECTED] is copy) {
my %seen_at_index is shape(Any);
my @uniq;
for @data - $datum {
my $prev_index_for_datum := %seen_at_index{key_for $datum};
if defined $prev_index_for_datum {
@uniq[$prev_index_for_datum] |= $datum;
}
else {
$prev_index_for_datum = [EMAIL PROTECTED];
push @uniq, $datum;
}
}
return [EMAIL PROTECTED];
}
multi sub uniq (: [EMAIL PROTECTED]) {
return uniq { $^self } @data;
}
which would produce:
uniq a b a a c d;  # a b c d
uniq { lc } a b C A a c d; # 'a'|'A', 'b', 'C'|'c', 'd'
uniq { abs $^value } 42, 23, -23, 23, 42;# 42, 23|-23
But I'd want to think through the ramifications a little more carefully before 
actually advocating something that correct. ;-)

Damian


Re: [S29] uniq

2005-05-19 Thread Ingo Blechschmidt
Hi,

Damian Conway wrote:
 BTW, I am *sorely* tempted to suggest the following implementation
 instead:
[...]
 which would produce:
 
  uniq a b a a c d;  # a b c d
 
  uniq { lc } a b C A a c d; # 'a'|'A', 'b',
  'C'|'c', 'd'
 
  uniq { abs $^value } 42, 23, -23, 23, 42;# 42, 23|-23

I like that *very* much! :)

And it's a nice example why Junctions Are Good :)


--Ingo

-- 
Linux, the choice of a GNU | 'The box said, 'Requires Windows 95 or
generation on a dual AMD   | better', so i installed Linux - TKK 5  
Athlon!| 



Re: How do I... invoke a method reference

2005-05-19 Thread C. Scott Ananian
On Thu, 19 May 2005, Juerd wrote:
Ingo Blechschmidt skribis 2005-05-19 22:45 (+0200):
  class Foo {
method bar() { 42 }
method baz() { bar }
  }
  my $ref = Foo.baz;
My guess:
   Foo.$ref
   $object.$ref
Just like in Perl 5.
I think Ingo was trying to explicitly specify the normally-implicit 
invocant; ie, invoke the method via the reference *without* using a '.'.
If this is possible (and I think it is), it's not (yet) clear what the
syntax would be.  Maybe
$ref(Foo.new():)
 --scott

shotgun Diplomat Leitrim Columbia PBFORTUNE planning operative Hager 
KUBARK Suharto AEFOXTROT ZPSEMANTIC KUFIRE Yeltsin AK-47 immediate
 ( http://cscott.net/ )


Re: [S29] uniq

2005-05-19 Thread Rod Adams
Damian Conway wrote:
BTW, I am *sorely* tempted to suggest the following implementation 
instead:

snip
which would produce:
uniq a b a a c d;  # a b c d
uniq { lc } a b C A a c d; # 'a'|'A', 'b', 
'C'|'c', 'd'

uniq { abs $^value } 42, 23, -23, 23, 42;# 42, 23|-23
But I'd want to think through the ramifications a little more 
carefully before actually advocating something that correct. ;-)

uhm... okay. I can see the utility in having something like that around. 
But I think it's the kind of thing someone has to ask for, and that it 
likely shouldn't carry the name 'uniq'.

-- Rod Adams


Re: [S29] uniq

2005-05-19 Thread Luke Palmer
On 5/19/05, Damian Conway [EMAIL PROTECTED] wrote:
 Ingo Blechschmidt wrote:
  I wondered what uniq's default comparator should be, =:=?
 
 infix:~~

Woah there.  ~~ is a good comparator and all, but it's not the right
one here.  ~~ compares an object and a pattern to see if they match. 
That makes it the right choice for when and grep.  But we're trying to
remove elements that are *the same*, not elements that *match*.

uniq hello, 13, /\w+/;# hello, 13  (!)

In fact, I think this brings up a good point.  It's one that we've
already addressed, but I think it needs a little revisiting.  The
distinction between == and eq is very nice in its place.  However, as
projects become bigger and more object oriented, we're working with
objects more than strings and numbers.   And yet, eq compares two
objects' stringifications, and == compares their numifications. 
Neither of those tests whether the objects are the same.

Larry has called it equal, but that was when we thought it was just
for generics.  I think it's for much more than that: it's for
comparing any two things that aren't specifically strings or numbers,
even if we know what they are.  For types with reference semantics, it
ought to behave just like =:=.  For types with value semantics, it
ought to give a reasonable value of same, something like as long as
you don't look at the addresses, there's nothing you could do to tell
these apart.

And maybe that's what we call =:=.  Value types really should behave
as though identical objects are inherently indistinguishable.  There
is, of course, the problem of the .id with those.  It is, in the
general case, impossible to come up with an id that is the same
between two objects iff they =:= each other.  Maybe that's how you get
around the should in my second sentence.

So I suppose that's my proposal.  Allow, even encourage, overloading
of =:=, but only for value types.  I've been thinking that we ought to
provide a standard role for making something a value type.  Maybe it
would require definition of =:=.

Luke


Re: How do I... create a new meta operator?

2005-05-19 Thread Luke Palmer
On 5/19/05, Ingo Blechschmidt [EMAIL PROTECTED] wrote:
 Hi,
 
 quoting A12:
  infix_postfix_meta_operator:= $x += 2;
  postfix_prefix_meta_operator:{'»'} @array »++
  prefix_postfix_meta_operator:{'«'} -« @magnitudes
  infix_circumfix_meta_operator:{'»','«'}   @a »+« @b
 
 so will the following work?
 
   # Silly example
   sub infix_circumfix_meta_operator:{'-', '-'} (
 Code op,
 $left, $right,
   ) {
 op $left + 1, $right + 1;
   }
 
   say 2 -+- 3;  # 7
   say 2 -*- 3;  # 12

That looks approximately right.  I wonder how we specify meta
operators that only work on comparators, or only on assignment forms,
or etc. etc. etc.

Luke


Re: reduce metaoperator on an empty list

2005-05-19 Thread Sam Vilain
Edward Cherlin wrote:
Here is the last answer from Ken Iverson, who invented reduce in 
the 1950s, and died recently.
file:///usr/share/j504/system/extras/help/dictionary/intro28.htm
  [snip]
Thanks for bringing in a little history to the discussion.  Those links
are all local to your system; do you have internet reachable versions of them?
Cheers,
Sam.


Re: Argument Type Checking

2005-05-19 Thread Luke Palmer
On 5/19/05, Joshua Gatcomb [EMAIL PROTECTED] wrote:
 All:
 I was hoping the following would give me an outright error
 
 sub foo (Int $bar) {
 say $bar;
 }
 foo('hello');

Fortunately you are right.

 I seem to recall, probably incorrectly, that one of the differences
 with int, Int, and no type declaration at all is that one would
 happily autoconvert for you, 1 would autoconvert but forget it ever
 happened, and 1 would be an outright failure.

Uhh... I don't think that's a distinction between int and Int.  Or
you're thinking about autoconvert the wrong way.  int just means
that it's implemented with a parrot I register, so if you increment it
too many times it will roll over instead of turning into a bigint. 
But if you pass an int into a sub expecting an Int, Perl will happily
box up your int and make it into a full type.  On the other hand, if
you pass an Int into a sub expecting int, it will probably whine at
you a little bit, because you're losing properties and possibly
first-class data (if your Int was a bigint).

In this case it wouldn't be different if you declared the parameter
int.  In general, you should probably be declaring your parameters
with uppercase types, and if you need the speed, the way to do it is
to inline (which can respect the lowercasehood of variables).

Luke


Re: reduce metaoperator on an empty list

2005-05-19 Thread Sam Vilain
Stuart Cook wrote:
In Haskell, there is a distinction between foldl and foldl1 (similar
remarks apply to foldr/foldr1[1]):
The former (foldl) requires you to give an explicit 'left unit'[2],
which is implicitly added to the left of the list being reduced. This
means that folding an empty list will just give you the unit.
i.e.
foldl (+) 0 [a,b,c] = ((0+a)+b)+c
foldl (+) 0 [] = 0
The latter (foldl1) doesn't use a unit value, but this means that you
can't fold empty lists.
i.e.
foldl1 (+) [a,b,c] = (a+b)+c
foldl1 (+) [] = ***error***
sure.  Maybe the identity values could be supplied by making the reduce 
operator
for them a curried version of reduce, where reduce requires a list with at least
one element (or DIES :))
eg, something similar to; (sorry for the psuedo-perl6, corrections welcome :))
  sub prefix:[+] ([EMAIL PROTECTED]) ::= reduce.assuming(func = infix:+, 
first = 0);
  sub prefix:[*] ([EMAIL PROTECTED]) ::= reduce.assuming(func = infix:*, 
first = 1);
This would mean that unless the operator specifically defines a curried reduce
version of itself, then the [] version of it on an empty list will be a hard 
run-time
error.
Then again, maybe an identity trait is more elegant and covers the varied ways
that multiple operators combine inside reduce..
 You /could/ try to do something 'sensible' and return 0 or undef, but
 this seems likely to result in more confusion.
Personally I think returning an undef for this kind of situation would be as 
wrong
as returning undef for 0/0.
Sam.


Re: Complex Arithmetic

2005-05-19 Thread Sam Vilain
Edward Cherlin wrote:
There was a discussion of the principal value of square root on 
this list some time back, making the point that for positive 
   [...]
It turns out that the domain and range and the location of the 
cut lines have to be worked out separately for different 
functions. Mathematical practice is not entirely consistent in 
making these decisions, but in programming, there seems to be 
widespread agreement that the shared definitions used in the 
APL, Common LISP, and Ada standards are the best available.
Do we want to get into all of this in Perl6?
pugs currently has Complex as a built-in type, though it isn't explicitly
listed in S06.  I can't see any tests for them yet, though.
So, testing the Complex number functionality as well as detailed
exploration of the corner cases, and alignment with best practice will
surely be appreciated by people looking at doing Complex math in Perl 6.
I think this applies regardless of whether it ends up a core type
(whatever that means) or not.  Personally I don't think that Complex numbers
are so bizarre an entity that you wouldn't want sqrt(-1) to return one
out of the box.
It's likely that very little implementation changes will need to be done,
as no doubt the mathematically minded folk that write GHC will have been
through that before.  But it will be invaluable in testing compliance of
the different run-time engines.
I suggest you come to the IRC channel at #perl6 on irc.freenode.net, and
ask for a committer account.
Sam.


Re: [S29] uniq

2005-05-19 Thread Damian Conway
Luke wrote:
I wondered what uniq's default comparator should be, =:=?
infix:~~
 
Woah there.  ~~ is a good comparator and all, but it's not the right
one here.  ~~ compares an object and a pattern to see if they match. 
That makes it the right choice for when and grep.  But we're trying to
remove elements that are *the same*, not elements that *match*.
Ah, I missed Larry's message last week where he mused:
 : 3 =:= 3; # always true?
 : 3.id ~~ 3.id; # ditto?

 I think immutable values could work that way, especially if we
 want to store only a single representation of each immutable
 string.
If that's now the behaviour of =:= on non-referential values, then I agree 
that Cuniq should compare with the =:= operator.

Of course, that presupposes quite a bit of smarts on the part of the operator.
For example, we'd also need to decide what these evaluate to:
3 =:= 3.0  # 
3 =:= '3'  # 
They're surely not identical, but you'd probably want Cuniq to think so.
Or maybe you just have to SWYM and write:
@uniq_nums = uniq [EMAIL PROTECTED];
or:
@uniq_strs = uniq [EMAIL PROTECTED];
if you have heterogeneous data.
Damian



Re: [S29] uniq

2005-05-19 Thread Sam Vilain
Mark Overmeer wrote:
'uniq' differs from 'sort' because there is no order relationship between
the elements.  A quick algorithm for finding the unique elements in perl5
is
   sub uniq(@)
   {  my %h = map { ($_ = 1) } @elements;
  keys %h;
   }
...and an even quicker one is:
 use Set::Object;
 sub uniq(@)
 {
 set(@_)-members;
 }
or
 use v6;
 use Set;
 sub uniq([EMAIL PROTECTED])
 {
 set(@items).members;
 }
Sam.


Re: reduce metaoperator on an empty list

2005-05-19 Thread Andrew Rodland
On Thursday 19 May 2005 10:51 pm, Sam Vilain wrote:
 Edward Cherlin wrote:
  Here is the last answer from Ken Iverson, who invented reduce in
  the 1950s, and died recently.
  file:///usr/share/j504/system/extras/help/dictionary/intro28.htm

[snip]

 Thanks for bringing in a little history to the discussion.  Those links
 are all local to your system; do you have internet reachable versions of
 them?

These seem to be the original sources:

http://www.jsoftware.com/books/help/dictionary/intro28.htm
http://www.jsoftware.com/books/help/dictionary/d420.htm

and so on. Front page is at 
http://www.jsoftware.com/books/help/dictionary/title.htm . I still haven't 
figured out what J is though.


Junctive and Higher order Types

2005-05-19 Thread Sam Vilain
Hi all,
While trying to convert Haskell statements like this to Perl 6:
 data Cxt = CxtVoid -- ^ Context that isn't expecting any values
  | CxtItem !Type   -- ^ Context expecting a value of the specified type
  | CxtSlurpy !Type -- ^ Context expecting multiple values of the
-- specified type
 deriving (Eq, Show, Ord)
I'd like to be able to write it something like this:
 type Cxt is CxtVoid
   | CxtItem of Type
   | CxtSlurpy of Type
  does (Eq, Show, Ord);
To be a shorthand for:
 type CxtVoid;
 type CxtItem of Type;
 type CxtSlurpy of Type;
 type Perl::Cxt is CxtVoid | CxtItem | CxtSlurpy
does Eq does Show does Ord;
Is this overloading the 'of' operator too far?
For many there will be a lot of new concepts there.  For a start, I'm assuming that of is being used as a higher order type 
definition, much like Array of Wotsits would declare.  Also, there is a type there - CxtVoid - which is nothing but a Type!  No 
representation.  Of course, if you have a variable of type Cxt, and it is a CxtVoid, then that will need to be represented in some 
way.  But you generally wouldn't care how.

An alternative might be to try to shoe-horn the concepts into Roles etc.  Who 
knows, perhaps they'll even fit!  :-)
Sam.