Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
reduce metaoperator on an empty list
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 71 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Matt Fowles  
View profile  
 More options May 18 2005, 8:57 pm
Newsgroups: perl.perl6.language
From: uberm...@gmail.com (Matt Fowles)
Date: Wed, 18 May 2005 20:57:42 -0400
Local: Wed, May 18 2005 8:57 pm
Subject: reduce metaoperator on an empty list
All~

What does the reduce metaoperator do with an empty list?

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
--
"Computer Science is merely the post-Turing Decline of Formal Systems Theory."
-???


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rod Adams  
View profile  
 More options May 18 2005, 9:12 pm
Newsgroups: perl.perl6.language
From: r...@rodadams.net (Rod Adams)
Date: Wed, 18 May 2005 20:12:53 -0500
Local: Wed, May 18 2005 9:12 pm
Subject: Re: reduce metaoperator on an empty list

My general thoughts has been that:

    [op] @list

behaves something like:

    eval join(op, @list)

so feeding it an empty list would return undef, regardless of op.
Similarly, if @list is just one element, it returns that element.

-- Rod Adams


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark A. Biggar  
View profile  
 More options May 18 2005, 9:26 pm
Newsgroups: perl.perl6.language
From: m...@biggar.org (Mark A. Biggar)
Date: Wed, 18 May 2005 18:26:19 -0700
Local: Wed, May 18 2005 9:26 pm
Subject: Re: reduce metaoperator on an empty list

Matt Fowles wrote:
> All~

> What does the reduce metaoperator do with an empty list?

> 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?

The usual definition of reduce in most languages that support it, is
that reduce over the empty list produces the Identity value for the
operation.  So for the above ops the answers are: 0, 1, depends, false,
true.  For chained ops like '<' it depends on whether x<y<z return x or
z on being true. if x then -inf else if z then +inf (don't ask if it
returns y).  Note that some ops (like '%') don't have an identity value
and therefore [%] over the empty list is the equivalent to a divide by 0
and probably throws an exception or at least returns undef.  Now this is
  a problem for user defined operations, so reduce of a user defined op
over the empty list is either an exception, return undef or we need a
trait that can be specified for an infix op that specifies what to
return for reduce over the empty list.  The compiler shouldn't bother to
check if what you specified is really the Identity value for op, but I'd
consider it a bug if it isn't.

--
m...@biggar.org
mark.a.big...@comcast.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stuart Cook  
View profile  
 More options May 18 2005, 9:27 pm
Newsgroups: perl.perl6.language
From: sco...@gmail.com (Stuart Cook)
Date: Thu, 19 May 2005 11:27:48 +1000
Local: Wed, May 18 2005 9:27 pm
Subject: Re: reduce metaoperator on an empty list
On 5/19/05, Matt Fowles <uberm...@gmail.com> wrote:

> All~

> What does the reduce metaoperator do with an empty list?

/me puts on his lambda hat

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***

/me puts camel hat back on

This suggests that []-reducing an empty list should probably (IMO) do
one of the following:

* Fail, since it's an ill-defined operation without an explicit unit
* Try to find a suitable unit value (see below), and fail if it doesn't find one

You /could/ try to do something 'sensible' and return 0 or undef, but
this seems likely to result in more confusion.

Another alternative is to give the user the option of specifying such
a unit when using the reduction meta-operator, but this seems to work
against the whole point of [+] (which is brevity). If you want to
specify your own unit, use '&reduce'.

> 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?

Perhaps the operator could have some kind of 'unit' trait? (Or perhaps
'left_unit' and 'right_unit'?)

Stuart

[1] Just remember that unlike foldr/foldl, which are explicitly
right/left associative, [+] is 'DWIM-associative', reflecting the
associativity of the underlying operator.

[2] e.g. 0 is the left (and right) unit of + because 0 + x == x (and x + 0 == 0)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stuart Cook  
View profile  
 More options May 18 2005, 10:25 pm
Newsgroups: perl.perl6.language
From: sco...@gmail.com (Stuart Cook)
Date: Thu, 19 May 2005 12:25:07 +1000
Local: Wed, May 18 2005 10:25 pm
Subject: Re: reduce metaoperator on an empty list
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.

The usual none(@Larry) disclaimer applies, of course...

Stuart


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rob Kinyon  
View profile  
 More options May 18 2005, 10:55 pm
Newsgroups: perl.perl6.language
From: rob.kin...@gmail.com (Rob Kinyon)
Date: Wed, 18 May 2005 22:55:37 -0400
Local: Wed, May 18 2005 10:55 pm
Subject: Re: reduce metaoperator on an empty list
On 5/18/05, Stuart Cook <sco...@gmail.com> 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 [+]@x is eval join( '+', @x
) ...

Rob


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark A. Biggar  
View profile  
 More options May 18 2005, 11:29 pm
Newsgroups: perl.perl6.language
From: m...@biggar.org (Mark A. Biggar)
Date: Wed, 18 May 2005 20:29:43 -0700
Local: Wed, May 18 2005 11:29 pm
Subject: Re: reduce metaoperator on an empty list

Stuart Cook 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.

> The usual none(@Larry) disclaimer applies, of course...

Well the only case where it probably really matters is [+] where you
really want the result to be 0.  Of course +undef == 0, so maybe
returning undef might be okay.  I'm thinking about the case:

[+] grep &some_condition, @a

where you really want the total to be 0, even if the result of the grep
is empty.

A case can also be made for (assuming @a = ();) that

[*}@a == 1
[~]@a eq '' (also covered by ~undef)
[?&]@a ~~ true
[?|]@a ~~ false (also covered by ?undef)
[?^]@a ~~ false (also covered by ?undef)
[+&]@a == MAXINT (whatever that is)
[+|]@a == 0 (also covered by +undef)
[+^]@a == 0 (also covered by +undef)

chained ops are wierd

[<]@a ~~ false
[>]@a ~~ false
[<=]@a ~~ true
[>=]@a ~~ true

Other ops have theoritical values that I don't know if we can handle:

[~&]@a should be an infinitely long bitstring of 1's
[~|]@a should be an infinitely long bitstring of 0's

Again, given that that the really important case [+] is covered by
+undef == 0, maybe just always returning undef is good enough.

--
m...@biggar.org
mark.a.big...@comcast.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rod Adams  
View profile  
 More options May 18 2005, 11:08 pm
Newsgroups: perl.perl6.language
From: r...@rodadams.net (Rod Adams)
Date: Wed, 18 May 2005 22:08:29 -0500
Local: Wed, May 18 2005 11:08 pm
Subject: Re: reduce metaoperator on an empty list

$rod == none(@Larry), and therefore just because I think it should be
that way, doesn't mean it is that way.

But the "eval join" way of looking at it does seem to be consistent with
what I've seen discussed previously, and would provide a useful way to
remember the effects of edge cases.

-- Rod Adams


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stuart Cook  
View profile  
 More options May 19 2005, 2:16 am
Newsgroups: perl.perl6.language
From: sco...@gmail.com (Stuart Cook)
Date: Thu, 19 May 2005 16:16:20 +1000
Local: Thurs, May 19 2005 2:16 am
Subject: Re: reduce metaoperator on an empty list
On 5/19/05, Brad Bowman <l...@bereft.net> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michele Dondi  
View profile  
 More options May 19 2005, 4:50 am
Newsgroups: perl.perl6.language
From: bla...@pcteor1.mi.infn.it (Michele Dondi)
Date: Thu, 19 May 2005 10:50:16 +0200 (CEST)
Local: Thurs, May 19 2005 4:50 am
Subject: Re: reduce metaoperator on an empty list

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"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michele Dondi  
View profile  
 More options May 19 2005, 4:54 am
Newsgroups: perl.perl6.language
From: bla...@pcteor1.mi.infn.it (Michele Dondi)
Date: Thu, 19 May 2005 10:54:50 +0200 (CEST)
Local: Thurs, May 19 2005 4:54 am
Subject: Re: reduce metaoperator on an empty list

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Edward Cherlin  
View profile  
 More options May 19 2005, 5:50 am
Newsgroups: perl.perl6.language
From: cher...@pacbell.net (Edward Cherlin)
Date: Thu, 19 May 2005 02:50:29 -0700
Local: Thurs, May 19 2005 5:50 am
Subject: Re: reduce metaoperator on an empty list
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 element        For

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
C. Scott Ananian  
View profile  
 More options May 19 2005, 10:21 am
Newsgroups: perl.perl6.language
From: csc...@cscott.net (C. Scott Ananian)
Date: Thu, 19 May 2005 10:21:17 -0400 (EDT)
Local: Thurs, May 19 2005 10:21 am
Subject: Re: reduce metaoperator on an empty list

On Wed, 18 May 2005, Rob Kinyon wrote:
> On 5/18/05, Stuart Cook <sco...@gmail.com> 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 [+]@x 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/ )


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sam Vilain  
View profile  
 More options May 19 2005, 10:51 pm
Newsgroups: perl.perl6.language
From: s...@vilain.net (Sam Vilain)
Date: Fri, 20 May 2005 14:51:56 +1200
Local: Thurs, May 19 2005 10:51 pm
Subject: Re: reduce metaoperator on an empty list
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrew Rodland  
View profile  
 More options May 19 2005, 11:42 pm
Newsgroups: perl.perl6.language
From: arodl...@entermail.net (Andrew Rodland)
Date: Thu, 19 May 2005 23:42:52 -0400
Local: Thurs, May 19 2005 11:42 pm
Subject: Re: reduce metaoperator on an empty list
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sam Vilain  
View profile  
 More options May 19 2005, 11:09 pm
Newsgroups: perl.perl6.language
From: s...@vilain.net (Sam Vilain)
Date: Fri, 20 May 2005 15:09:08 +1200
Local: Thurs, May 19 2005 11:09 pm
Subject: Re: reduce metaoperator on an empty list

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:<[+]> (*@args) ::= &reduce.assuming(func => &infix:<+>, first => 0);
   sub prefix:<[*]> (*@args) ::= &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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Randal L. Schwartz  
View profile  
 More options May 20 2005, 9:19 am
Newsgroups: perl.perl6.language
From: mer...@stonehenge.com (Randal L. Schwartz)
Date: 20 May 2005 06:19:50 -0700
Local: Fri, May 20 2005 9:19 am
Subject: Re: reduce metaoperator on an empty list

>>>>> "Randal" == Randal L Schwartz <mer...@stonehenge.com> writes:

Randal> For example, if I wanted the identity hash (where all values are 1,
Randal> but keys are original list elements), I could do:

Randal> my %hash = @somelist.inject({}, { $^a{$^b} = 1; $^a });

And yes, I know I can spell this as:

my %hash = ({}, @somelist).reduce({ $^a{$^b} = 1; $^a });

But it puts things all in the wrong place for me. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Randal L. Schwartz  
View profile  
 More options May 20 2005, 9:09 am
Newsgroups: perl.perl6.language
From: mer...@stonehenge.com (Randal L. Schwartz)
Date: 20 May 2005 06:09:55 -0700
Local: Fri, May 20 2005 9:09 am
Subject: Re: reduce metaoperator on an empty list

>>>>> "Mark" == Mark A Biggar <m...@biggar.org> writes:

Mark> The usual definition of reduce in most languages that support it, is
Mark> that reduce over the empty list produces the Identity value for the
Mark> operation.

In Smalltalk, the equivalent of "reduce" is "inject:into:", so a
"sum" reduce looks like:

  sum := aList inject: 0 into: [:previous :this | previous + this]

Now the advantage here is that if aList is empty, we get back the inject
value.  Thus, the behavior is always well-defined.

The Perl reduce operator treats the first element of the list as the
"inject" value above.  However, if the first element is missing,
the most Perlish thing I can think of is having it return undef,
because it's like you've specified an undef inject value.

I'd also argue that we could provide .inject_into, to make Smalltalkers
happy to always spell out the initial value and codeblock, instead
of relying on the first element of the list for the initial value.

For example, if I wanted the identity hash (where all values are 1,
but keys are original list elements), I could do:

my %hash = @somelist.inject({}, { $^a{$^b} = 1; $^a });

That'd be Way Cool.  Once you get your head around inject, you never
want to go back to reduce. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Macdonald  
View profile  
 More options May 20 2005, 10:18 am
Newsgroups: perl.perl6.language
From: j...@perlwolf.com (John Macdonald)
Date: Fri, 20 May 2005 10:18:49 -0400
Local: Fri, May 20 2005 10:18 am
Subject: Re: reduce metaoperator on an empty list

I think we should provide built-in operators with an attribute
called "identity".  Reduce, when given an empty list, would
check if the operator has a defined identity attribute.  If so,
it is returned as the result of the reduction.  If the opereator
has no identity attribute, reduce throws an exception for an
empty list.

Is there a built-in operator that doesn't have a meaningful
identity value?  I first thought of exponentiation, but it has
an identity value of 1 - you just have to realize that since
it is a right associative operator, the identity has to be
applied from the right.

I suspect that if people ever get into writing code that works
on operators instead of data, there would be additional uses
found for the identity attribute (and there may be additional
operator attributes that make sense there too, although none
come immediately to mind).  MJD will soon have to start working
on the second edition of Higher Order Perl.

--


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark A. Biggar  
View profile  
 More options May 20 2005, 12:07 pm
Newsgroups: perl.perl6.language
From: m...@biggar.org (Mark A. Biggar)
Date: Fri, 20 May 2005 09:07:31 -0700
Local: Fri, May 20 2005 12:07 pm
Subject: Re: reduce metaoperator on an empty list

John Macdonald wrote:
> Is there a built-in operator that doesn't have a meaningful
> identity value?  I first thought of exponentiation, but it has
> an identity value of 1 - you just have to realize that since
> it is a right associative operator, the identity has to be
> applied from the right.

Well the identity of % is +inf (also right side only).  The identities
for ~| and ~^ are infinitely long bitstrings of 0's, while that for ~&
is a similarly long bitstring of 1's.  The chained comparison ops are
weird as depending of which why you define the associativity (and thus
which side's value you return when true) you get either a left side only
or right side only Identity.  E.g. if X<Y is left associative and
returns Y when true then it has a left side identity of -inf, etc.  But
as I'm not sure if the chained ops are actually going to be defined
defined in terms of the associativity of the binary op (the way false
results propagates through messes things up), so that argument may not work.

--
m...@biggar.org
mark.a.big...@comcast.net


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Edward Cherlin  
View profile  
 More options May 20 2005, 4:29 pm
Newsgroups: perl.perl6.language
From: cher...@pacbell.net (Edward Cherlin)
Date: Fri, 20 May 2005 13:29:20 -0700
Local: Fri, May 20 2005 4:29 pm
Subject: Re: [unclassified] Re: reduce metaoperator on an empty list
On Thursday 19 May 2005 19:51, 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

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

Sorry. It's exactly the same material the provide with their
software, so I got confused.

>    [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.

--
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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark A Biggar  
View profile  
 More options May 20 2005, 6:14 pm
Newsgroups: perl.perl6.language
From: mark.a.big...@comcast.net (Mark A Biggar)
Date: Fri, 20 May 2005 22:14:26 +0000
Local: Fri, May 20 2005 6:14 pm
Subject: Re: reduce metaoperator on an empty list

> Mark A. Biggar wrote:
> > Well the identity of % is +inf (also right side only).

> I read $n % any( $n..Inf ) == $n. The point is there's no
> unique right identity and thus (Num,%) disqualifies for a
> Monoid. BTW, the above is a nice example where a junction
> needn't be preserved :)

If as usual the definition of a right identity value e is that a op e = a for all a,
then only +inf works.  Besdies you example should have been;
$n % any (($n+1)..Inf),  $n % $n = 0.

> > E.g. if X<Y is left associative and  returns Y when true then ...

> Sorry, is it the case that $x = $y < $z might put something else
> but 0 or 1 into $x depending on the order relation between $y and $z?

Which is one reason why I siad that it might not make sense to define the chaining ops in terms of the associtivity of the binary ops,  But as we are interested in what [<] over the empty list shoud return , the identity (left or right) of '<' is unimportant as I think that should return false as there is nothing to be less then anything else.  Note that defaulting to undef therefore works in that case.

--
Mark Biggar
m...@biggar.org
mark.a.big...@comcast.net
mbig...@paypal.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matt Fowles  
View profile  
 More options May 21 2005, 12:45 am
Newsgroups: perl.perl6.language
From: uberm...@gmail.com (Matt Fowles)
Date: Sat, 21 May 2005 00:45:59 -0400
Local: Sat, May 21 2005 12:45 am
Subject: Re: reduce metaoperator on an empty list
Mark~

On 5/20/05, mark.a.big...@comcast.net <mark.a.big...@comcast.net> wrote:

On the contrary a mathematician would say that the empty list is
monotonically increasing (vacuously) and the answer should be true.

Matt
--
"Computer Science is merely the post-Turing Decline of Formal Systems Theory."
-???


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Edward Cherlin  
View profile  
 More options May 22 2005, 1:41 am
Newsgroups: perl.perl6.language
From: cher...@pacbell.net (Edward Cherlin)
Date: Sat, 21 May 2005 22:41:00 -0700
Subject: Re: reduce metaoperator on an empty list
On Friday 20 May 2005 07:18, John Macdonald wrote:

> Is there a built-in operator that doesn't have a meaningful
> identity value?  

Certainly.

> I first thought of exponentiation, but it has
> an identity value of 1 - you just have to realize that since
> it is a right associative operator, the identity has to be
> applied from the right.

> I suspect that if people ever get into writing code that works
> on operators instead of data, there would be additional uses
> found for the identity attribute (and there may be additional
> operator attributes that make sense there too, although none
> come immediately to mind).  

APL and J programmers have lots of examples.

--
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Edward Cherlin  
View profile  
 More options May 22 2005, 1:34 am
Newsgroups: perl.perl6.language
From: cher...@pacbell.net (Edward Cherlin)
Date: Sat, 21 May 2005 22:34:14 -0700
Local: Sun, May 22 2005 1:34 am
Subject: Re: reduce metaoperator on an empty list
On Thursday 19 May 2005 20:42, Andrew Rodland wrote:

It's an enhanced APL without the funny characters.

--
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 71   Newer >
« Back to Discussions « Newer topic     Older topic »