Optional parameters: Difference between revisions

From Rosetta Code
Content added Content deleted
(new task)
 
(+ Tcl)
Line 48: Line 48:
CL-USER> (sort-table *data* :ordering (lambda (a b) (> (length a) (length b))))
CL-USER> (sort-table *data* :ordering (lambda (a b) (> (length a) (length b))))
(("zap" "zip" "Zot") ("a" "b" "c") ("" "q" "z"))</lang>
(("zap" "zip" "Zot") ("a" "b" "c") ("" "q" "z"))</lang>


=={{header|Tcl}}==

In Tcl, optional arguments are collected when the last argument
to a proc definition is called "args". The ''lsort'' command has a similar API, but with different keywords, so we adapt them.
<lang Tcl>package require Tcl 8.5

proc tablesort {table args} {
array set opt {-ordering "" -column 0 -reverse 0}
array set opt $args
set pars [list -index $opt(-column)]
if $opt(-reverse) {lappend pars -decreasing}
if {$opt(-ordering) ne ""} {lappend pars -command $opt(-ordering)}
lsort {*}$pars $table
}

% tablesort $data
{"" q z} {a b c} {zap zip Zot}

% tablesort $data -column 1
{a b c} {"" q z} {zap zip Zot}

% tablesort $data -column 0
{"" q z} {a b c} {zap zip Zot}

% tablesort $data -column 0 -reverse 1
{zap zip Zot} {a b c} {"" q z}

% tablesort $data -ordering {
apply {{a b} {expr {[string length $b]-[string length $a]}}}
}
{zap zip Zot} {a b c} {"" q z}
</lang>

Revision as of 06:10, 24 May 2009

Task
Optional parameters
You are encouraged to solve this task according to the task description, using any language you may know.

Define a function/method/subroutine which sorts a sequence ("table") of sequences ("rows") of strings ("cells"), by one of the strings. Besides the input to be sorted, it shall have the following optional parameters:

ordering
A function specifying the ordering of strings; lexicographic by default.
column
An integer specifying which string of each row to compare; the first by default.
reverse
Reverses the ordering.

This task should be considered to include both positional and named optional parameters, as well as overloading on argument count as in Java or selector name as in Smalltalk, or, in the extreme, using different function names. Provide these variations of sorting in whatever way is most natural to your language.

Do not implement a sorting algorithm; this task is about the interface. If you can't use a built-in sort routine, just omit the implementation (with a comment).

Common Lisp

Common Lisp has both named and positional parameters.

<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 sort takes a "less than" predicate function. The 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>


Tcl

In Tcl, optional arguments are collected when the last argument to a proc definition is called "args". The lsort command has a similar API, but with different keywords, so we adapt them. <lang Tcl>package require Tcl 8.5

proc tablesort {table args} {

   array set opt {-ordering "" -column 0 -reverse 0}
   array set opt $args
   set pars [list -index $opt(-column)]
   if $opt(-reverse) {lappend pars -decreasing}
   if {$opt(-ordering) ne ""} {lappend pars -command $opt(-ordering)}
   lsort {*}$pars $table

}

% tablesort $data {"" q z} {a b c} {zap zip Zot}

% tablesort $data -column 1 {a b c} {"" q z} {zap zip Zot}

% tablesort $data -column 0 {"" q z} {a b c} {zap zip Zot}

% tablesort $data -column 0 -reverse 1 {zap zip Zot} {a b c} {"" q z}

% tablesort $data -ordering {

       apply {{a b} {expr {[string length $b]-[string length $a]}}}
   }

{zap zip Zot} {a b c} {"" q z} </lang>