Changeset 13527 for docs/Perl6/Spec

Show
Ignore:
Timestamp:
09/21/06 05:43:41 (2 years ago)
Author:
lwall
Message:

1st whack at exporting methods.
Some bogus package names are turning into export tags.
Added generalized pick method.
Added signatures as sort criteria

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • docs/Perl6/Spec/Functions.pod

    r13519 r13527  
    1414 Date:          12 Mar 2005 
    1515 Last Modified: 20 Sep 2006 
    16  Version:       7 
     16 Version:       8 
    1717 
    1818This document attempts to document the list of builtin functions in Perl 6. 
     
    2727=head1 Notes 
    2828 
    29 In Perl 6, all builtin functions belong to a named package. Not all 
     29In Perl 6, all builtin functions belong to a named package (generally a 
     30class or role). Not all 
    3031functions are guaranteed to be imported into the global package 
    3132C<::*>. In addition, the list of functions imported into C<::*> will be 
     
    5455of this document. 
    5556 
     57=head2 Multis vs. Functions 
     58 
     59In actual fact, most of the "functions" defined here are multi subs, 
     60or are multi methods that are also exported as multi subs.  Multi subs 
     61are all visible in the global namespace (unless declared with a "my 
     62multi").  The assumption is that with sufficiently specific typing on 
     63the multis, the user is free to extend a particular name to new types. 
     64 
    5665=head1 Type Declarations 
    5766 
     
    100109(Plus the corresponding C<StrLingua> types, presumably.) 
    101110 
    102 =item MatchTest 
    103  
    104  subset MatchTest of Item | Junction; 
     111=item Matcher 
     112 
     113 subset Matcher of Item | Junction; 
    105114 
    106115Used to supply a test to match against. Assume C<~~> will be used against it. 
     
    114123=head1 Function Packages 
    115124 
    116 =head2 Math::Basic 
    117  
    118 B<API document>: L<Math::Basic> 
    119  
    120 C<Math::Basic> provides a number of constants in addition to the basic 
     125=head2 Num 
     126 
     127The following are all defined in the C<Num> role: 
     128 
     129B<API document>: L<Num> 
     130 
     131C<Num> provides a number of constants in addition to the basic 
    121132mathematical functions. To get these constants, you must request 
    122133them: 
    123134 
    124  use Math::Basic :constants; 
    125  
    126 or use the full name, e.g. C<Math::Basic::pi>. 
     135 use Num :constants; 
     136 
     137or use the full name, e.g. C<Num::pi>. 
    127138 
    128139=over 
     
    130141=item abs 
    131142 
    132  our Num multi Math::Basic::abs ( Num $x ) 
     143 our Num multi method abs ( Num $x: ) is export 
    133144 
    134145Absolute Value. 
     
    136147=item floor 
    137148 
    138  our Int multi Math::Basic::floor ( Num $x ) 
     149 our Int multi method floor ( Num $x: ) is export 
    139150 
    140151Returns the highest integer not greater than $x. 
     
    142153=item ceiling 
    143154 
    144  our Int multi Math::Basic::ceiling ( Num $x ) 
    145  our Int multi Math::Basic::ceil ( Num $x ) 
     155 our Int multi method ceil ( Num $x: ) is export 
    146156 
    147157Returns the lowest integer not less than $x. 
     
    149159=item round 
    150160 
    151  our Int multi Math::Basic::round ( Num $x ) 
     161 our Int multi method round ( Num $x: ) is export 
    152162 
    153163Returns the nearest integer to $x.  The algorithm is floor($x + 0.5). 
     
    156166=item truncate 
    157167 
    158  our Int multi Math::Basic::truncate ( Num $x ) 
    159  our Int multi Math::Basic::int ( Num $x ) 
     168 our Int multi method truncate ( Num $x: ) is export 
     169 our Int multi method int ( Num $x: ) is export 
    160170 
    161171Returns the closest integer to $x whose absolute value is not greater 
     
    167177=item exp 
    168178 
    169  our Num multi Math::Basic::exp ( Num $exponent, Num :$base = Num::e ) 
     179 our Num multi method exp ( Num $exponent: Num :$base = Num::e ) is export 
    170180 
    171181Performs similar to C<$base ** $exponent>. C<$base> defaults to the 
     
    174184=item log 
    175185 
    176  our Num multi Math::Basic::log ( Num $x, Num :$base ) 
     186 our Num multi method log ( Num $x: Num :$base = Num::e ) is export 
    177187 
    178188Logarithm of base C<$base>, default Natural. Calling with C<$x == 0> is an 
     
    182192=item log10 
    183193 
    184  our Num multi Math::Basic::log10 (Num $x); 
     194 our Num multi method log10 (Num $x:); is export 
    185195 
    186196A base C<10> logarithm, othewise identical to C<log>. 
     
    188198=item rand 
    189199 
    190  our Num multi Math::Basic::rand ( Num $x = 1 ) 
     200 our Num multi method rand ( Num $x: ) 
     201 our Num multi rand ( Num $x = 1 ) 
    191202 
    192203Pseudo random number in range C<< 0 ..^ $x >>.  That is, C<0> is theoretically possible, 
     
    196207=item sign 
    197208 
    198  our Int multi Math::Basic::sign ( Num $x ) 
     209 our Int multi method sign ( Num $x: ) is export 
    199210 
    200211Returns 1 when C<$x> is greater than 0, -1 when it is less than 0, 0 when it 
     
    203214=item srand 
    204215 
    205  multi Math::Basic::srand ( Num $seed = default_seed_algorithm()) 
     216 multi method srand ( Num $seed: ) 
     217 multi srand ( Num $seed = default_seed_algorithm()) 
    206218 
    207219Seed the generator C<rand> uses. C<$seed> defaults to some combination 
     
    213225=item sqrt 
    214226 
    215  our Num multi Math::Basic::sqrt ( Num $x ) 
     227 our Num multi method sqrt ( Num $x: ) is export 
    216228 
    217229Returns the square root of the parameter. 
     
    230242 
    231243 
    232 =head2 Math::Trig 
     244=head2 The :Trig tag 
     245 
     246The following are also defined in C<Num> but not exported without a C<:Trig> 
     247tag.  (Which installs their names into C<Num::Trig>, as it happens.) 
    233248 
    234249=over 4 
     
    236251=item I<Standard Trig Functions> 
    237252 
    238  our Num multi Num::func ( Num  $x, $base = 'radians' ) 
    239  our Num multi Math::Trig::func ( Num $x, $base = 'radians' ) 
     253 Num multi method func ( Num  $x: $base = 'radians' ) is export(:Trig) 
    240254 
    241255where I<func> is one of: 
     
    259273a consistent base so you don't have to supply it with every call: 
    260274 
    261  my module Trig ::= Math::Trig.assuming(:base<degrees>); 
     275 my module Trig ::= Num::Trig.assuming(:base<degrees>); 
    262276 
    263277This overrides the default of "radians". 
     
    265279=item atan2 
    266280 
    267  our Num multi Math::Trig::atan2 ( Num $y, Num $x = 1, Num $base ) 
     281 our Num multi method atan2 ( Num $y: Num $y = 1 ) 
     282 our Num multi atan2 ( Num $y, Num $x = 1 ) 
    268283 
    269284This second form of C<atan> computes the arctangent of $y/$x, and takes 
    270285the quadrant into account. Otherwise behaves as other trigonometric functions. 
    271  
    272 [Note: changed atan back to atan2, or the default $x = 1 will confuse MMD. 
    273 The other alternative would be to remove the default. --law] 
    274286 
    275287=back 
     
    436448=head2 Array 
    437449 
     450All these methods are defined in the C<Array> role/class. 
     451 
    438452=over 
    439453 
    440454=item delete 
    441455 
    442  our List multi method Array::delete (@array : *@indices ) 
     456 our List multi method delete (@array : *@indices ) is export 
    443457 
    444458Sets elements specified by C<@indices> in the invocant to a 
     
    457471=item exists 
    458472 
    459  our Bool multi method Array::exists (@array : Int *@indices ) 
     473 our Bool multi method exists (@array : Int *@indices ) is export 
    460474 
    461475True if the specified Array element has been assigned to. This 
     
    470484=item pop 
    471485 
    472  
    473  our Scalar multi Array::pop ( @array is rw ) 
    474  our Scalar multi method Array::pop ( @array: ) 
     486 our Scalar multi method pop ( @array: ) is export 
    475487 
    476488Remove the last element of C<@array> and return it. 
     
    478490=item push 
    479491 
    480  our Int multi Array::push ( @array is rw, *@values ) 
    481  our Int multi method Array::push ( @array: *@values ) 
     492 our Int multi method push ( @array: *@values ) is export 
    482493 
    483494Add to the end of C<@array>, all of the subsequent arguments. 
     
    485496=item shift 
    486497 
    487  our Scalar multi Array::shift ( @array is rw  ) 
    488  our Scalar multi method Array::shift ( @array:  ) 
     498 our Scalar multi method shift ( @array:  ) is export 
    489499 
    490500Remove the first element from C<@array> and return it. 
     
    492502=item splice 
    493503 
    494  our List multi Array::splice( @array is rw, Int $offset = 0, Int $size?, *@values ) 
     504 our List multi method splice( @array is rw, Int $offset = 0, Int $size?, *@values ) is export 
    495505 
    496506C<splice> fills many niches in array-management, but its fundamental behavior 
     
    524534=item unshift 
    525535 
    526  our Int multi Array::unshift ( @array is rw, *@values ) 
    527  our Int multi method Array::unshift ( @array: *@values ) 
     536 our Int multi method unshift ( @array: *@values ) is export 
    528537 
    529538C<unshift> adds the values onto the start of the C<@array>. 
     
    537546=item values 
    538547 
    539  multi Int|List Array::keys ( @array ; MatchTest *@indextests ) 
    540  multi Int|List Array::kv ( @array ; MatchTest *@indextests ) 
    541  multi Int|(List of Pair) Array::pairs  (@array ; MatchTest *@indextests ) 
    542  multi Int|List Array::values ( @array ; MatchTest *@indextests ) 
    543  
    544 (XXX these signatures are wrong. -luqui) 
     548our List multi method keys ( @array: Matcher *@indextests ) is export 
     549our List multi method kv ( @array: Matcher *@indextests ) is export 
     550our List multi method pairs  (@array: Matcher *@indextests ) is export 
     551our List multi method values ( @array: Matcher *@indextests ) is export 
    545552 
    546553Iterates the elements of C<@array>, in order. 
     
    565572=head2 List 
    566573 
     574The following are defined in the C<List> role/class: 
     575 
    567576=over 
    568577 
    569578=item classify 
    570579 
    571  our Lazy of Pair multi Array::classify ( @values, Code *&test ) 
    572  our Lazy of Pair multi Array::classify ( @values, MatchTest $test ) 
    573  our Lazy of Pair multi List::classify ( MatchTest $test, *@values ) 
     580 our Lazy of Pair multi method classify ( @values: Matcher $test ) 
     581 our Lazy of Pair multi classify ( Matcher $test, *@values ) 
    574582 
    575583C<classify> takes a list or array of values and returns a lazily evaluated 
     
    593601=item grep 
    594602 
    595  our Lazy multi Array::grep ( @values, Code *&test ) 
    596  our Lazy multi Array::grep ( @values, MatchTest $test ) 
    597  our Lazy multi List::grep ( MatchTest $test, *@values ) 
     603 our Lazy multi method grep ( @values: Matcher $test ) 
     604 our Lazy multi grep ( Matcher $test, *@values ) 
    598605 
    599606C<grep> takes a list or array of values and returns a lazily evaluated 
     
    603610Here is an example of its use: 
    604611 
    605  @friends = grep { .is_friend } @coworkers; 
     612 @friends = grep { .is_friend }, @coworkers; 
    606613 
    607614This takes the array C<@coworkers>, checks every element to see 
     
    609616the resulting list to store into C<@friends>. 
    610617 
     618Note that, unlike in Perl 5, a comma is required after the C<Matcher> 
     619in the multi form. 
     620 
     621=item pick 
     622 
     623 our Lazy multi method pick ( @values: Int $num = 1 ) 
     624 our Lazy multi method pick ( @values: Whatever ) 
     625 our Lazy multi pick ( Int $num, *@values ) 
     626 our Lazy multi pick ( Whatever, *@values ) 
     627 
     628C<pick> takes a list or array of values and returns a random selection 
     629of elements from the list (without replacement).  If C<*> is specified 
     630as the number (or if the number of elements in the list is less than 
     631the specified number), all the available elements are returned in 
     632random order: 
     633 
     634    @team = @volunteers.pick(5); 
     635    @shuffled = @deck.pick(*); 
     636 
    611637=item join 
    612638 
    613  our Str multi Array::join ( @values, Str $separator? ) 
    614  our Str multi List::join ( Str $separator?, *@values ) 
     639 our Str multi method join ( @values: Str $separator = ' ' ) 
     640 our Str multi join ( Str $separator = ' ', *@values ) 
    615641 
    616642C<join> returns a single string comprised of all of the elements 
     
    620646Given an empty list, C<join> returns the empty string. 
    621647 
     648To join with no separator you can use the C<[~]> reduce operator. 
     649 
    622650=item map 
    623651 
    624  our Lazy multi Array::map ( @values, Code *&expression ) 
    625  our Lazy multi List::map ( Code $expression?, *@values ) 
     652 our Lazy multi method map ( @values: Code *&expression ) 
     653 our Lazy multi map ( Code $expression, *@values ) 
    626654 
    627655C<map> returns a lazily evaluated list which is comprised of 
     
    631659Here is an example of its use: 
    632660 
    633  @addresses = map { %addresses_by_name<$_> } @names; 
     661 @addresses = map { %addresses_by_name<$_> }, @names; 
    634662 
    635663Here we take an array of names, and look each name up in 
     
    641669that were passed. For example: 
    642670 
    643  @factors = map { prime_factors($_) } @composites; 
     671 @factors = map { prime_factors($_) }, @composites; 
    644672 
    645673=item reduce 
    646674 
    647  our Scalar multi Array::reduce ( @values ; Code *&expression ) 
    648  our Scalar multi List::reduce ( Code $expression ; *@values ) 
     675 our Item multi method reduce ( @values: Code *&expression ) 
     676 our Item multi reduce ( Code $expression ; *@values ) 
    649677   my $res; 
    650678   for @values -> $cur { 
     
    658686=item reverse 
    659687 
    660  our Hash multi Hash::reverse ( %hash ) { 
    661    (my %result){%hash.values} = %hash.keys; 
    662    %result; 
     688 role Hash { 
     689     our Hash multi method reverse ( %hash: ) is export { 
     690       (my %result){%hash.values} = %hash.keys; 
     691       %result; 
     692     } 
    663693 } 
    664694 
    665  multi Lazy Array::reverse ( @values ) 
    666  multi Lazy List::reverse ( *@values ) { 
     695 our Lazy multi method reverse ( @values: ) is export 
     696 our Lazy multi reverse ( *@values ) { 
    667697    gather { 
    668698        1 while take pop @values; 
     
    670700 } 
    671701 
    672  multi Str Str::reverse ( $str ) { 
    673     split('', $str).reverse.join 
    674  ) 
     702 role Str { 
     703     our Str multi method reverse ( $str: ) is export { 
     704        split('', $str).reverse.join 
     705     ) 
     706 } 
    675707 
    676708 
    677709=item sort 
    678710 
    679  subset KeyExtractor of Code(Any --> Any); 
    680  subset Comparator   of Code(Any, Any --> Int ); 
    681  subset SortCriterion of KeyExtractor | Comparator | Pair(KeyExtractor, Comparator); 
    682  
    683  our Array multi Array::sort( @values is rw, *&by, Bit $inplace? ) 
    684  our Array multi Array::sort( @values is rw, SortCriterion @by, Bit $inplace? ) 
    685  our Array multi Array::sort( @values is rw, SortCriterion $by = &infix:<cmp>, Bit $inplace? ) 
    686  
    687  our List multi List::sort( SortCriterion @by,  *@values ) 
    688  our List multi List::sort( SortCriterion $by = &infix:<cmp>, *@values ) 
     711 subset KeyExtractor of Code where { .sig === :(Any --> Any) }; 
     712 subset Comparator   of Code where { .sig === :(Any, Any --> Int ) }; 
     713 subset SortCriterion of Signature | KeyExtractor | Comparator | Pair(KeyExtractor, Comparator); 
     714 
     715 our Array multi method sort( @values: *&by ) 
     716 our Array multi method sort( @values: SortCriterion @by ) 
     717 our Array multi method sort( @values: SortCriterion $by = &infix:<cmp> ) 
     718 
     719 our List multi sort( SortCriterion @by,  *@values ) 
     720 our List multi sort( SortCriterion $by = &infix:<cmp>, *@values ) 
    689721 
    690722Returns C<@values> sorted, using criteria C<$by> or C<@by> for 
    691 comparisons. C<@by> differs from C<$by> in that each criteria is 
     723comparisons. C<@by> differs from C<$by> in that each criterion is 
    692724applied, in order, until a non-zero (tie) result is achieved. 
    693725 
    694 Criterion can take a few different forms: 
     726Criteria can take a few different forms: 
    695727 
    696728=over 8 
     
    704736=item KeyExtractor 
    705737 
    706 A closure with arity of 1, which returns the "key" by which to sort. If 
    707 the closure returns a Num, C<E<lt>=E<gt>> is used for comparison, 
    708 otherwise C<cmp>. 
     738A closure with arity of 1, which returns the "key" by which to sort. 
     739Values are compared using C<cmp>, which in Perl 6 does different 
     740comparisons depending on the types.  (To get a Perl 5 string comparison 
     741you must use C<leg> instead.) 
    709742 
    710743=item Pair(KeyExtractor, Comparator) 
     
    712745A combination of the two methods above, for when one wishes to take 
    713746advantage of the internal caching of keys that is expected to happen, 
    714 but wishes to compare them with something other than C<E<lt>=E<gt>> or 
    715 C<cmp>. 
    716  
    717 =back 
    718  
    719 Any Criterion may receive either or both of the traits C<is descending> 
    720 and C<is insensitive> to reverse the order of sort, or the adjust the 
    721 case sensitivity of C<cmp> as a Comparator. 
     747but wishes to compare them with something other than C<cmp>, such 
     748as C<E<lt>=E<gt>> or C<leg>. 
     749 
     750=item Signature 
     751 
     752If a signature is specified as a criterion, the signature is bound 
     753to each value and then each parameter does comparisons in positional order 
     754according to its type, as modified by its traits.  Sort-specific traits 
     755are allowed in such a signature such as C<is descending> or C<is insensitive>. 
     756Basically, the system will write the body of the key extraction subroutine 
     757for you based on the signature. 
     758 
     759=back 
     760 
     761Any Criterion may receive either or both of the mixins C<descending> 
     762and C<insensitive> to reverse the order of sort, or the adjust the 
     763case sensitivity of C<cmp> as a Comparator.  (Mixins are applied to 
     764values using C<but>.) 
    722765 
    723766If all criteria are exhausted when comparing two elements, sort should 
    724767return them in the same relative order they had in C<@values>. 
    725768 
    726 If C<$inplace> is specified, the array is sorted in place. 
     769To sort an array in place use the C<.=sort> mutator form. 
    727770 
    728771See L<http://www.nntp.perl.org/group/perl.perl6.language/16578> for more 
     
    782825=item values 
    783826 
    784  multi Int|List Hash::keys ( %hash ; MatchTest *@keytests ) 
    785  multi Int|List Hash::kv ( %hash ; MatchTest *@keytests ) 
    786  multi Int|(List of Pair) Hash::pairs  (%hash ; MatchTest *@keytests ) 
    787  multi Int|List Hash::values ( %hash ; MatchTest *@keytests ) 
     827 multi Int|List Hash::keys ( %hash ; Matcher *@keytests ) 
     828 multi Int|List Hash::kv ( %hash ; Matcher *@keytests ) 
     829 multi Int|(List of Pair) Hash::pairs  (%hash ; Matcher *@keytests ) 
     830 multi Int|List Hash::values ( %hash ; Matcher *@keytests ) 
    788831 
    789832Iterates the elements of C<%hash> in no apparent order, but the order