Optional parameters: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 223: | Line 223: | ||
: ?table |
: ?table |
||
& sortTable$(!table.(column.2) (reverse.yes)) |
& sortTable$(!table.(column.2) (reverse.yes)) |
||
);</lang> |
);</lang> |
||
=={{header|C}}== |
=={{header|C}}== |
||
Line 487: | Line 487: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
=={{header|Clojure}}== |
|||
There is a built-in sort routine, but rather than figure out what all these arguments are supposed to mean, I've just defined the interface. |
|||
<lang Clojure>(defn sort [table & {:keys [ordering column reverse?] |
|||
:or {ordering :lex, column 1}}] |
|||
(println table ordering column reverse?)) |
|||
(sort [1 8 3] :reverse? true) |
|||
[1 8 3] :lex 1 true</lang> |
|||
=={{header|Common Lisp}}== |
|||
Common Lisp has both named and positional parameters. The following example shows optional named parameters, using the <code>&key</code> keyword. Optional positional parameters are specified using the <code>&optional</code> keyword. |
|||
<lang lisp>(defun sort-table (table &key (ordering #'string<) |
|||
(column 0) |
|||
reverse) |
|||
(sort table (if reverse |
|||
(complement ordering) |
|||
ordering) |
|||
:key (lambda (row) (elt row column))))</lang> |
|||
(Notes: The builtin [http://www.lispworks.com/documentation/HyperSpec/Body/f_sort_.htm sort] takes a "less than" predicate function. The [http://www.lispworks.com/documentation/HyperSpec/Body/f_comple.htm complement] function inverts a predicate.) |
|||
Example uses: |
|||
<lang lisp>CL-USER> (defparameter *data* '(("a" "b" "c") ("" "q" "z") ("zap" "zip" "Zot"))) |
|||
*DATA* |
|||
CL-USER> (sort-table *data*) |
|||
(("" "q" "z") ("a" "b" "c") ("zap" "zip" "Zot")) |
|||
CL-USER> (sort-table *data* :column 2) |
|||
(("zap" "zip" "Zot") ("a" "b" "c") ("" "q" "z")) |
|||
CL-USER> (sort-table *data* :column 1) |
|||
(("a" "b" "c") ("" "q" "z") ("zap" "zip" "Zot")) |
|||
CL-USER> (sort-table *data* :column 1 :reverse t) |
|||
(("zap" "zip" "Zot") ("" "q" "z") ("a" "b" "c")) |
|||
CL-USER> (sort-table *data* :ordering (lambda (a b) (> (length a) (length b)))) |
|||
(("zap" "zip" "Zot") ("a" "b" "c") ("" "q" "z"))</lang> |
|||
=={{header|D}}== |
=={{header|D}}== |
||
Line 544: | Line 587: | ||
</pre> |
</pre> |
||
Another way to emulate optional arguments is with function overloading, creating several functions with a different number of arguments. |
Another way to emulate optional arguments is with function overloading, creating several functions with a different number of arguments. |
||
=={{header|Clojure}}== |
|||
There is a built-in sort routine, but rather than figure out what all these arguments are supposed to mean, I've just defined the interface. |
|||
<lang Clojure>(defn sort [table & {:keys [ordering column reverse?] |
|||
:or {ordering :lex, column 1}}] |
|||
(println table ordering column reverse?)) |
|||
(sort [1 8 3] :reverse? true) |
|||
[1 8 3] :lex 1 true</lang> |
|||
=={{header|Common Lisp}}== |
|||
Common Lisp has both named and positional parameters. The following example shows optional named parameters, using the <code>&key</code> keyword. Optional positional parameters are specified using the <code>&optional</code> keyword. |
|||
<lang lisp>(defun sort-table (table &key (ordering #'string<) |
|||
(column 0) |
|||
reverse) |
|||
(sort table (if reverse |
|||
(complement ordering) |
|||
ordering) |
|||
:key (lambda (row) (elt row column))))</lang> |
|||
(Notes: The builtin [http://www.lispworks.com/documentation/HyperSpec/Body/f_sort_.htm sort] takes a "less than" predicate function. The [http://www.lispworks.com/documentation/HyperSpec/Body/f_comple.htm complement] function inverts a predicate.) |
|||
Example uses: |
|||
<lang lisp>CL-USER> (defparameter *data* '(("a" "b" "c") ("" "q" "z") ("zap" "zip" "Zot"))) |
|||
*DATA* |
|||
CL-USER> (sort-table *data*) |
|||
(("" "q" "z") ("a" "b" "c") ("zap" "zip" "Zot")) |
|||
CL-USER> (sort-table *data* :column 2) |
|||
(("zap" "zip" "Zot") ("a" "b" "c") ("" "q" "z")) |
|||
CL-USER> (sort-table *data* :column 1) |
|||
(("a" "b" "c") ("" "q" "z") ("zap" "zip" "Zot")) |
|||
CL-USER> (sort-table *data* :column 1 :reverse t) |
|||
(("zap" "zip" "Zot") ("" "q" "z") ("a" "b" "c")) |
|||
CL-USER> (sort-table *data* :ordering (lambda (a b) (> (length a) (length b)))) |
|||
(("zap" "zip" "Zot") ("a" "b" "c") ("" "q" "z"))</lang> |
|||
=={{header|E}}== |
=={{header|E}}== |
||
Line 713: | Line 713: | ||
reverse [{"0789","123","456"},{"456","0789","123"},{"123","456","0789"}] |
reverse [{"0789","123","456"},{"456","0789","123"},{"123","456","0789"}] |
||
</pre> |
</pre> |
||
=={{header|F_Sharp|F#}}== |
|||
{{trans|Oz}} |
|||
F# supports optional parameters for members only, not for free-standing functions. |
|||
Optional parameters are marked by using a question mark in front of the identifier. Their values are passed as option types, i.e. as <code>Some value</code> or <code>None</code>. The helper function <code>defaultArg</code> can be used to specify default values. In the example below, we use shadowing in order to reuse the identifiers <code>ordering</code>, <code>column</code> and <code>reverse</code>. |
|||
Typically, parameters are named at the caller site when optional parameters are involved. However, this is not technically required as long as only right-most arguments are omitted. |
|||
<lang fsharp>type Table(rows:string[][]) = |
|||
// in-place sorting of rows |
|||
member x.Sort(?ordering, ?column, ?reverse) = |
|||
let ordering = defaultArg ordering compare |
|||
let column = defaultArg column 0 |
|||
let reverse = defaultArg reverse false |
|||
let factor = if reverse then -1 else 1 |
|||
let comparer (row1:string[]) (row2:string[]) = |
|||
factor * ordering row1.[column] row2.[column] |
|||
Array.sortInPlaceWith comparer rows |
|||
member x.Print() = |
|||
for row in rows do printfn "%A" row |
|||
// Example usage |
|||
let t = new Table([| [|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|] |
|||
[|"can"; "z"; "a"|] |]) |
|||
printfn "Unsorted"; t.Print() |
|||
t.Sort() |
|||
printfn "Default sort"; t.Print() |
|||
t.Sort(column=2) |
|||
printfn "Sorted by col. 2"; t.Print() |
|||
t.Sort(column=1) |
|||
printfn "Sorted by col. 1"; t.Print() |
|||
t.Sort(column=1, reverse=true) |
|||
printfn "Reverse sorted by col. 1"; t.Print() |
|||
t.Sort(ordering=fun s1 s2 -> compare s2.Length s1.Length) |
|||
printfn "Sorted by decreasing length"; t.Print()</lang> |
|||
Output: |
|||
<pre>Unsorted |
|||
[|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|] |
|||
[|"can"; "z"; "a"|] |
|||
Default sort |
|||
[|""; "q"; "z"|] |
|||
[|"a"; "b"; "c"|] |
|||
[|"can"; "z"; "a"|] |
|||
Sorted by col. 2 |
|||
[|"can"; "z"; "a"|] |
|||
[|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|] |
|||
Sorted by col. 1 |
|||
[|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|] |
|||
[|"can"; "z"; "a"|] |
|||
Reverse sorted by col. 1 |
|||
[|"can"; "z"; "a"|] |
|||
[|""; "q"; "z"|] |
|||
[|"a"; "b"; "c"|] |
|||
Sorted by decreasing length |
|||
[|"can"; "z"; "a"|] |
|||
[|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|]</pre> |
|||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 825: | Line 898: | ||
end program UsingTest</lang> |
end program UsingTest</lang> |
||
=={{header|F_Sharp|F#}}== |
|||
{{trans|Oz}} |
|||
F# supports optional parameters for members only, not for free-standing functions. |
|||
Optional parameters are marked by using a question mark in front of the identifier. Their values are passed as option types, i.e. as <code>Some value</code> or <code>None</code>. The helper function <code>defaultArg</code> can be used to specify default values. In the example below, we use shadowing in order to reuse the identifiers <code>ordering</code>, <code>column</code> and <code>reverse</code>. |
|||
Typically, parameters are named at the caller site when optional parameters are involved. However, this is not technically required as long as only right-most arguments are omitted. |
|||
<lang fsharp>type Table(rows:string[][]) = |
|||
// in-place sorting of rows |
|||
member x.Sort(?ordering, ?column, ?reverse) = |
|||
let ordering = defaultArg ordering compare |
|||
let column = defaultArg column 0 |
|||
let reverse = defaultArg reverse false |
|||
let factor = if reverse then -1 else 1 |
|||
let comparer (row1:string[]) (row2:string[]) = |
|||
factor * ordering row1.[column] row2.[column] |
|||
Array.sortInPlaceWith comparer rows |
|||
member x.Print() = |
|||
for row in rows do printfn "%A" row |
|||
// Example usage |
|||
let t = new Table([| [|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|] |
|||
[|"can"; "z"; "a"|] |]) |
|||
printfn "Unsorted"; t.Print() |
|||
t.Sort() |
|||
printfn "Default sort"; t.Print() |
|||
t.Sort(column=2) |
|||
printfn "Sorted by col. 2"; t.Print() |
|||
t.Sort(column=1) |
|||
printfn "Sorted by col. 1"; t.Print() |
|||
t.Sort(column=1, reverse=true) |
|||
printfn "Reverse sorted by col. 1"; t.Print() |
|||
t.Sort(ordering=fun s1 s2 -> compare s2.Length s1.Length) |
|||
printfn "Sorted by decreasing length"; t.Print()</lang> |
|||
Output: |
|||
<pre>Unsorted |
|||
[|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|] |
|||
[|"can"; "z"; "a"|] |
|||
Default sort |
|||
[|""; "q"; "z"|] |
|||
[|"a"; "b"; "c"|] |
|||
[|"can"; "z"; "a"|] |
|||
Sorted by col. 2 |
|||
[|"can"; "z"; "a"|] |
|||
[|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|] |
|||
Sorted by col. 1 |
|||
[|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|] |
|||
[|"can"; "z"; "a"|] |
|||
Reverse sorted by col. 1 |
|||
[|"can"; "z"; "a"|] |
|||
[|""; "q"; "z"|] |
|||
[|"a"; "b"; "c"|] |
|||
Sorted by decreasing length |
|||
[|"can"; "z"; "a"|] |
|||
[|"a"; "b"; "c"|] |
|||
[|""; "q"; "z"|]</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 1,640: | Line 1,640: | ||
sortarray(#items)</lang> |
sortarray(#items)</lang> |
||
=={{header|TIScript}}== |
|||
TIScript allows to define optional parameters with default values: |
|||
<lang javascript>function sorter(table, ordering = "lexicographic", column = 0, reverse = false) { |
|||
// ... |
|||
} |
|||
sorter(the_data,"numeric");</lang> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 1,940: | Line 1,930: | ||
{printf "%-5s", $_;} |
{printf "%-5s", $_;} |
||
print "\n";}</lang> |
print "\n";}</lang> |
||
=={{header|Perl 6}}== |
|||
Using named parameters: |
|||
<lang perl6>method sorttable(:$column = 0, :$reverse, :&ordering = &infix:<cmp>) { |
|||
my @result = self»[$column].sort: &ordering; |
|||
return $reverse ?? @result.reverse !! @result; |
|||
}</lang> |
|||
Using optional positional parameters: |
|||
<lang perl6>method sorttable-pos($column = 0, $reverse?, &ordering = &infix:<cmp>) { |
|||
my @result = self»[$column].sort: &ordering; |
|||
return $reverse ?? @result.reverse !! @result; |
|||
}</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,115: | Line 2,092: | ||
#:key (λ (row) (list-ref row column)))) |
#:key (λ (row) (list-ref row column)))) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Using named parameters: |
|||
<lang perl6>method sorttable(:$column = 0, :$reverse, :&ordering = &infix:<cmp>) { |
|||
my @result = self»[$column].sort: &ordering; |
|||
return $reverse ?? @result.reverse !! @result; |
|||
}</lang> |
|||
Using optional positional parameters: |
|||
<lang perl6>method sorttable-pos($column = 0, $reverse?, &ordering = &infix:<cmp>) { |
|||
my @result = self»[$column].sort: &ordering; |
|||
return $reverse ?? @result.reverse !! @result; |
|||
}</lang> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,449: | Line 2,440: | ||
apply {{a b} {expr {[string length $b]-[string length $a]}}} |
apply {{a b} {expr {[string length $b]-[string length $a]}}} |
||
}]</lang> |
}]</lang> |
||
=={{header|TIScript}}== |
|||
TIScript allows to define optional parameters with default values: |
|||
<lang javascript>function sorter(table, ordering = "lexicographic", column = 0, reverse = false) { |
|||
// ... |
|||
} |
|||
sorter(the_data,"numeric");</lang> |
|||
=={{header|Unix Shell}}== |
=={{header|Unix Shell}}== |
||
Line 2,611: | Line 2,612: | ||
optional_parameters theRange:="A1:C4", ordering:=1, column:=2, reverse:=1 |
optional_parameters theRange:="A1:C4", ordering:=1, column:=2, reverse:=1 |
||
End Sub</lang> |
End Sub</lang> |
||
=={{header|XSLT}}== |
=={{header|XSLT}}== |
||
You can give any template parameter a default value using the optional "select" attribute. |
You can give any template parameter a default value using the optional "select" attribute. |