Optional parameters: Difference between revisions

Content added Content deleted
(add E example)
(Added Perl.)
Line 363: Line 363:


OCaml does not support optional positional parameters, because, since OCaml supports currying, it would conflict with partial applications, where you do not provide all the arguments to a function, and it results in a function which expects the remaining arguments.
OCaml does not support optional positional parameters, because, since OCaml supports currying, it would conflict with partial applications, where you do not provide all the arguments to a function, and it results in a function which expects the remaining arguments.

=={{header|Perl}}==
This function expects its first argument to be a reference to an array of arrays. It interprets any remaining arguments as a hash of optional parameters.

<lang perl>sub sorttable
{my @table = @{shift()};
my %opt =
(ordering => sub {$_[0] cmp $_[1]}, column => 0, reverse => 0, @_);
my $col = $opt{column};
my @result = sort
{$opt{ordering}->($a->[$col], $b->[$col])}
@table;
return ($opt{reverse} ? [reverse @result] : \@result);}</lang>

An example of use:

<lang perl>my $a = [["a", "b", "c"], ["", "q", "z"], ["zap", "zip", "Zot"]];
foreach (@{sorttable $a, column => 1, reverse => 1})
{foreach (@$_)
{printf "%-5s", $_;}
print "\n";}</lang>


=={{header|Python}}==
=={{header|Python}}==