Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Optional-parameters/00-META.yaml
Normal file
3
Task/Optional-parameters/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Optional_parameters
|
||||
note: Basic language learning
|
||||
22
Task/Optional-parameters/00-TASK.txt
Normal file
22
Task/Optional-parameters/00-TASK.txt
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
;Task:
|
||||
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'''. If the language supports both methods naturally, you are encouraged to describe both.
|
||||
|
||||
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).
|
||||
|
||||
See also:
|
||||
* [[Named Arguments]]
|
||||
<br><br>
|
||||
|
||||
35
Task/Optional-parameters/ALGOL-68/optional-parameters.alg
Normal file
35
Task/Optional-parameters/ALGOL-68/optional-parameters.alg
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
# as the options have distinct types (INT, BOOL and PROC( STRING, STRING )INT) the #
|
||||
# easiest way to support these optional parameters in Algol 68 would be to have an array #
|
||||
# with elements of these types #
|
||||
# See the Named Arguments sample for cases where the option types are not distinct #
|
||||
|
||||
# default comparison function #
|
||||
PROC default compare = ( STRING a, b )INT: IF a < b THEN -1 ELIF a = b THEN 0 ELSE 1 FI;
|
||||
|
||||
# sorting procedure #
|
||||
PROC configurable sort = ( [,]STRING data, []UNION( INT, BOOL, PROC( STRING, STRING )INT ) options )VOID:
|
||||
BEGIN
|
||||
# set initial values for the options #
|
||||
INT sort column := 2 LWB data;
|
||||
BOOL reverse sort := FALSE;
|
||||
PROC( STRING, STRING )INT comparator := default compare;
|
||||
# overide from the supplied options #
|
||||
FOR opt pos FROM LWB options TO UPB options DO
|
||||
CASE options[ opt pos ]
|
||||
IN ( PROC( STRING, STRING )INT p ): comparator := p
|
||||
, ( INT c ): sort column := c
|
||||
, ( BOOL r ): reverse sort := r
|
||||
ESAC
|
||||
OD
|
||||
# do the sort .... #
|
||||
END # configurable sort # ;
|
||||
|
||||
# example calls #
|
||||
[ 1 : 2, 1 : 3 ]STRING data := ( ( "a", "bb", "cde" ), ( "x", "abcdef", "Q" ) );
|
||||
|
||||
# sort data, default comparison, first column, reverse order #
|
||||
configurable sort( data, ( TRUE ) );
|
||||
# sort data, second column, ignore first chaacter when sorting, normal order #
|
||||
configurable sort( data, ( 2, ( STRING a, STRING b )INT: default compare( a[ LWB a + 1 : ], b[ LWB b + 1 : ] ) ) );
|
||||
# default sort #
|
||||
configurable sort( data, () )
|
||||
14
Task/Optional-parameters/Ada/optional-parameters-1.ada
Normal file
14
Task/Optional-parameters/Ada/optional-parameters-1.ada
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
package Tables is
|
||||
|
||||
type Table is private;
|
||||
|
||||
type Ordering is (Lexicographic, Psionic, ...); -- add others
|
||||
|
||||
procedure Sort (It : in out Table;
|
||||
Order_By : in Ordering := Lexicographic;
|
||||
Column : in Positive := 1;
|
||||
Reverse_Ordering : in Boolean := False);
|
||||
|
||||
private
|
||||
... -- implementation specific
|
||||
end Tables;
|
||||
10
Task/Optional-parameters/Ada/optional-parameters-2.ada
Normal file
10
Task/Optional-parameters/Ada/optional-parameters-2.ada
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
with Tables;
|
||||
procedure Table_Test is
|
||||
My_Table : Tables.Table;
|
||||
begin
|
||||
... -- insert stuff in table
|
||||
Sort (My_Table); -- use default sorting
|
||||
Sort (My_Table, Psionic, 5, True); -- use psionic sorting by 5th column in reverse order
|
||||
Sort (It => My_Table, Reverse_Ordering => True); -- use default sorting in reverse order
|
||||
... -- other stuff
|
||||
end Table_Test;
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
on sortTable(x)
|
||||
set {sortOrdering, sortColumn, sortReverse} to {sort_lexicographic, 1, false}
|
||||
try
|
||||
set sortOrdering to x's ordering
|
||||
end try
|
||||
try
|
||||
set sortColumn to x's column
|
||||
end try
|
||||
try
|
||||
set sortReverse to x's reverse
|
||||
end try
|
||||
return sortOrdering's sort(x's sequence, sortColumn, sortReverse)
|
||||
end sortTable
|
||||
|
||||
script sort_lexicographic
|
||||
on sort(table, column, reverse)
|
||||
-- Implement lexicographic Sorting process here.
|
||||
return table
|
||||
end sort
|
||||
end script
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
-- Another sort function.
|
||||
script sort_colex
|
||||
on sort(table, column, reverse)
|
||||
-- Implement colexicographic Sorting process here.
|
||||
return table
|
||||
end sort
|
||||
end script
|
||||
|
||||
-- Populate a table (list) with data.
|
||||
set table to {{1,2},{3,4}}
|
||||
|
||||
sortTable({sequence:table, ordering:sort_lexicographic, column:1, reverse:false})
|
||||
sortTable({sequence:table, ordering:sort_colex, column:2, reverse:true})
|
||||
sortTable({sequence:table, reverse:true})
|
||||
sortTable({sequence:table})
|
||||
33
Task/Optional-parameters/Arturo/optional-parameters.arturo
Normal file
33
Task/Optional-parameters/Arturo/optional-parameters.arturo
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
sortTable: function [tbl][
|
||||
column: "0"
|
||||
reversed?: false
|
||||
unless null? c: <= attr 'column -> column: to :string c
|
||||
unless null? attr 'reverse -> reversed?: true
|
||||
|
||||
result: new sort.by: column map tbl 'r [
|
||||
to :dictionary flatten couple 0..dec size r r
|
||||
]
|
||||
|
||||
if reversed? -> reverse 'result
|
||||
|
||||
return map result 'r -> values r
|
||||
]
|
||||
|
||||
printTable: function [tbl, title][
|
||||
print ["==" title]
|
||||
loop tbl 'row [
|
||||
print row
|
||||
]
|
||||
print ""
|
||||
]
|
||||
|
||||
lst: [
|
||||
["a", "b", "c"]
|
||||
["", "q", "z"]
|
||||
["zap", "zip", "Zot"]
|
||||
]
|
||||
|
||||
printTable sortTable lst "Default sort"
|
||||
printTable sortTable.column:1 lst "Sorting by column=1"
|
||||
printTable sortTable.reverse lst "Sorting, reversed"
|
||||
printTable sortTable.reverse.column:1 lst "Sorting by column=1, reversed"
|
||||
33
Task/Optional-parameters/AutoHotkey/optional-parameters.ahk
Normal file
33
Task/Optional-parameters/AutoHotkey/optional-parameters.ahk
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
Gosub start ; create and show the gui
|
||||
sort_table("Text", column := 2, reverse := 1) ; lexicographic sort
|
||||
Sleep, 2000
|
||||
sort_table("Integer", column := 2, reverse := 1) ; numerical sort
|
||||
Return
|
||||
|
||||
start:
|
||||
Gui, Add, ListView, r20 w200, 1|2|3
|
||||
data =
|
||||
(
|
||||
1,2,3
|
||||
b,q,z
|
||||
c,z,z
|
||||
)
|
||||
Loop, Parse, data, `n
|
||||
{
|
||||
StringSplit, row, A_LoopField, `,
|
||||
LV_Add(row, row1, row2, row3)
|
||||
}
|
||||
LV_ModifyCol(50) ; Auto-size columns
|
||||
Gui, Show
|
||||
Return
|
||||
|
||||
; The function supporting named, defaulted arguments
|
||||
sort_table(ordering = "Text", column = 0, reverse = 0)
|
||||
{
|
||||
If reverse
|
||||
desc = desc
|
||||
LV_ModifyCol(column, "sort" . desc . " " . ordering)
|
||||
}
|
||||
|
||||
GuiClose:
|
||||
ExitApp
|
||||
10
Task/Optional-parameters/BBC-BASIC/optional-parameters.basic
Normal file
10
Task/Optional-parameters/BBC-BASIC/optional-parameters.basic
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
DIM table$(100,100)
|
||||
PROCsort_default(table$())
|
||||
PROCsort_options(table$(), TRUE, 1, FALSE)
|
||||
END
|
||||
|
||||
DEF PROCsort_options(table$(), ordering%, column%, reverse%)
|
||||
DEF PROCsort_default(table$()) : LOCAL ordering%, column%, reverse%
|
||||
REM The sort goes here, controlled by the options
|
||||
REM Zero/FALSE values for the options shall select the defaults
|
||||
ENDPROC
|
||||
22
Task/Optional-parameters/Bracmat/optional-parameters.bracmat
Normal file
22
Task/Optional-parameters/Bracmat/optional-parameters.bracmat
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
( ( sortTable
|
||||
= table ordering column reverse
|
||||
. !arg
|
||||
: ( ?table
|
||||
. ( ? (ordering.?ordering) ?
|
||||
| ?&lexicographic:?ordering
|
||||
)
|
||||
: ( ? (column.?column) ?
|
||||
| ?&1:?column
|
||||
)
|
||||
: ( ? (reverse.?reverse) ?
|
||||
| ?&no:?reverse
|
||||
)
|
||||
)
|
||||
& (...)
|
||||
)
|
||||
& (12.Claes.left)
|
||||
(11.Otto.right)
|
||||
(8.Frederikke.middle)
|
||||
: ?table
|
||||
& sortTable$(!table.(column.2) (reverse.yes))
|
||||
);
|
||||
91
Task/Optional-parameters/C++/optional-parameters.cpp
Normal file
91
Task/Optional-parameters/C++/optional-parameters.cpp
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
// helper comparator that is passed to std::sort()
|
||||
template <class T>
|
||||
struct sort_table_functor {
|
||||
typedef bool (*CompFun)(const T &, const T &);
|
||||
const CompFun ordering;
|
||||
const int column;
|
||||
const bool reverse;
|
||||
sort_table_functor(CompFun o, int c, bool r) :
|
||||
ordering(o), column(c), reverse(r) { }
|
||||
bool operator()(const std::vector<T> &x, const std::vector<T> &y) const {
|
||||
const T &a = x[column],
|
||||
&b = y[column];
|
||||
return reverse ? ordering(b, a)
|
||||
: ordering(a, b);
|
||||
}
|
||||
};
|
||||
|
||||
// natural-order less-than comparator
|
||||
template <class T>
|
||||
bool myLess(const T &x, const T &y) { return x < y; }
|
||||
|
||||
// this is the function we call, which takes optional parameters
|
||||
template <class T>
|
||||
void sort_table(std::vector<std::vector<T> > &table,
|
||||
int column = 0, bool reverse = false,
|
||||
bool (*ordering)(const T &, const T &) = myLess) {
|
||||
std::sort(table.begin(), table.end(),
|
||||
sort_table_functor<T>(ordering, column, reverse));
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// helper function to print our 3x3 matrix
|
||||
template <class T>
|
||||
void print_matrix(std::vector<std::vector<T> > &data) {
|
||||
for () {
|
||||
for (int j = 0; j < 3; j++)
|
||||
std::cout << data[i][j] << "\t";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// order in descending length
|
||||
bool desc_len_comparator(const std::string &x, const std::string &y) {
|
||||
return x.length() > y.length();
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
std::string data_array[3][3] =
|
||||
{
|
||||
{"a", "b", "c"},
|
||||
{"", "q", "z"},
|
||||
{"zap", "zip", "Zot"}
|
||||
};
|
||||
|
||||
std::vector<std::vector<std::string> > data_orig;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
std::vector<std::string> row;
|
||||
for (int j = 0; j < 3; j++)
|
||||
row.push_back(data_array[i][j]);
|
||||
data_orig.push_back(row);
|
||||
}
|
||||
print_matrix(data_orig);
|
||||
|
||||
std::vector<std::vector<std::string> > data = data_orig;
|
||||
sort_table(data);
|
||||
print_matrix(data);
|
||||
|
||||
data = data_orig;
|
||||
sort_table(data, 2);
|
||||
print_matrix(data);
|
||||
|
||||
data = data_orig;
|
||||
sort_table(data, 1);
|
||||
print_matrix(data);
|
||||
|
||||
data = data_orig;
|
||||
sort_table(data, 1, true);
|
||||
print_matrix(data);
|
||||
|
||||
data = data_orig;
|
||||
sort_table(data, 0, false, desc_len_comparator);
|
||||
print_matrix(data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
167
Task/Optional-parameters/C/optional-parameters.c
Normal file
167
Task/Optional-parameters/C/optional-parameters.c
Normal file
|
|
@ -0,0 +1,167 @@
|
|||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef const char * String;
|
||||
typedef struct sTable {
|
||||
String * *rows;
|
||||
int n_rows,n_cols;
|
||||
} *Table;
|
||||
|
||||
typedef int (*CompareFctn)(String a, String b);
|
||||
|
||||
struct {
|
||||
CompareFctn compare;
|
||||
int column;
|
||||
int reversed;
|
||||
} sortSpec;
|
||||
|
||||
int CmprRows( const void *aa, const void *bb)
|
||||
{
|
||||
String *rA = *(String *const *)aa;
|
||||
String *rB = *(String *const *)bb;
|
||||
int sortCol = sortSpec.column;
|
||||
|
||||
String left = sortSpec.reversed ? rB[sortCol] : rA[sortCol];
|
||||
String right = sortSpec.reversed ? rA[sortCol] : rB[sortCol];
|
||||
return sortSpec.compare( left, right );
|
||||
}
|
||||
|
||||
/** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||
* tbl parameter is a table of rows of strings
|
||||
* argSpec is a string containing zero or more of the letters o,c,r
|
||||
* if o is present - the corresponding optional argument is a function which
|
||||
* determines the ordering of the strings.
|
||||
* if c is present - the corresponding optional argument is an integer that
|
||||
* specifies the column to sort on.
|
||||
* if r is present - the corresponding optional argument is either
|
||||
* true(nonzero) or false(zero) and if true, the sort will b in reverse order
|
||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||
int sortTable(Table tbl, const char* argSpec,... )
|
||||
{
|
||||
va_list vl;
|
||||
const char *p;
|
||||
int c;
|
||||
sortSpec.compare = &strcmp;
|
||||
sortSpec.column = 0;
|
||||
sortSpec.reversed = 0;
|
||||
|
||||
va_start(vl, argSpec);
|
||||
if (argSpec)
|
||||
for (p=argSpec; *p; p++) {
|
||||
switch (*p) {
|
||||
case 'o':
|
||||
sortSpec.compare = va_arg(vl,CompareFctn);
|
||||
break;
|
||||
case 'c':
|
||||
c = va_arg(vl,int);
|
||||
if ( 0<=c && c<tbl->n_cols)
|
||||
sortSpec.column = c;
|
||||
break;
|
||||
case 'r':
|
||||
sortSpec.reversed = (0!=va_arg(vl,int));
|
||||
break;
|
||||
}
|
||||
}
|
||||
va_end(vl);
|
||||
qsort( tbl->rows, tbl->n_rows, sizeof(String *), CmprRows);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void printTable( Table tbl, FILE *fout, const char *colFmts[])
|
||||
{
|
||||
int row, col;
|
||||
|
||||
for (row=0; row<tbl->n_rows; row++) {
|
||||
fprintf(fout, " ");
|
||||
for(col=0; col<tbl->n_cols; col++) {
|
||||
fprintf(fout, colFmts[col], tbl->rows[row][col]);
|
||||
}
|
||||
fprintf(fout, "\n");
|
||||
}
|
||||
fprintf(fout, "\n");
|
||||
}
|
||||
|
||||
int ord(char v)
|
||||
{
|
||||
return v-'0';
|
||||
}
|
||||
|
||||
/* an alternative comparison function */
|
||||
int cmprStrgs(String s1, String s2)
|
||||
{
|
||||
const char *p1 = s1;
|
||||
const char *p2 = s2;
|
||||
const char *mrk1, *mrk2;
|
||||
while ((tolower(*p1) == tolower(*p2)) && *p1) {
|
||||
p1++; p2++;
|
||||
}
|
||||
if (isdigit(*p1) && isdigit(*p2)) {
|
||||
long v1, v2;
|
||||
if ((*p1 == '0') ||(*p2 == '0')) {
|
||||
while (p1 > s1) {
|
||||
p1--; p2--;
|
||||
if (*p1 != '0') break;
|
||||
}
|
||||
if (!isdigit(*p1)) {
|
||||
p1++; p2++;
|
||||
}
|
||||
}
|
||||
mrk1 = p1; mrk2 = p2;
|
||||
v1 = 0;
|
||||
while(isdigit(*p1)) {
|
||||
v1 = 10*v1+ord(*p1);
|
||||
p1++;
|
||||
}
|
||||
v2 = 0;
|
||||
while(isdigit(*p2)) {
|
||||
v2 = 10*v2+ord(*p2);
|
||||
p2++;
|
||||
}
|
||||
if (v1 == v2)
|
||||
return(p2-mrk2)-(p1-mrk1);
|
||||
return v1 - v2;
|
||||
}
|
||||
if (tolower(*p1) != tolower(*p2))
|
||||
return (tolower(*p1) - tolower(*p2));
|
||||
for(p1=s1, p2=s2; (*p1 == *p2) && *p1; p1++, p2++);
|
||||
return (*p1 -*p2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
const char *colFmts[] = {" %-5.5s"," %-5.5s"," %-9.9s"};
|
||||
String r1[] = { "a101", "red", "Java" };
|
||||
String r2[] = { "ab40", "gren", "Smalltalk" };
|
||||
String r3[] = { "ab9", "blue", "Fortran" };
|
||||
String r4[] = { "ab09", "ylow", "Python" };
|
||||
String r5[] = { "ab1a", "blak", "Factor" };
|
||||
String r6[] = { "ab1b", "brwn", "C Sharp" };
|
||||
String r7[] = { "Ab1b", "pink", "Ruby" };
|
||||
String r8[] = { "ab1", "orng", "Scheme" };
|
||||
|
||||
String *rows[] = { r1, r2, r3, r4, r5, r6, r7, r8 };
|
||||
struct sTable table;
|
||||
table.rows = rows;
|
||||
table.n_rows = 8;
|
||||
table.n_cols = 3;
|
||||
|
||||
sortTable(&table, "");
|
||||
printf("sort on col 0, ascending\n");
|
||||
printTable(&table, stdout, colFmts);
|
||||
|
||||
sortTable(&table, "ro", 1, &cmprStrgs);
|
||||
printf("sort on col 0, reverse.special\n");
|
||||
printTable(&table, stdout, colFmts);
|
||||
|
||||
sortTable(&table, "c", 1);
|
||||
printf("sort on col 1, ascending\n");
|
||||
printTable(&table, stdout, colFmts);
|
||||
|
||||
sortTable(&table, "cr", 2, 1);
|
||||
printf("sort on col 2, reverse\n");
|
||||
printTable(&table, stdout, colFmts);
|
||||
return 0;
|
||||
}
|
||||
6
Task/Optional-parameters/Clojure/optional-parameters.clj
Normal file
6
Task/Optional-parameters/Clojure/optional-parameters.clj
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(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
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
(defun sort-table (table &key (ordering #'string<)
|
||||
(column 0)
|
||||
reverse)
|
||||
(sort table (if reverse
|
||||
(complement ordering)
|
||||
ordering)
|
||||
:key (lambda (row) (elt row column))))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
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"))
|
||||
28
Task/Optional-parameters/D/optional-parameters.d
Normal file
28
Task/Optional-parameters/D/optional-parameters.d
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import std.stdio, std.algorithm, std.functional;
|
||||
|
||||
string[][] sortTable(string[][] table,
|
||||
in bool function(string[],string[]) ordering=null,
|
||||
in int column = 0,
|
||||
in bool reverse = false) {
|
||||
if (ordering is null)
|
||||
table.schwartzSort!(row => row[column])();
|
||||
else
|
||||
table.sort!ordering();
|
||||
if (reverse)
|
||||
table.reverse();
|
||||
return table;
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto data = [["a", "b", "c"],
|
||||
["", "q", "z"],
|
||||
["zap", "zip", "Zot"]];
|
||||
|
||||
alias show = curry!(writefln, "%-(%s\n%)\n");
|
||||
show(data);
|
||||
show(sortTable(data));
|
||||
show(sortTable(data, null, 2));
|
||||
show(sortTable(data, null, 1));
|
||||
show(sortTable(data, null, 1, true));
|
||||
show(sortTable(data, (a,b) => b.length > a.length));
|
||||
}
|
||||
166
Task/Optional-parameters/Delphi/optional-parameters.delphi
Normal file
166
Task/Optional-parameters/Delphi/optional-parameters.delphi
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
program Optional_parameters;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.SysUtils;
|
||||
|
||||
type
|
||||
TRow = TArray<string>;
|
||||
|
||||
TOrderingFun = TFunc<TRow, TRow, Boolean>;
|
||||
|
||||
TTable = array of TRow;
|
||||
|
||||
TRowHelper = record helper for TRow
|
||||
public
|
||||
procedure Swap(var other: TRow);
|
||||
function ToString: string;
|
||||
function Length: Integer;
|
||||
end;
|
||||
|
||||
TTableHelper = record helper for TTable
|
||||
private
|
||||
procedure ExchangeRow(i, j: Integer);
|
||||
public
|
||||
procedure Sort(OrderingFun: TOrderingFun);
|
||||
procedure Reverse;
|
||||
function ToString: string;
|
||||
end;
|
||||
|
||||
function Max(a, b: Integer): Integer;
|
||||
begin
|
||||
if a > b then
|
||||
exit(a);
|
||||
Result := b;
|
||||
end;
|
||||
|
||||
{ TRowHelper }
|
||||
|
||||
function TRowHelper.Length: Integer;
|
||||
begin
|
||||
Result := System.Length(self);
|
||||
end;
|
||||
|
||||
procedure TRowHelper.Swap(var other: TRow);
|
||||
var
|
||||
aLengthOther, aLengthSelf, aLength: Integer;
|
||||
tmp: string;
|
||||
i: Integer;
|
||||
begin
|
||||
aLengthOther := other.Length;
|
||||
aLengthSelf := self.Length;
|
||||
aLength := max(aLengthOther, aLengthSelf);
|
||||
if aLength = 0 then
|
||||
exit;
|
||||
|
||||
SetLength(self, aLength);
|
||||
SetLength(other, aLength);
|
||||
|
||||
for i := 0 to aLength - 1 do
|
||||
begin
|
||||
tmp := self[i];
|
||||
self[i] := other[i];
|
||||
other[i] := tmp;
|
||||
end;
|
||||
|
||||
SetLength(self, aLengthOther);
|
||||
SetLength(other, aLengthSelf);
|
||||
end;
|
||||
|
||||
function TRowHelper.ToString: string;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
Result := '[';
|
||||
for i := 0 to High(self) do
|
||||
begin
|
||||
if i > 0 then
|
||||
Result := Result + ', ';
|
||||
Result := Result + '"' + self[i] + '"';
|
||||
end;
|
||||
Result := Result + ']';
|
||||
end;
|
||||
|
||||
{ TTableHelper }
|
||||
|
||||
procedure TTableHelper.ExchangeRow(i, j: Integer);
|
||||
begin
|
||||
Self[i].Swap(self[j]);
|
||||
end;
|
||||
|
||||
procedure TTableHelper.reverse;
|
||||
var
|
||||
aLength, aHalfLength: Integer;
|
||||
i: Integer;
|
||||
begin
|
||||
aLength := Length(self);
|
||||
aHalfLength := aLength div 2;
|
||||
for i := 0 to aHalfLength - 1 do
|
||||
ExchangeRow(i, aLength - i - 1);
|
||||
end;
|
||||
|
||||
procedure TTableHelper.Sort(OrderingFun: TOrderingFun);
|
||||
var
|
||||
i, j, aLength: Integer;
|
||||
begin
|
||||
if not Assigned(OrderingFun) then
|
||||
exit;
|
||||
|
||||
aLength := Length(self);
|
||||
for i := 0 to aLength - 2 do
|
||||
for j := i + 1 to aLength - 1 do
|
||||
if OrderingFun(self[i], self[j]) then
|
||||
ExchangeRow(i, j);
|
||||
end;
|
||||
|
||||
function TTableHelper.ToString: string;
|
||||
var
|
||||
i: Integer;
|
||||
begin
|
||||
Result := '[';
|
||||
for i := 0 to High(self) do
|
||||
begin
|
||||
if i > 0 then
|
||||
Result := Result + #10;
|
||||
Result := Result + self[i].ToString;
|
||||
end;
|
||||
Result := Result + ']';
|
||||
end;
|
||||
|
||||
function SortTable(table: TTable; Ordering: TOrderingFun = nil; column: Integer
|
||||
= 0; reverse: Boolean = false): TTable;
|
||||
var
|
||||
acolumn: Integer;
|
||||
begin
|
||||
acolumn := column;
|
||||
if not Assigned(Ordering) then
|
||||
Ordering :=
|
||||
function(left, right: TRow): Boolean
|
||||
begin
|
||||
Result := left[acolumn] > right[acolumn];
|
||||
end;
|
||||
|
||||
table.Sort(Ordering);
|
||||
if (reverse) then
|
||||
table.reverse();
|
||||
Result := table;
|
||||
end;
|
||||
|
||||
var
|
||||
data: TTable = [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']];
|
||||
|
||||
begin
|
||||
Writeln(data.ToString, #10);
|
||||
Writeln(SortTable(data).ToString, #10);
|
||||
Writeln(SortTable(data).ToString, #10);
|
||||
Writeln(SortTable(data, nil, 2).ToString, #10);
|
||||
Writeln(SortTable(data, nil, 1).ToString, #10);
|
||||
Writeln(SortTable(data, nil, 1, True).ToString, #10);
|
||||
Writeln(SortTable(data,
|
||||
function(left, right: TRow): Boolean
|
||||
begin
|
||||
Result := left.Length > right.Length;
|
||||
end).ToString, #10);
|
||||
Readln;
|
||||
end.
|
||||
22
Task/Optional-parameters/E/optional-parameters.e
Normal file
22
Task/Optional-parameters/E/optional-parameters.e
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
def defaultOrdering(a, b) { return a.op__cmp(b) }
|
||||
|
||||
def sort {
|
||||
|
||||
to run(table) {
|
||||
return sort(table, 0, false, defaultOrdering)
|
||||
}
|
||||
to run(table, column) {
|
||||
return sort(table, column, false, defaultOrdering)
|
||||
}
|
||||
to run(table, column, reverse) {
|
||||
return sort(table, column, reverse, defaultOrdering)
|
||||
}
|
||||
|
||||
to run(table :List[List[String]], column :int, reverse :boolean, ordering) {
|
||||
return table.sort(fn a, b {
|
||||
def ord := ordering(a[column], b[column])
|
||||
if (reverse) { -ord } else { ord }
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
46
Task/Optional-parameters/Ecstasy/optional-parameters.ecstasy
Normal file
46
Task/Optional-parameters/Ecstasy/optional-parameters.ecstasy
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
module OptionalParameters {
|
||||
typedef Type<String >.Orderer as ColumnOrderer;
|
||||
typedef Type<String[]>.Orderer as RowOrderer;
|
||||
|
||||
static String[][] sort(String[][] table,
|
||||
ColumnOrderer? orderer = Null,
|
||||
Int column = 0,
|
||||
Boolean reverse = False,
|
||||
) {
|
||||
// provide a default orderer
|
||||
orderer ?:= (s1, s2) -> s1 <=> s2;
|
||||
|
||||
// optionally reverse the order
|
||||
ColumnOrderer byString = reverse
|
||||
? ((s1, s2) -> orderer(s1, s2).reversed)
|
||||
: orderer;
|
||||
|
||||
// sort the indicated column
|
||||
RowOrderer byColumn = (row1, row2) -> byString(row1[column], row2[column]);
|
||||
|
||||
return table.sorted(byColumn);
|
||||
}
|
||||
|
||||
void run() {
|
||||
String[][] table =
|
||||
[
|
||||
["c", "x", "i"],
|
||||
["a", "y", "p"],
|
||||
["b", "z", "a"],
|
||||
];
|
||||
|
||||
show("original input", table);
|
||||
show("by default sort on column 0", sort(table));
|
||||
show("by column 2", sort(table, column=2));
|
||||
show("by column 2 reversed", sort(table, column=2, reverse=True));
|
||||
}
|
||||
|
||||
void show(String title, String[][] table) {
|
||||
@Inject Console console;
|
||||
console.print($"{title}:");
|
||||
for (val row : table) {
|
||||
console.print($" {row}");
|
||||
}
|
||||
console.print();
|
||||
}
|
||||
}
|
||||
31
Task/Optional-parameters/Elixir/optional-parameters.elixir
Normal file
31
Task/Optional-parameters/Elixir/optional-parameters.elixir
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
defmodule Optional_parameters do
|
||||
def sort( table, options\\[] ) do
|
||||
options = options ++ [ ordering: :lexicographic, column: 0, reverse: false ]
|
||||
ordering = options[ :ordering ]
|
||||
column = options[ :column ]
|
||||
reverse = options[ :reverse ]
|
||||
sorted = sort( table, ordering, column )
|
||||
if reverse, do: Enum.reverse( sorted ), else: sorted
|
||||
end
|
||||
|
||||
defp sort( table, :lexicographic, column ) do
|
||||
Enum.sort_by( table, &elem( &1, column ) )
|
||||
end
|
||||
defp sort( table, :numeric, column ) do
|
||||
Enum.sort_by( table, &elem( &1, column ) |> String.to_integer )
|
||||
end
|
||||
|
||||
def task do
|
||||
table = [ { "123", "456", "0789" },
|
||||
{ "456", "0789", "123" },
|
||||
{ "0789", "123", "456" } ]
|
||||
IO.write "sort defaults "; IO.inspect sort( table )
|
||||
IO.write " & reverse "; IO.inspect sort( table, reverse: true )
|
||||
IO.write "sort column 2 "; IO.inspect sort( table, column: 2)
|
||||
IO.write " & reverse "; IO.inspect sort( table, column: 2, reverse: true)
|
||||
IO.write "sort numeric "; IO.inspect sort( table, ordering: :numeric)
|
||||
IO.write " & reverse "; IO.inspect sort( table, ordering: :numeric, reverse: true)
|
||||
end
|
||||
end
|
||||
|
||||
Optional_parameters.task
|
||||
39
Task/Optional-parameters/Erlang/optional-parameters.erl
Normal file
39
Task/Optional-parameters/Erlang/optional-parameters.erl
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
-module( optional_parameters ).
|
||||
|
||||
-export( [sort/2, task/0] ).
|
||||
|
||||
sort( Table, Options ) ->
|
||||
Ordering = proplists:get_value( ordering, Options, lexicographic ),
|
||||
Column = proplists:get_value( column, Options, 1 ),
|
||||
Is_reverse = proplists:get_value( reverse, Options, false ),
|
||||
Sorted = sort( Table, Ordering, Column ),
|
||||
sorted_reverse( Is_reverse, Sorted ).
|
||||
|
||||
task() ->
|
||||
io:fwrite( "sort defaults ~p~n", [sort( table(), [])] ),
|
||||
io:fwrite( "reverse ~p~n", [sort( table(), [reverse])] ),
|
||||
io:fwrite( "sort column 3 ~p~n", [sort( table(), [{column, 3}])] ),
|
||||
io:fwrite( "reverse ~p~n", [sort( table(), [{column, 3}, reverse])] ),
|
||||
io:fwrite( "sort numeric ~p~n", [sort( table(), [{ordering, numeric}])] ),
|
||||
io:fwrite( "reverse ~p~n", [sort( table(), [{ordering, numeric}, reverse])] ).
|
||||
|
||||
|
||||
|
||||
row_numeric( Tuple ) -> erlang:list_to_tuple( [{erlang:list_to_integer(X), X} || X <- erlang:tuple_to_list(Tuple)] ).
|
||||
|
||||
row_remove_numeric( Tuple ) -> erlang:list_to_tuple( [Y || {_X, Y} <- erlang:tuple_to_list(Tuple)] ).
|
||||
|
||||
sort( Table, lexicographic, Column ) -> lists:keysort( Column, Table );
|
||||
sort( Table, numeric, Column ) ->
|
||||
Numeric_table = [row_numeric(X) || X <- Table],
|
||||
Sorted_numeric = lists:keysort( Column, Numeric_table ),
|
||||
[row_remove_numeric(X) || X <- Sorted_numeric].
|
||||
|
||||
sorted_reverse( true, Sorted ) -> lists:reverse( Sorted );
|
||||
sorted_reverse( false, Sorted ) -> Sorted.
|
||||
|
||||
table() -> [table_row1(), table_row2(), table_row3()].
|
||||
|
||||
table_row1() -> {"123", "456", "0789"}.
|
||||
table_row2() -> {"456", "0789", "123"}.
|
||||
table_row3() -> {"0789", "123", "456"}.
|
||||
37
Task/Optional-parameters/F-Sharp/optional-parameters.fs
Normal file
37
Task/Optional-parameters/F-Sharp/optional-parameters.fs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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()
|
||||
58
Task/Optional-parameters/Factor/optional-parameters.factor
Normal file
58
Task/Optional-parameters/Factor/optional-parameters.factor
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
USING: accessors combinators io kernel math.order prettyprint
|
||||
sequences sorting ;
|
||||
|
||||
TUPLE: table-sorter
|
||||
data
|
||||
{ column initial: 0 }
|
||||
reversed?
|
||||
{ ordering initial: [ ] } ;
|
||||
|
||||
: <table-sorter> ( -- obj ) table-sorter new ;
|
||||
|
||||
: sort-table ( table-sorter -- matrix )
|
||||
{
|
||||
[ data>> ]
|
||||
[ column>> [ swap nth ] curry ]
|
||||
[ ordering>> compose ]
|
||||
[ reversed?>> [ >=< ] [ <=> ] ? [ bi@ ] prepose curry ]
|
||||
} cleave [ sort ] curry call( x -- x ) ;
|
||||
|
||||
|
||||
! ===== Now we can use the interface defined above =====
|
||||
|
||||
CONSTANT: table
|
||||
{ { "a" "b" "c" } { "" "q" "z" } { "can" "z" "a" } }
|
||||
|
||||
"Unsorted" print
|
||||
table simple-table.
|
||||
|
||||
"Default sort" print
|
||||
<table-sorter>
|
||||
table >>data
|
||||
sort-table simple-table.
|
||||
|
||||
"Sorted by col 2" print
|
||||
<table-sorter>
|
||||
table >>data
|
||||
2 >>column
|
||||
sort-table simple-table.
|
||||
|
||||
"Sorted by col 1" print
|
||||
<table-sorter>
|
||||
table >>data
|
||||
1 >>column
|
||||
sort-table simple-table.
|
||||
|
||||
"Reverse sorted by col 1" print
|
||||
<table-sorter>
|
||||
table >>data
|
||||
1 >>column
|
||||
t >>reversed?
|
||||
sort-table simple-table.
|
||||
|
||||
"Sorted by decreasing length" print
|
||||
<table-sorter>
|
||||
table >>data
|
||||
t >>reversed?
|
||||
[ length ] >>ordering
|
||||
sort-table simple-table.
|
||||
69
Task/Optional-parameters/Fortran/optional-parameters-1.f
Normal file
69
Task/Optional-parameters/Fortran/optional-parameters-1.f
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
module ExampleOptionalParameter
|
||||
! use any module needed for the sort function(s)
|
||||
! and all the interfaces needed to make the code work
|
||||
implicit none
|
||||
contains
|
||||
|
||||
subroutine sort_table(table, ordering, column, reverse)
|
||||
type(table_type), intent(inout) :: table
|
||||
integer, optional :: column
|
||||
logical, optional :: reverse
|
||||
optional :: ordering
|
||||
interface
|
||||
integer function ordering(a, b)
|
||||
type(table_element), intent(in) :: a, b
|
||||
end function ordering
|
||||
end interface
|
||||
|
||||
integer :: the_column, i
|
||||
logical :: reversing
|
||||
type(table_row) :: rowA, rowB
|
||||
|
||||
if ( present(column) ) then
|
||||
if ( column > get_num_of_columns(table) ) then
|
||||
! raise an error?
|
||||
else
|
||||
the_column = column
|
||||
end if
|
||||
else
|
||||
the_column = 1 ! a default value, de facto
|
||||
end if
|
||||
|
||||
reversing = .false. ! default value
|
||||
if ( present(reverse) ) reversing = reverse
|
||||
|
||||
do
|
||||
! loops over the rows to sort... at some point, we need
|
||||
! comparing an element (cell) of the row, with the element
|
||||
! in another row; ... let us suppose rowA and rowB are
|
||||
! the two rows we are considering
|
||||
ea = get_element(rowA, the_column)
|
||||
eb = get_element(rowB, the_column)
|
||||
if ( present(ordering) ) then
|
||||
if ( .not. reversing ) then
|
||||
if ( ordering(ea, eb) > 0 ) then
|
||||
! swap the rowA with the rowB
|
||||
end if
|
||||
else ! < instead of >
|
||||
if ( ordering(ea, eb) < 0 ) then
|
||||
! swap the rowA with the rowB
|
||||
end if
|
||||
end if
|
||||
else
|
||||
if ( .not. reversing ) then
|
||||
if ( lexinternal(ea, eb) > 0 ) then
|
||||
! swap the rowA with the rowB
|
||||
end if
|
||||
else ! < instead of >
|
||||
if ( lexinternal(ea, eb) < 0 ) then
|
||||
! swap the rowA with the rowB
|
||||
end if
|
||||
end if
|
||||
end if
|
||||
! ... more of the sorting algo ...
|
||||
! ... and rows traversing ... (and an exit condition of course!)
|
||||
end do
|
||||
|
||||
end subroutine sort_table
|
||||
|
||||
end module ExampleOptionalParameter
|
||||
36
Task/Optional-parameters/Fortran/optional-parameters-2.f
Normal file
36
Task/Optional-parameters/Fortran/optional-parameters-2.f
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
program UsingTest
|
||||
use ExampleOptionalParameter
|
||||
implicit none
|
||||
|
||||
type(table_type) :: table
|
||||
|
||||
! create the table...
|
||||
|
||||
! sorting taking from column 1, not reversed, using internal
|
||||
! default comparator
|
||||
call sort_table(table)
|
||||
|
||||
! the same as above, but in reversed order; we MUST specify
|
||||
! the name of the argument since it is not given in the same
|
||||
! order of the subroutine spec
|
||||
call sort_table(table, reverse=.true.)
|
||||
|
||||
! sort the table using a custom comparator
|
||||
call sort_table(table, my_cmp)
|
||||
! or
|
||||
call sort_table(table, ordering=my_cmp)
|
||||
|
||||
! as above, but taking from column 2
|
||||
call sort_table(table, my_cmp, 2)
|
||||
! or (swapping the order of args for fun)
|
||||
call sort_table(table, column=2, ordering=my_cmp)
|
||||
|
||||
! with custom comparator, column 2 and reversing...
|
||||
call sort_table(table, my_cmp, 2, .true.)
|
||||
! of course we can swap the order of optional args
|
||||
! by prefixing them with the name of the arg
|
||||
|
||||
! sort from column 2, with internal comparator
|
||||
call sort_table(table, column=2)
|
||||
|
||||
end program UsingTest
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Function power(n As Integer, p As Integer = 2) As Double
|
||||
Return n ^ p
|
||||
End Function
|
||||
|
||||
Print power(2) ' muestra 4
|
||||
Print power(2, 3) ' muestra 8
|
||||
Sleep
|
||||
20
Task/Optional-parameters/Go/optional-parameters-1.go
Normal file
20
Task/Optional-parameters/Go/optional-parameters-1.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
type cell string
|
||||
|
||||
type spec struct {
|
||||
less func(cell, cell) bool
|
||||
column int
|
||||
reverse bool
|
||||
}
|
||||
|
||||
func newSpec() (s spec) {
|
||||
// initialize any defaults
|
||||
return
|
||||
}
|
||||
|
||||
// sort with all defaults
|
||||
t.sort(newSpec())
|
||||
|
||||
// reverse sort
|
||||
s := newSpec
|
||||
s.reverse = true
|
||||
t.sort(s)
|
||||
5
Task/Optional-parameters/Go/optional-parameters-2.go
Normal file
5
Task/Optional-parameters/Go/optional-parameters-2.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
type spec struct {
|
||||
ordering func(cell, cell) bool
|
||||
column int
|
||||
reverse bool
|
||||
}
|
||||
1
Task/Optional-parameters/Go/optional-parameters-3.go
Normal file
1
Task/Optional-parameters/Go/optional-parameters-3.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
spec{reverse: true}
|
||||
93
Task/Optional-parameters/Go/optional-parameters-4.go
Normal file
93
Task/Optional-parameters/Go/optional-parameters-4.go
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type cell string
|
||||
type row []cell
|
||||
type table struct {
|
||||
rows []row
|
||||
column int
|
||||
less func(cell, cell) bool
|
||||
}
|
||||
|
||||
func (c cell) String() string {
|
||||
return fmt.Sprintf("%q", string(c))
|
||||
}
|
||||
|
||||
func (t table) printRows(heading string) {
|
||||
fmt.Println("--", heading)
|
||||
for _, row := range t.rows {
|
||||
fmt.Println(row)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// sort.Interface
|
||||
func (t table) Len() int { return len(t.rows) }
|
||||
func (t table) Swap(i, j int) { t.rows[i], t.rows[j] = t.rows[j], t.rows[i] }
|
||||
func (t table) Less(i, j int) bool {
|
||||
return t.less(t.rows[i][t.column], t.rows[j][t.column])
|
||||
}
|
||||
|
||||
// struct implements named parameter-like capability
|
||||
type spec struct {
|
||||
ordering func(cell, cell) bool
|
||||
column int
|
||||
reverse bool
|
||||
}
|
||||
|
||||
|
||||
func (t *table) sort(s spec) {
|
||||
// set up column and comparison function for sort
|
||||
t.column = s.column
|
||||
switch {
|
||||
case s.ordering != nil:
|
||||
t.less = s.ordering
|
||||
case s.reverse:
|
||||
t.less = func(a, b cell) bool { return a > b }
|
||||
default:
|
||||
t.less = func(a, b cell) bool { return a < b }
|
||||
}
|
||||
|
||||
// sort
|
||||
sort.Sort(t)
|
||||
|
||||
// reverse if necessary
|
||||
if s.ordering == nil || !s.reverse {
|
||||
return
|
||||
}
|
||||
last := len(t.rows) - 1
|
||||
for i := last / 2; i >= 0; i-- {
|
||||
t.rows[i], t.rows[last-i] = t.rows[last-i], t.rows[i]
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := table{rows: []row{
|
||||
{"pail", "food"},
|
||||
{"pillbox", "nurse maids"},
|
||||
{"suitcase", "airedales"},
|
||||
{"bathtub", "chocolate"},
|
||||
{"schooner", "ice cream sodas"},
|
||||
}}
|
||||
|
||||
t.printRows("song")
|
||||
|
||||
// no "parameters"
|
||||
t.sort(spec{})
|
||||
t.printRows("sorted on first column")
|
||||
|
||||
// "named parameter" reverse.
|
||||
t.sort(spec{reverse: true})
|
||||
t.printRows("reverse sorted on first column")
|
||||
|
||||
// "named parameters" column and ordering
|
||||
t.sort(spec{
|
||||
column: 1,
|
||||
ordering: func(a, b cell) bool { return len(a) > len(b) },
|
||||
})
|
||||
t.printRows("sorted by descending string length on second column")
|
||||
}
|
||||
112
Task/Optional-parameters/Go/optional-parameters-5.go
Normal file
112
Task/Optional-parameters/Go/optional-parameters-5.go
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type cell string
|
||||
type row []cell
|
||||
type table struct {
|
||||
rows []row
|
||||
column int
|
||||
less func(cell, cell) bool
|
||||
}
|
||||
|
||||
func (c cell) String() string {
|
||||
return fmt.Sprintf("%q", string(c))
|
||||
}
|
||||
|
||||
func (t table) printRows(heading string) {
|
||||
fmt.Println("--", heading)
|
||||
for _, row := range t.rows {
|
||||
fmt.Println(row)
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
// sort.Interface
|
||||
func (t table) Len() int { return len(t.rows) }
|
||||
func (t table) Swap(i, j int) { t.rows[i], t.rows[j] = t.rows[j], t.rows[i] }
|
||||
func (t table) Less(i, j int) bool {
|
||||
return t.less(t.rows[i][t.column], t.rows[j][t.column])
|
||||
}
|
||||
|
||||
// struct implements named parameter-like capability
|
||||
type spec struct {
|
||||
ordering func(cell, cell) bool
|
||||
column int
|
||||
reverse bool
|
||||
}
|
||||
|
||||
// A defined option type is not really needed by the technique, but has
|
||||
// a nice advantage for documentation. If this type is exported, then
|
||||
// the the Go documentation tool go doc will organize all of the option
|
||||
// functions together under the type. (Go doc will see them as constructors
|
||||
// for the type.)
|
||||
type Option func(*spec)
|
||||
|
||||
func ordering(o func(cell, cell) bool) Option {
|
||||
return func(s *spec) { s.ordering = o }
|
||||
}
|
||||
|
||||
func column(c int) Option {
|
||||
return func(s *spec) { s.column = c }
|
||||
}
|
||||
|
||||
func reverse() Option {
|
||||
return func(s *spec) { s.reverse = true }
|
||||
}
|
||||
|
||||
func (t *table) sort(options ...Option) {
|
||||
var s spec
|
||||
for _, o := range options {
|
||||
o(&s)
|
||||
}
|
||||
// set up column and comparison function for sort
|
||||
t.column = s.column
|
||||
switch {
|
||||
case s.ordering != nil:
|
||||
t.less = s.ordering
|
||||
case s.reverse:
|
||||
t.less = func(a, b cell) bool { return a > b }
|
||||
default:
|
||||
t.less = func(a, b cell) bool { return a < b }
|
||||
}
|
||||
|
||||
// sort
|
||||
sort.Sort(t)
|
||||
|
||||
// reverse if necessary
|
||||
if s.ordering == nil || !s.reverse {
|
||||
return
|
||||
}
|
||||
last := len(t.rows) - 1
|
||||
for i := last / 2; i >= 0; i-- {
|
||||
t.rows[i], t.rows[last-i] = t.rows[last-i], t.rows[i]
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
t := table{rows: []row{
|
||||
{"pail", "food"},
|
||||
{"pillbox", "nurse maids"},
|
||||
{"suitcase", "airedales"},
|
||||
{"bathtub", "chocolate"},
|
||||
{"schooner", "ice cream sodas"},
|
||||
}}
|
||||
|
||||
t.printRows("song")
|
||||
// no parameters
|
||||
t.sort()
|
||||
t.printRows("sorted on first column")
|
||||
|
||||
// "named parameter" reverse.
|
||||
t.sort(reverse())
|
||||
t.printRows("reverse sorted on first column")
|
||||
|
||||
// "named parameters" column and ordering
|
||||
byLen := func(a, b cell) bool { return len(a) > len(b) }
|
||||
t.sort(column(1), ordering(byLen))
|
||||
t.printRows("sorted by descending string length on second column")
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
def orderedSort(Collection table, column = 0, reverse = false, ordering = {x, y -> x <=> y } as Comparator) {
|
||||
table.sort(false) { x, y -> (reverse ? -1 : 1) * ordering.compare(x[column], y[column])}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def table = [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']]
|
||||
|
||||
assert orderedSort(table) == [['', 'q', 'z'], ['a', 'b', 'c'], ['zap', 'zip', 'Zot']]
|
||||
assert orderedSort(table, 2) == [['zap', 'zip', 'Zot'], ['a', 'b', 'c'], ['', 'q', 'z']]
|
||||
assert orderedSort(table, 1) == [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']]
|
||||
assert orderedSort(table, 1, true) == [['zap', 'zip', 'Zot'],['', 'q', 'z'],['a', 'b', 'c']]
|
||||
assert orderedSort(table, 0, false, {x, y -> y?.size() <=> x?.size()} as Comparator) == [['zap', 'zip', 'Zot'],['a', 'b', 'c'],['', 'q', 'z']]
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Collection.metaClass.orderedSort = { params ->
|
||||
def column = params?.column ?: 0
|
||||
def reverse = params?.reverse ?: false
|
||||
def ordering = params?.ordering ?: {x, y -> x <=> y } as Comparator
|
||||
|
||||
table.sort(false) { x, y -> (reverse ? -1 : 1) * ordering.compare(x[column], y[column])}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def table = [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']]
|
||||
|
||||
assert table.orderedSort() == [['', 'q', 'z'], ['a', 'b', 'c'], ['zap', 'zip', 'Zot']]
|
||||
assert table.orderedSort(column: 2) == [['zap', 'zip', 'Zot'], ['a', 'b', 'c'], ['', 'q', 'z']]
|
||||
assert table.orderedSort(column: 1) == [['a', 'b', 'c'], ['', 'q', 'z'], ['zap', 'zip', 'Zot']]
|
||||
assert table.orderedSort(column: 1, reverse: true) == [['zap', 'zip', 'Zot'],['', 'q', 'z'],['a', 'b', 'c']]
|
||||
assert table.orderedSort(ordering: {x, y -> y?.size() <=> x?.size()} as Comparator) == [['zap', 'zip', 'Zot'],['a', 'b', 'c'],['', 'q', 'z']]
|
||||
15
Task/Optional-parameters/Haskell/optional-parameters-1.hs
Normal file
15
Task/Optional-parameters/Haskell/optional-parameters-1.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
{-# LANGUAGE RecordWildCards #-}
|
||||
|
||||
data SorterArgs = SorterArgs { cmp :: String, col :: Int, rev :: Bool } deriving Show
|
||||
defSortArgs = SorterArgs "lex" 0 False
|
||||
|
||||
|
||||
sorter :: SorterArgs -> [[String]] -> [[String]]
|
||||
sorter (SorterArgs{..}) = case cmp of
|
||||
_ -> undefined
|
||||
|
||||
main = do
|
||||
sorter defSortArgs{cmp = "foo", col=1, rev=True} [[]]
|
||||
sorter defSortArgs{cmp = "foo"} [[]]
|
||||
sorter defSortArgs [[]]
|
||||
return ()
|
||||
12
Task/Optional-parameters/Haskell/optional-parameters-2.hs
Normal file
12
Task/Optional-parameters/Haskell/optional-parameters-2.hs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import Data.Maybe (fromMaybe)
|
||||
-- Use fromMaybe as an operator because its prettier
|
||||
(//) = flip fromMaybe
|
||||
|
||||
sorter :: Maybe String -> Maybe Int -> Maybe Bool -> [[String]] -> [[String]]
|
||||
sorter ((// "lex") -> cmp)
|
||||
((// 0) -> col)
|
||||
((// False) -> rev) = undefined
|
||||
|
||||
main = do
|
||||
sorter (Just "foo") (Just 1) (Just True)
|
||||
sorter Nothing Nothing Nothing
|
||||
20
Task/Optional-parameters/Icon/optional-parameters.icon
Normal file
20
Task/Optional-parameters/Icon/optional-parameters.icon
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
procedure main()
|
||||
X := [ [1,2,3], [2,3,1], [3,1,2]) # A list of lists
|
||||
Sort(X) # vanilla sort
|
||||
Sort(X,"ordering","numeric","column",2,"reverse") # using optional parameters
|
||||
end
|
||||
|
||||
procedure Sort(X,A[]) # A[] provides for a variable number of arguments
|
||||
while a := get(A) do {
|
||||
case a of {
|
||||
"ordering" : op := case get(A) | runerr(205,a) of {
|
||||
"lexicographic"|"string": "<<"
|
||||
"numeric": "<"
|
||||
default: runerr(205,op)
|
||||
}
|
||||
"column" : col := 0 < integer(col := get(A)) | runerr(205,col)
|
||||
"reverse" : reverseorder := reverse
|
||||
default: runerr(205,a)
|
||||
}
|
||||
return (\reverseorder|1)(bubblesortf(X,\c|1,\op|"<<")) # reverse or return the sorted list
|
||||
end
|
||||
6
Task/Optional-parameters/J/optional-parameters-1.j
Normal file
6
Task/Optional-parameters/J/optional-parameters-1.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
srtbl=: verb define
|
||||
'' srtbl y
|
||||
:
|
||||
'`ordering column reverse'=. x , (#x)}. ]`0:`0:
|
||||
|.^:reverse y /: ordering (column {"1 ])y
|
||||
)
|
||||
40
Task/Optional-parameters/J/optional-parameters-2.j
Normal file
40
Task/Optional-parameters/J/optional-parameters-2.j
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
]Table=: ('a';'b';'c'),('';'q';'z'),:'zip';'zap';'Zot'
|
||||
┌───┬───┬───┐
|
||||
│a │b │c │
|
||||
├───┼───┼───┤
|
||||
│ │q │z │
|
||||
├───┼───┼───┤
|
||||
│zip│zap│Zot│
|
||||
└───┴───┴───┘
|
||||
srtbl Table NB. default sort
|
||||
┌───┬───┬───┐
|
||||
│ │q │z │
|
||||
├───┼───┼───┤
|
||||
│a │b │c │
|
||||
├───┼───┼───┤
|
||||
│zip│zap│Zot│
|
||||
└───┴───┴───┘
|
||||
]`1: srtbl Table NB. sort by column 1
|
||||
┌───┬───┬───┐
|
||||
│a │b │c │
|
||||
├───┼───┼───┤
|
||||
│ │q │z │
|
||||
├───┼───┼───┤
|
||||
│zip│zap│Zot│
|
||||
└───┴───┴───┘
|
||||
]`2:`1: srtbl Table NB. reverse sort by column 2
|
||||
┌───┬───┬───┐
|
||||
│zip│zap│Zot│
|
||||
├───┼───┼───┤
|
||||
│ │q │z │
|
||||
├───┼───┼───┤
|
||||
│a │b │c │
|
||||
└───┴───┴───┘
|
||||
#&>`0: srtbl Table NB. sort by length
|
||||
┌───┬───┬───┐
|
||||
│ │q │z │
|
||||
├───┼───┼───┤
|
||||
│a │b │c │
|
||||
├───┼───┼───┤
|
||||
│zip│zap│Zot│
|
||||
└───┴───┴───┘
|
||||
73
Task/Optional-parameters/Java/optional-parameters.java
Normal file
73
Task/Optional-parameters/Java/optional-parameters.java
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import java.util.*;
|
||||
|
||||
public class OptionalParams {
|
||||
// "natural ordering" comparator
|
||||
static <T extends Comparable<? super T>> Comparator<T> naturalOrdering() {
|
||||
return Collections.reverseOrder(Collections.<T>reverseOrder());
|
||||
}
|
||||
|
||||
public static <T extends Comparable<? super T>> void
|
||||
sortTable(T[][] table) {
|
||||
sortTable(table, 0);
|
||||
}
|
||||
public static <T extends Comparable<? super T>> void
|
||||
sortTable(T[][] table,
|
||||
int column) {
|
||||
sortTable(table, column, false);
|
||||
}
|
||||
public static <T extends Comparable<? super T>> void
|
||||
sortTable(T[][] table,
|
||||
int column, boolean reverse) {
|
||||
sortTable(table, column, reverse, OptionalParams.<T>naturalOrdering());
|
||||
}
|
||||
public static <T> void sortTable(T[][] table,
|
||||
final int column,
|
||||
final boolean reverse,
|
||||
final Comparator<T> ordering) {
|
||||
Comparator<T[]> myCmp = new Comparator<T[]>() {
|
||||
public int compare(T[] x, T[] y) {
|
||||
return (reverse ? -1 : 1) *
|
||||
ordering.compare(x[column], y[column]);
|
||||
}
|
||||
};
|
||||
Arrays.sort(table, myCmp);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[][] data0 = {{"a", "b", "c"},
|
||||
{"", "q", "z"},
|
||||
{"zap", "zip", "Zot"}};
|
||||
System.out.println(Arrays.deepToString(data0));
|
||||
// prints: [[a, b, c], [, q, z], [zap, zip, Zot]]
|
||||
|
||||
// we copy it so that we don't change the original copy
|
||||
String[][] data = data0.clone();
|
||||
sortTable(data);
|
||||
System.out.println(Arrays.deepToString(data));
|
||||
// prints: [[, q, z], [a, b, c], [zap, zip, Zot]]
|
||||
|
||||
data = data0.clone();
|
||||
sortTable(data, 2);
|
||||
System.out.println(Arrays.deepToString(data));
|
||||
// prints: [[zap, zip, Zot], [a, b, c], [, q, z]]
|
||||
|
||||
data = data0.clone();
|
||||
sortTable(data, 1);
|
||||
System.out.println(Arrays.deepToString(data));
|
||||
// prints: [[a, b, c], [, q, z], [zap, zip, Zot]]
|
||||
|
||||
data = data0.clone();
|
||||
sortTable(data, 1, true);
|
||||
System.out.println(Arrays.deepToString(data));
|
||||
// prints: [[zap, zip, Zot], [, q, z], [a, b, c]]
|
||||
|
||||
data = data0.clone();
|
||||
sortTable(data, 0, false, new Comparator<String>() {
|
||||
public int compare(String a, String b) {
|
||||
return b.length() - a.length();
|
||||
}
|
||||
});
|
||||
System.out.println(Arrays.deepToString(data));
|
||||
// prints: [[zap, zip, Zot], [a, b, c], [, q, z]]
|
||||
}
|
||||
}
|
||||
10
Task/Optional-parameters/JavaScript/optional-parameters.js
Normal file
10
Task/Optional-parameters/JavaScript/optional-parameters.js
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
function sorter(table, options) {
|
||||
opts = {}
|
||||
opts.ordering = options.ordering || 'lexicographic';
|
||||
opts.column = options.column || 0;
|
||||
opts.reverse = options.reverse || false;
|
||||
|
||||
// ...
|
||||
}
|
||||
|
||||
sorter(the_data, {reverse: true, ordering: 'numeric'});
|
||||
3
Task/Optional-parameters/Jq/optional-parameters-1.jq
Normal file
3
Task/Optional-parameters/Jq/optional-parameters-1.jq
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
def bar: 2 *.;
|
||||
|
||||
def foo: {"a": bar};
|
||||
1
Task/Optional-parameters/Jq/optional-parameters-2.jq
Normal file
1
Task/Optional-parameters/Jq/optional-parameters-2.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
def less_than_or_equal: .[0] <= .[1];
|
||||
9
Task/Optional-parameters/Jq/optional-parameters-3.jq
Normal file
9
Task/Optional-parameters/Jq/optional-parameters-3.jq
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
def sorter(options):
|
||||
sort_table( if (options|has("ordering")) then options.ordering
|
||||
else less_than_or_equal
|
||||
end;
|
||||
options.column or 0;
|
||||
options.reverse or false );
|
||||
|
||||
# If jq > 1.4 is being used, we may also define:
|
||||
def sorter: sorter({});
|
||||
6
Task/Optional-parameters/Jq/optional-parameters-4.jq
Normal file
6
Task/Optional-parameters/Jq/optional-parameters-4.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[1,2] | sorter({ "reverse": true, "ordering": less_than_or_equal } )
|
||||
|
||||
[1,2] | sorter({ "reverse": true })
|
||||
|
||||
# If sorter/0 has also been defined:
|
||||
[1,2] | sorter
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
sorttable(T; ordering=<, column=1, reverse=false) =
|
||||
sort(T, by = t -> t[column], lt = reverse ? (a,b) -> ordering(b,a) : ordering)
|
||||
sorttable(T, ordering=<, column=1, reverse=false) =
|
||||
sorttable(T, ordering=ordering, column=column, reverse=reverse)
|
||||
17
Task/Optional-parameters/Julia/optional-parameters-2.julia
Normal file
17
Task/Optional-parameters/Julia/optional-parameters-2.julia
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
julia> data = {["a", "b", "c"], ["", "q", "z"], ["zap", "zip", "Zot"]}
|
||||
3-element Array{Any,1}:
|
||||
["a","b","c"]
|
||||
["","q","z"]
|
||||
["zap","zip","Zot"]
|
||||
|
||||
julia> sorttable(data, column=2, reverse=true) # named arguments
|
||||
3-element Array{Any,1}:
|
||||
["zap","zip","Zot"]
|
||||
["","q","z"]
|
||||
["a","b","c"]
|
||||
|
||||
julia> sorttable(data, >, 2) # the same thing, with positional arguments
|
||||
3-element Array{Any,1}:
|
||||
["zap","zip","Zot"]
|
||||
["","q","z"]
|
||||
["a","b","c"]
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
include ..\Utilitys.tlhy
|
||||
|
||||
:mypower
|
||||
1 tolist flatten len
|
||||
1 equal
|
||||
( [pop drop 2]
|
||||
[pop pop drop]
|
||||
) if
|
||||
power
|
||||
;
|
||||
|
||||
"2 ^2 = " print 2 mypower ?
|
||||
"2 ^3 = " print ( 2 3 ) mypower ?
|
||||
|
||||
"End " input
|
||||
46
Task/Optional-parameters/Kotlin/optional-parameters.kotlin
Normal file
46
Task/Optional-parameters/Kotlin/optional-parameters.kotlin
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// version 1.1.51
|
||||
|
||||
typealias Table = List<List<String>>
|
||||
|
||||
/* Note that if ordering is specified, first two parameters are ignored */
|
||||
fun Table.sort(
|
||||
column: Int = 0,
|
||||
reverse: Boolean = false,
|
||||
ordering: Comparator<List<String>> =
|
||||
if (!reverse) compareBy { it[column] }
|
||||
else compareByDescending { it[column] }
|
||||
) = this.sortedWith(ordering)
|
||||
|
||||
fun Table.print(title: String) {
|
||||
println(title)
|
||||
for (i in 0 until this.size) {
|
||||
for (j in 0 until this[0].size) System.out.print("%-3s ".format(this[i][j]))
|
||||
println()
|
||||
}
|
||||
println()
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val table = listOf(
|
||||
listOf("a", "b", "c"),
|
||||
listOf("", "q", "z"),
|
||||
listOf("zap", "zip", "Zot")
|
||||
)
|
||||
table.print("Original:")
|
||||
|
||||
val titles = listOf(
|
||||
"Sorted by col 0:", "Sorted by col 1:", "Sorted by col 2:",
|
||||
"Reverse sorted by col 0:", "Reverse sorted by col 1:", "Reverse Sorted by col 2"
|
||||
)
|
||||
val params = listOf(
|
||||
0 to false, 1 to false, 2 to false, 0 to true, 1 to true, 2 to true
|
||||
)
|
||||
for ((i, title) in titles.withIndex()) {
|
||||
val table2 = table.sort(params[i].first, params[i].second)
|
||||
table2.print(title)
|
||||
}
|
||||
// using non-default Comparator (case insensitive by col 2, reversed)
|
||||
val comp: Comparator<List<String>> = compareByDescending { it[2].toLowerCase() }
|
||||
val table3 = table.sort(ordering = comp)
|
||||
table3.print("Reverse case insensitive sort by col 2:")
|
||||
}
|
||||
33
Task/Optional-parameters/Lasso/optional-parameters.lasso
Normal file
33
Task/Optional-parameters/Lasso/optional-parameters.lasso
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
define sortarray( // params are set by position
|
||||
items::array, // required param
|
||||
ordering::string = 'lexicographic', // optional param
|
||||
column::integer = 1,
|
||||
reverse::boolean = false
|
||||
) => {
|
||||
// sorting process
|
||||
local(sorteditems = array)
|
||||
// Lasso has no build in method to sort an array of arrays by position in the contained arrays
|
||||
// But a method could be built for it
|
||||
return #sorteditems
|
||||
}
|
||||
|
||||
define sortarray(
|
||||
-items::array, // required param
|
||||
-ordering::string = 'lexicographic', // optional param
|
||||
-column::integer = 1,
|
||||
-reverse::boolean = false
|
||||
) => sortarray(#items, #ordering, #column, #reverse)
|
||||
|
||||
local(items = array(
|
||||
array(10, 'red', 'Volvo'),
|
||||
array(15, 'gren', 'Ford'),
|
||||
array(48, 'yellow', 'Kia'),
|
||||
array(12, 'black', 'Holden'),
|
||||
array(19, 'brown', 'Fiat'),
|
||||
array(8, 'pink', 'Batmobile'),
|
||||
array(74, 'orange', 'Bicycle')
|
||||
))
|
||||
|
||||
sortarray(-items = #items, -reverse)
|
||||
|
||||
sortarray(#items)
|
||||
3
Task/Optional-parameters/Logo/optional-parameters-1.logo
Normal file
3
Task/Optional-parameters/Logo/optional-parameters-1.logo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
to sort :table [:column 1] [:ordering "before?] [:reverse "false]
|
||||
; ...
|
||||
end
|
||||
3
Task/Optional-parameters/Logo/optional-parameters-2.logo
Normal file
3
Task/Optional-parameters/Logo/optional-parameters-2.logo
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
sort :table
|
||||
(sort :table 2)
|
||||
(sort :table 3 "less? "true)
|
||||
43
Task/Optional-parameters/Lua/optional-parameters.lua
Normal file
43
Task/Optional-parameters/Lua/optional-parameters.lua
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
function showTable(tbl)
|
||||
if type(tbl)=='table' then
|
||||
local result = {}
|
||||
for _, val in pairs(tbl) do
|
||||
table.insert(result, showTable(val))
|
||||
end
|
||||
return '{' .. table.concat(result, ', ') .. '}'
|
||||
else
|
||||
return (tostring(tbl))
|
||||
end
|
||||
end
|
||||
|
||||
function sortTable(op)
|
||||
local tbl = op.table or {}
|
||||
local column = op.column or 1
|
||||
local reverse = op.reverse or false
|
||||
local cmp = op.cmp or (function (a, b) return a < b end)
|
||||
local compareTables = function (a, b)
|
||||
local result = cmp(a[column], b[column])
|
||||
if reverse then return not result else return result end
|
||||
end
|
||||
table.sort(tbl, compareTables)
|
||||
end
|
||||
|
||||
A = {{"quail", "deer", "snake"},
|
||||
{"dalmation", "bear", "fox"},
|
||||
{"ant", "cougar", "coyote"}}
|
||||
print('original', showTable(A))
|
||||
|
||||
sortTable{table=A}
|
||||
print('defaults', showTable(A))
|
||||
|
||||
sortTable{table=A, column=2}
|
||||
print('col 2 ', showTable(A))
|
||||
|
||||
sortTable{table=A, column=3}
|
||||
print('col 3 ', showTable(A))
|
||||
|
||||
sortTable{table=A, column=3, reverse=true}
|
||||
print('col 3 rev', showTable(A))
|
||||
|
||||
sortTable{table=A, cmp=(function (a, b) return #a < #b end)}
|
||||
print('by length', showTable(A))
|
||||
13
Task/Optional-parameters/Maple/optional-parameters-1.maple
Normal file
13
Task/Optional-parameters/Maple/optional-parameters-1.maple
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
OptionalSort := proc(input, {
|
||||
ordering :: Or(procedures,identical("lexicographic")) := "lexicographic",
|
||||
column :: posint := 1,
|
||||
reverse :: truefalse := false
|
||||
} )
|
||||
local compare;
|
||||
if ordering = "lexicographic" then
|
||||
compare := (x,y)->evalb(`if`(reverse,x[column]>=y[column],x[column]<=y[column]));
|
||||
else
|
||||
compare := (x,y)->`if`(reverse,ordering(x[column],y),ordering(y,x));
|
||||
end if;
|
||||
sort( input, compare );
|
||||
end proc:
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
> L := [[1, 2], [3, 4], [-5, 7]]:
|
||||
> OptionalSort(L);
|
||||
[[-5, 7], [1, 2], [3, 4]]
|
||||
> OptionalSort(L, reverse);
|
||||
[[3, 4], [1, 2], [-5, 7]]
|
||||
> OptionalSort(L, reverse, column = 2);
|
||||
[[-5, 7], [3, 4], [1, 2]]
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
Options[OptionalSort]={ordering->lexicographic,column->1,reverse-> False};
|
||||
OptionalSort[x_List,OptionsPattern[]]:=If[OptionValue[reverse]==True,
|
||||
SortBy[x ,#[[OptionValue[column]]]&]//Reverse,
|
||||
SortBy[x,#[[OptionValue[column]]]&] ]
|
||||
|
||||
OptionalSort[{{"a" ,"b", "c"}, {"", "q", "z"},{"zap" ,"zip", "Zot"}} ]
|
||||
->{{,q,z},{a,b,c},{zap,zip,Zot}}
|
||||
|
||||
OptionalSort[{{"a" ,"b", "c"}, {"", "q", "z"},{"zap" ,"zip", "Zot"}},{ordering->lexicographic,column->2,reverse-> True} ]
|
||||
->{{zap,zip,Zot},{,q,z},{a,b,c}}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Sorter (table : list[list[string]], ordering = "lexicographic", column = 0, reverse = false) : list[list[string]]
|
||||
{
|
||||
// implementation goes here
|
||||
}
|
||||
22
Task/Optional-parameters/Nim/optional-parameters.nim
Normal file
22
Task/Optional-parameters/Nim/optional-parameters.nim
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import algorithm, strutils, sugar
|
||||
|
||||
proc printTable(a: seq[seq[string]]) =
|
||||
for row in a:
|
||||
for x in row: stdout.write x, repeat(' ', 4 - x.len)
|
||||
echo ""
|
||||
echo ""
|
||||
|
||||
proc sortTable(a: seq[seq[string]], column = 0, reverse = false,
|
||||
ordering: (proc(a,b: string): int) = system.cmp) : seq[seq[string]] =
|
||||
let order = if reverse: Descending else: Ascending
|
||||
result = a
|
||||
result.sort(proc(x,y:seq[string]):int = ordering(x[column],y[column]), order)
|
||||
|
||||
const data = @[@["a", "b", "c"], @["", "q", "z"], @["zap", "zip", "Zot"]]
|
||||
|
||||
printTable data
|
||||
printTable sortTable(data)
|
||||
printTable sortTable(data, column = 2)
|
||||
printTable sortTable(data, column = 1)
|
||||
printTable sortTable(data, column = 1, reverse = true)
|
||||
printTable sortTable(data, ordering = (a,b) => cmp[int](b.len,a.len))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
let sort_table ?(ordering = compare) ?(column = 0) ?(reverse = false) table =
|
||||
let cmp x y = ordering (List.nth x column) (List.nth y column) * (if reverse then -1 else 1) in
|
||||
List.sort cmp table
|
||||
18
Task/Optional-parameters/OCaml/optional-parameters-2.ocaml
Normal file
18
Task/Optional-parameters/OCaml/optional-parameters-2.ocaml
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# let data = [["a"; "b"; "c"]; [""; "q"; "z"]; ["zap"; "zip"; "Zot"]];;
|
||||
val data : string list list =
|
||||
[["a"; "b"; "c"]; [""; "q"; "z"]; ["zap"; "zip"; "Zot"]]
|
||||
# sort_table data;;
|
||||
- : string list list =
|
||||
[[""; "q"; "z"]; ["a"; "b"; "c"]; ["zap"; "zip"; "Zot"]]
|
||||
# sort_table ~column:2 data;;
|
||||
- : string list list =
|
||||
[["zap"; "zip"; "Zot"]; ["a"; "b"; "c"]; [""; "q"; "z"]]
|
||||
# sort_table ~column:1 data;;
|
||||
- : string list list =
|
||||
[["a"; "b"; "c"]; [""; "q"; "z"]; ["zap"; "zip"; "Zot"]]
|
||||
# sort_table ~column:1 ~reverse:true data;;
|
||||
- : string list list =
|
||||
[["zap"; "zip"; "Zot"]; [""; "q"; "z"]; ["a"; "b"; "c"]]
|
||||
# sort_table ~ordering:(fun a b -> compare (String.length b) (String.length a)) data;;
|
||||
- : string list list =
|
||||
[["zap"; "zip"; "Zot"]; ["a"; "b"; "c"]; [""; "q"; "z"]]
|
||||
25
Task/Optional-parameters/Objective-C/optional-parameters.m
Normal file
25
Task/Optional-parameters/Objective-C/optional-parameters.m
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
typedef enum { kOrdNone, kOrdLex, kOrdByAddress, kOrdNumeric } SortOrder;
|
||||
|
||||
@interface MyArray : NSObject {}
|
||||
// . . .
|
||||
@end
|
||||
|
||||
@implementation MyArray
|
||||
|
||||
- (void)sort {
|
||||
[self sortWithOrdering:kOrdLex onColumn:0 reversed:NO];
|
||||
}
|
||||
|
||||
- (void)sortWithOrdering:(SortOrder)ord {
|
||||
[self sortWithOrdering:ord onColumn:0 reversed:NO];
|
||||
}
|
||||
|
||||
- (void)sortWithOrdering:(SortOrder)ord onColumn:(int)col {
|
||||
[self sortWithOrdering:ord onColumn:col reversed:NO];
|
||||
}
|
||||
|
||||
- (void)sortWithOrdering:(SortOrder)ord onColumn:(int)col reversed:(BOOL)rev {
|
||||
// . . . Actual sort goes here . . .
|
||||
}
|
||||
|
||||
@end
|
||||
30
Task/Optional-parameters/Oz/optional-parameters.oz
Normal file
30
Task/Optional-parameters/Oz/optional-parameters.oz
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
declare
|
||||
class Table
|
||||
attr
|
||||
rows
|
||||
|
||||
meth init(Rows)
|
||||
rows := Rows
|
||||
end
|
||||
|
||||
meth sort(ordering:O<=Lexicographic column:C<=1 reverse:R<=false)
|
||||
fun {Predicate Row1 Row2}
|
||||
Res = {O {Nth Row1 C} {Nth Row2 C}}
|
||||
in
|
||||
if R then {Not Res} else Res end
|
||||
end
|
||||
in
|
||||
rows := {Sort @rows Predicate}
|
||||
end
|
||||
end
|
||||
|
||||
fun {Lexicographic As Bs} %% omitted for brevity
|
||||
end
|
||||
|
||||
T = {New Table init([["a" "b" "c"] ["" "q" "z"] ["zap" "zip" "Zot"]])}
|
||||
in
|
||||
{T sort}
|
||||
{T sort(column:3)}
|
||||
{T sort(column:2)}
|
||||
{T sort(column:2 reverse:true)}
|
||||
{T sort(ordering:fun {$ A B} {Length B} < {Length A} end)}
|
||||
|
|
@ -0,0 +1 @@
|
|||
sort(v, ordering=0, column=0, reverse=0)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
/*
|
||||
GP;install("test_func", "vDG", "test", "path/to/test.gp.so");
|
||||
*/
|
||||
void
|
||||
test_func(GEN x) {
|
||||
if (x == NULL)
|
||||
pari_printf("Argument omitted.\n");
|
||||
else
|
||||
pari_printf("Argument was: %Ps\n", x);
|
||||
}
|
||||
10
Task/Optional-parameters/Perl/optional-parameters-1.pl
Normal file
10
Task/Optional-parameters/Perl/optional-parameters-1.pl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
sub sorttable
|
||||
{my @table = @{shift()};
|
||||
my %opt =
|
||||
(ordering => sub {$_[0] cmp $_[1]}, column => 0, reverse => 0, @_);
|
||||
my $col = $opt{column};
|
||||
my $func = $opt{ordering};
|
||||
my @result = sort
|
||||
{$func->($a->[$col], $b->[$col])}
|
||||
@table;
|
||||
return ($opt{reverse} ? [reverse @result] : \@result);}
|
||||
5
Task/Optional-parameters/Perl/optional-parameters-2.pl
Normal file
5
Task/Optional-parameters/Perl/optional-parameters-2.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
my $a = [["a", "b", "c"], ["", "q", "z"], ["zap", "zip", "Zot"]];
|
||||
foreach (@{sorttable $a, column => 1, reverse => 1})
|
||||
{foreach (@$_)
|
||||
{printf "%-5s", $_;}
|
||||
print "\n";}
|
||||
8
Task/Optional-parameters/Phix/optional-parameters-1.phix
Normal file
8
Task/Optional-parameters/Phix/optional-parameters-1.phix
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
-->
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">increment</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">inc</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">inc</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">increment</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- shows 6</span>
|
||||
<span style="color: #0000FF;">?</span><span style="color: #000000;">increment</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- shows 7</span>
|
||||
<!--
|
||||
3
Task/Optional-parameters/Phix/optional-parameters-2.phix
Normal file
3
Task/Optional-parameters/Phix/optional-parameters-2.phix
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
-->
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d records sorted in %3.2s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">records</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">})</span>
|
||||
<!--
|
||||
23
Task/Optional-parameters/Phix/optional-parameters-3.phix
Normal file
23
Task/Optional-parameters/Phix/optional-parameters-3.phix
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
-->
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">sortcol</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">sortdir</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">by_column</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">sortdir</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">compare</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">sortcol</span><span style="color: #0000FF;">],</span><span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">][</span><span style="color: #000000;">sortcol</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">table_size</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- {1,2,..table_size}</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">click_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">self</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">pStatus</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">...</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">sortc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"SORTSIGN%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">sortdir</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">IupGetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">self</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sortc</span><span style="color: #0000FF;">)=</span><span style="color: #008000;">"DOWN"</span><span style="color: #0000FF;">?-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">self</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sortc</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sortdir</span><span style="color: #0000FF;">=-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"UP"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"DOWN"</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #000000;">sortcol</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span>
|
||||
<span style="color: #000000;">tags</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">custom_sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"by_column"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">tags</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">value_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*self*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tags</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">][</span><span style="color: #000000;">c</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
def mypower
|
||||
1 tolist flatten len
|
||||
1 == if
|
||||
1 get 2
|
||||
else
|
||||
2 get swap 1 get rot
|
||||
endif
|
||||
power
|
||||
nip
|
||||
enddef
|
||||
|
||||
"2 ^2 = " print 2 mypower print nl
|
||||
"2 ^3 = " print 2 3 2 tolist mypower print
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
include ..\Utilitys.pmt
|
||||
|
||||
def mypower
|
||||
1 tolist flatten len
|
||||
1 == if
|
||||
pop drop 2
|
||||
else
|
||||
pop pop drop
|
||||
endif
|
||||
power
|
||||
enddef
|
||||
|
||||
"2 ^2: " print 2 mypower ?
|
||||
"2 ^3: " print ( 2 3 ) mypower ?
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
include ..\Utilitys.pmt
|
||||
|
||||
( ( "a" "b" "c" )
|
||||
( "" "q" "z" )
|
||||
( "zap" "zip" "Zot" ) )
|
||||
|
||||
|
||||
def mysort /# column reverse #/
|
||||
len >ps
|
||||
tps 0 == if drop ( 1 false ) endif
|
||||
ps> 1 == if false 0 put endif
|
||||
pop swap >ps pop drop swap
|
||||
len for var i
|
||||
i get
|
||||
tps extract swap 1 put
|
||||
i set
|
||||
endfor
|
||||
sort
|
||||
swap if reverse endif
|
||||
len for var i
|
||||
i get
|
||||
pop swap tps put
|
||||
i set
|
||||
endfor
|
||||
ps> drop
|
||||
enddef
|
||||
|
||||
pstack
|
||||
( ) mysort pstack
|
||||
( 3 ) mysort pstack
|
||||
( 2 ) mysort pstack
|
||||
( 2 true ) mysort pstack
|
||||
8
Task/Optional-parameters/PicoLisp/optional-parameters.l
Normal file
8
Task/Optional-parameters/PicoLisp/optional-parameters.l
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
(de sortTable (Tbl . @)
|
||||
(let (Ordering prog Column 1 Reverse NIL) # Set defaults
|
||||
(bind (rest) # Bind optional params
|
||||
(setq Tbl
|
||||
(by '((L) (Ordering (get L Column)))
|
||||
sort
|
||||
Tbl ) )
|
||||
(if Reverse (flip Tbl) Tbl) ) ) )
|
||||
35
Task/Optional-parameters/Python/optional-parameters.py
Normal file
35
Task/Optional-parameters/Python/optional-parameters.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
>>> def printtable(data):
|
||||
for row in data:
|
||||
print ' '.join('%-5s' % ('"%s"' % cell) for cell in row)
|
||||
|
||||
|
||||
>>> import operator
|
||||
>>> def sorttable(table, ordering=None, column=0, reverse=False):
|
||||
return sorted(table, cmp=ordering, key=operator.itemgetter(column), reverse=reverse)
|
||||
|
||||
>>> data = [["a", "b", "c"], ["", "q", "z"], ["zap", "zip", "Zot"]]
|
||||
>>> printtable(data)
|
||||
"a" "b" "c"
|
||||
"" "q" "z"
|
||||
"zap" "zip" "Zot"
|
||||
>>> printtable( sorttable(data) )
|
||||
"" "q" "z"
|
||||
"a" "b" "c"
|
||||
"zap" "zip" "Zot"
|
||||
>>> printtable( sorttable(data, column=2) )
|
||||
"zap" "zip" "Zot"
|
||||
"a" "b" "c"
|
||||
"" "q" "z"
|
||||
>>> printtable( sorttable(data, column=1) )
|
||||
"a" "b" "c"
|
||||
"" "q" "z"
|
||||
"zap" "zip" "Zot"
|
||||
>>> printtable( sorttable(data, column=1, reverse=True) )
|
||||
"zap" "zip" "Zot"
|
||||
"" "q" "z"
|
||||
"a" "b" "c"
|
||||
>>> printtable( sorttable(data, ordering=lambda a,b: cmp(len(b),len(a))) )
|
||||
"zap" "zip" "Zot"
|
||||
"a" "b" "c"
|
||||
"" "q" "z"
|
||||
>>>
|
||||
7
Task/Optional-parameters/R/optional-parameters.r
Normal file
7
Task/Optional-parameters/R/optional-parameters.r
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
tablesort <- function(x, ordering="lexicographic", column=1, reverse=false)
|
||||
{
|
||||
# Implementation
|
||||
}
|
||||
|
||||
# Usage is e.g.
|
||||
tablesort(mytable, column=3)
|
||||
23
Task/Optional-parameters/REXX/optional-parameters-1.rexx
Normal file
23
Task/Optional-parameters/REXX/optional-parameters-1.rexx
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
sortStrings: procedure expose @. /*the stemmed array is named: @. */
|
||||
col= 1 /*set some defaults (here and below). */
|
||||
reverse= 'NO'
|
||||
order= 'LEXICOGRAPHIC'
|
||||
arg options /*obtain the options (in uppercase). */
|
||||
do j=1 for words(options) /*examine all the words (options). */
|
||||
x= word(options, j)
|
||||
select
|
||||
when datatype(x, 'W') then col= x / 1 /*normalize the number. */
|
||||
when pos('=', x)==0 then order= x /*has it an equal sign? */
|
||||
otherwise parse var x nam '=' value /*get value.*/
|
||||
end /*select*/
|
||||
end /*j*/
|
||||
|
||||
/*╔═══════════════════════════════════════════════════════════╗
|
||||
║ check for errors here: COL isn't a positive integer ···, ║
|
||||
║ REVERSE value isn't NO or YES, ║
|
||||
║ ORDER value is recognized ··· ║
|
||||
╚═══════════════════════════════════════════════════════════╝*/
|
||||
|
||||
... main body of string sort here ...
|
||||
|
||||
return /*stick a fork in it, we're all done. */
|
||||
8
Task/Optional-parameters/REXX/optional-parameters-2.rexx
Normal file
8
Task/Optional-parameters/REXX/optional-parameters-2.rexx
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
/*REXX example uses the SortStrings subroutine with some (passed) optional arguments. */
|
||||
|
||||
|
||||
@.1= 'one'; @.2= "two"; @.3= 'three' /*define an array (@.) of strings here.*/
|
||||
|
||||
|
||||
call sortStrings 'Reverse=no' 3
|
||||
/*stick a fork in it, we're all done. */
|
||||
10
Task/Optional-parameters/Racket/optional-parameters.rkt
Normal file
10
Task/Optional-parameters/Racket/optional-parameters.rkt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#lang racket
|
||||
|
||||
(define (sort-table table
|
||||
[ordering string<=?]
|
||||
[column 0]
|
||||
[reverse? #f])
|
||||
(sort table (if reverse?
|
||||
(negate ordering)
|
||||
ordering)
|
||||
#:key (λ (row) (list-ref row column))))
|
||||
4
Task/Optional-parameters/Raku/optional-parameters-1.raku
Normal file
4
Task/Optional-parameters/Raku/optional-parameters-1.raku
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
method sorttable(:$column = 0, :$reverse, :&ordering = &infix:<cmp>) {
|
||||
my @result = self»[$column].sort: &ordering;
|
||||
return $reverse ?? @result.reverse !! @result;
|
||||
}
|
||||
4
Task/Optional-parameters/Raku/optional-parameters-2.raku
Normal file
4
Task/Optional-parameters/Raku/optional-parameters-2.raku
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
method sorttable-pos($column = 0, $reverse?, &ordering = &infix:<cmp>) {
|
||||
my @result = self»[$column].sort: &ordering;
|
||||
return $reverse ?? @result.reverse !! @result;
|
||||
}
|
||||
2
Task/Optional-parameters/Ruby/optional-parameters-1.rb
Normal file
2
Task/Optional-parameters/Ruby/optional-parameters-1.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
def table_sort(table, ordering=:<=>, column=0, reverse=false)
|
||||
# ...
|
||||
16
Task/Optional-parameters/Ruby/optional-parameters-2.rb
Normal file
16
Task/Optional-parameters/Ruby/optional-parameters-2.rb
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def table_sort(table, ordering: :<=>, column: 0, reverse: false)
|
||||
p = ordering.to_proc
|
||||
if reverse
|
||||
table.sort {|a, b| p.(b[column], a[column])}
|
||||
else
|
||||
table.sort {|a, b| p.(a[column], b[column])}
|
||||
end
|
||||
end
|
||||
|
||||
# Quick example:
|
||||
table = [
|
||||
["Ottowa", "Canada"],
|
||||
["Washington", "USA"],
|
||||
["Mexico City", "Mexico"],
|
||||
]
|
||||
p table_sort(table, column: 1)
|
||||
12
Task/Optional-parameters/Ruby/optional-parameters-3.rb
Normal file
12
Task/Optional-parameters/Ruby/optional-parameters-3.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
def table_sort(table, opts = {})
|
||||
defaults = {:ordering => :<=>, :column => 0, :reverse => false}
|
||||
opts = defaults.merge(opts)
|
||||
|
||||
c = opts[:column]
|
||||
p = opts[:ordering].to_proc
|
||||
if opts[:reverse]
|
||||
table.sort {|a, b| p.call(b[c], a[c])}
|
||||
else
|
||||
table.sort {|a, b| p.call(a[c], b[c])}
|
||||
end
|
||||
end
|
||||
139
Task/Optional-parameters/Rust/optional-parameters.rust
Normal file
139
Task/Optional-parameters/Rust/optional-parameters.rust
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
use std::cmp::Ordering;
|
||||
|
||||
struct Table {
|
||||
rows: Vec<Vec<String>>,
|
||||
ordering_function: fn(&str, &str) -> Ordering,
|
||||
ordering_column: usize,
|
||||
reverse: bool,
|
||||
}
|
||||
|
||||
impl Table {
|
||||
fn new(rows: Vec<Vec<String>>) -> Table {
|
||||
Table {
|
||||
rows: rows,
|
||||
ordering_column: 0,
|
||||
reverse: false,
|
||||
ordering_function: |str1, str2| str1.cmp(str2),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Table {
|
||||
fn with_ordering_column(&mut self, ordering_column: usize) -> &mut Table {
|
||||
self.ordering_column = ordering_column;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_reverse(&mut self, reverse: bool) -> &mut Table {
|
||||
self.reverse = reverse;
|
||||
self
|
||||
}
|
||||
|
||||
fn with_ordering_fun(&mut self, compare: fn(&str, &str) -> Ordering) -> &mut Table {
|
||||
self.ordering_function = compare;
|
||||
self
|
||||
}
|
||||
|
||||
fn sort(&mut self) {
|
||||
let fun = &mut self.ordering_function;
|
||||
let idx = self.ordering_column;
|
||||
if self.reverse {
|
||||
self.rows
|
||||
.sort_unstable_by(|vec1, vec2| (fun)(&vec1[idx], &vec2[idx]).reverse());
|
||||
} else {
|
||||
self.rows
|
||||
.sort_unstable_by(|vec1, vec2| (fun)(&vec1[idx], &vec2[idx]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::Table;
|
||||
|
||||
fn generate_test_table() -> Table {
|
||||
Table::new(vec![
|
||||
vec!["0".to_string(), "fff".to_string()],
|
||||
vec!["2".to_string(), "aab".to_string()],
|
||||
vec!["1".to_string(), "ccc".to_string()],
|
||||
])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple_sort() {
|
||||
let mut table = generate_test_table();
|
||||
table.sort();
|
||||
assert_eq!(
|
||||
table.rows,
|
||||
vec![
|
||||
vec!["0".to_string(), "fff".to_string()],
|
||||
vec!["1".to_string(), "ccc".to_string()],
|
||||
vec!["2".to_string(), "aab".to_string()],
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ordering_column() {
|
||||
let mut table = generate_test_table();
|
||||
table.with_ordering_column(1).sort();
|
||||
assert_eq!(
|
||||
table.rows,
|
||||
vec![
|
||||
vec!["2".to_string(), "aab".to_string()],
|
||||
vec!["1".to_string(), "ccc".to_string()],
|
||||
vec!["0".to_string(), "fff".to_string()],
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_reverse() {
|
||||
let mut table = generate_test_table();
|
||||
table.with_reverse(true).sort();
|
||||
assert_eq!(
|
||||
table.rows,
|
||||
vec![
|
||||
vec!["2".to_string(), "aab".to_string()],
|
||||
vec!["1".to_string(), "ccc".to_string()],
|
||||
vec!["0".to_string(), "fff".to_string()],
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_custom_ordering_fun() {
|
||||
let mut table = generate_test_table();
|
||||
// Simple ordering function that reverses stuff.
|
||||
// Should operate like the test before.
|
||||
table.with_ordering_fun(|x, y| x.cmp(y).reverse()).sort();
|
||||
assert_eq!(
|
||||
table.rows,
|
||||
vec![
|
||||
vec!["2".to_string(), "aab".to_string()],
|
||||
vec!["1".to_string(), "ccc".to_string()],
|
||||
vec!["0".to_string(), "fff".to_string()],
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_everything_together() {
|
||||
let mut table = generate_test_table();
|
||||
// Using the reversing cmp function, then reverse (= don't do anything)
|
||||
// then sort from column 1.
|
||||
table
|
||||
.with_ordering_fun(|x, y| x.cmp(y).reverse())
|
||||
.with_reverse(true)
|
||||
.with_ordering_column(1)
|
||||
.sort();
|
||||
assert_eq!(
|
||||
table.rows,
|
||||
vec![
|
||||
vec!["2".to_string(), "aab".to_string()],
|
||||
vec!["1".to_string(), "ccc".to_string()],
|
||||
vec!["0".to_string(), "fff".to_string()],
|
||||
],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
def sortTable(data: List[List[String]],
|
||||
ordering: (String, String) => Boolean = (_ < _),
|
||||
column: Int = 0,
|
||||
reverse: Boolean = false) = {
|
||||
val result = data.sortWith((a, b) => ordering(a(column), b(column)))
|
||||
if (reverse) result.reverse else result
|
||||
}
|
||||
11
Task/Optional-parameters/Scala/optional-parameters-2.scala
Normal file
11
Task/Optional-parameters/Scala/optional-parameters-2.scala
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
val data=List(List("a","b","c"), List("","q","z"), List("zap","zip","Zot"))
|
||||
println(data)
|
||||
//-> List(List(a, b, c), List(, q, z), List(zap, zip, Zot))
|
||||
println(sortTable(data))
|
||||
//-> List(List(, q, z), List(a, b, c), List(zap, zip, Zot))
|
||||
println(sortTable(data, reverse=true))
|
||||
//-> List(List(zap, zip, Zot), List(a, b, c), List(, q, z))
|
||||
println(sortTable(data, column=2))
|
||||
//-> List(List(zap, zip, Zot), List(a, b, c), List(, q, z))
|
||||
println(sortTable(data, ((a, b)=> b.size<a.size)))
|
||||
//-> List(List(zap, zip, Zot), List(a, b, c), List(, q, z))
|
||||
16
Task/Optional-parameters/Sidef/optional-parameters-1.sidef
Normal file
16
Task/Optional-parameters/Sidef/optional-parameters-1.sidef
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
func table_sort(table, ordering: '<=>', column: 0, reverse: false) {
|
||||
if (reverse) {
|
||||
table.sort {|a,b| b[column].$ordering(a[column])}
|
||||
} else {
|
||||
table.sort {|a,b| a[column].$ordering(b[column])}
|
||||
}
|
||||
}
|
||||
|
||||
# Quick example:
|
||||
var table = [
|
||||
["Ottowa", "Canada"],
|
||||
["Washington", "USA"],
|
||||
["Mexico City", "Mexico"],
|
||||
];
|
||||
|
||||
say table_sort(table, column: 1);
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
class String {
|
||||
method my_sort(arg) {
|
||||
(self.len <=> arg.len) ->
|
||||
|| (self.lc <=> arg.lc) ->
|
||||
|| (self <=> arg)
|
||||
}
|
||||
}
|
||||
|
||||
say table_sort(table, column: 1, ordering: 'my_sort');
|
||||
8
Task/Optional-parameters/Slate/optional-parameters.slate
Normal file
8
Task/Optional-parameters/Slate/optional-parameters.slate
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
s@(Sequence traits) tableSort &column: column &sortBy: sortBlock &reverse: reverse
|
||||
[
|
||||
column `defaultsTo: 0.
|
||||
sortBlock `defaultsTo: [| :a :b | (a lexicographicallyCompare: b) isNegative].
|
||||
(reverse `defaultsTo: False)
|
||||
ifTrue: [sortBlock := [| :a :b | (sortBlock applyTo: {a. b}) not]].
|
||||
s sortBy: [| :a :b | sortBlock applyTo: {a at: column. b at: column}]
|
||||
].
|
||||
5
Task/Optional-parameters/Swift/optional-parameters.swift
Normal file
5
Task/Optional-parameters/Swift/optional-parameters.swift
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
enum SortOrder { case kOrdNone, kOrdLex, kOrdByAddress, kOrdNumeric }
|
||||
|
||||
func sortTable(table: [[String]], less: (String,String)->Bool = (<), column: Int = 0, reversed: Bool = false) {
|
||||
// . . . Actual sort goes here . . .
|
||||
}
|
||||
5
Task/Optional-parameters/TIScript/optional-parameters.ti
Normal file
5
Task/Optional-parameters/TIScript/optional-parameters.ti
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
function sorter(table, ordering = "lexicographic", column = 0, reverse = false) {
|
||||
// ...
|
||||
}
|
||||
|
||||
sorter(the_data,"numeric");
|
||||
15
Task/Optional-parameters/Tcl/optional-parameters-1.tcl
Normal file
15
Task/Optional-parameters/Tcl/optional-parameters-1.tcl
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
proc tablesort {table {ordering ""} {column 0} {reverse 0}} {
|
||||
set direction [expr {$reverse ? "-decreasing" : "-increasing"}]
|
||||
if {$ordering ne ""} {
|
||||
lsort -command $ordering $direction -index $column $table
|
||||
} else {
|
||||
lsort $direction -index $column $table
|
||||
}
|
||||
}
|
||||
|
||||
puts [tablesort $data]
|
||||
puts [tablesort $data "" 1]
|
||||
puts [tablesort $data "" 0 1]
|
||||
puts [tablesort $data {
|
||||
apply {{a b} {expr {[string length $a]-[string length $b]}}}
|
||||
}]
|
||||
18
Task/Optional-parameters/Tcl/optional-parameters-2.tcl
Normal file
18
Task/Optional-parameters/Tcl/optional-parameters-2.tcl
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package require Tcl 8.5; # Only for the list expansion syntax
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
puts [tablesort $data]
|
||||
puts [tablesort $data column 1]
|
||||
puts [tablesort $data column 0]
|
||||
puts [tablesort $data column 0 reverse 1]
|
||||
puts [tablesort $data ordering {
|
||||
apply {{a b} {expr {[string length $b]-[string length $a]}}}
|
||||
}]
|
||||
36
Task/Optional-parameters/UNIX-Shell/optional-parameters.sh
Normal file
36
Task/Optional-parameters/UNIX-Shell/optional-parameters.sh
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/env bash
|
||||
# sort-args.sh
|
||||
|
||||
data() {
|
||||
cat <<EOF
|
||||
123 456 0789
|
||||
456 0789 123
|
||||
0789 123 456
|
||||
EOF
|
||||
}
|
||||
|
||||
# sort_table [column NUM | KIND | reverse] ... <INPUT >OUTPUT
|
||||
# KIND = lexicographical | numeric | human
|
||||
|
||||
sort_table() {
|
||||
local opts='-b'
|
||||
local column=1
|
||||
while (( $# > 0 )) ; do
|
||||
case "$1" in
|
||||
column|col|c) column=${2?Missing column number} ; shift ;;
|
||||
lexicographical|lex|l) opts+=' -d' ;;
|
||||
numeric|num|n) opts+=' -g' ;;
|
||||
human|hum|h) opts+=' -h' ;;
|
||||
reverse|rev|r) opts+=' -r' ;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
eval "sort $opts -k $column,$column -"
|
||||
}
|
||||
|
||||
echo sort defaults ; data | sort_table
|
||||
echo sort defaults reverse ; data | sort_table reverse
|
||||
echo sort column 2 ; data | sort_table col 2
|
||||
echo sort column 2 reverse ; data | sort_table col 2 reverse
|
||||
echo sort numeric ; data | sort_table numeric
|
||||
echo sort numeric reverse ; data | sort_table numeric reverse
|
||||
10
Task/Optional-parameters/Ursala/optional-parameters-1.ursala
Normal file
10
Task/Optional-parameters/Ursala/optional-parameters-1.ursala
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#import std
|
||||
#import nat
|
||||
|
||||
ss ::
|
||||
|
||||
ordering %fZ ~ordering||lleq!
|
||||
column %n ~column||1!
|
||||
reversed %b
|
||||
|
||||
sorter = +^(~reversed?/~&x! ~&!,-<+ +^/~ordering ~~+ ~&h++ //skip+ predecessor+ ~column)
|
||||
17
Task/Optional-parameters/Ursala/optional-parameters-2.ursala
Normal file
17
Task/Optional-parameters/Ursala/optional-parameters-2.ursala
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
example_table =
|
||||
|
||||
<
|
||||
<'foo','b '>,
|
||||
<'barr','a '>,
|
||||
<'bazzz','c'>>
|
||||
|
||||
#cast %sLLL
|
||||
|
||||
examples =
|
||||
|
||||
<
|
||||
(sorter ss&) example_table, # default sorting algorithm
|
||||
(sorter ss[ordering: leql]) example_table, # sort by field lengths but otherwise default
|
||||
(sorter ss[column: 2]) example_table, # etc.
|
||||
(sorter ss[reversed: true]) example_table,
|
||||
(sorter ss[reversed: true,column: 2]) example_table>
|
||||
31
Task/Optional-parameters/VBA/optional-parameters.vba
Normal file
31
Task/Optional-parameters/VBA/optional-parameters.vba
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
Private Sub optional_parameters(theRange As String, _
|
||||
Optional ordering As Integer = 0, _
|
||||
Optional column As Integer = 1, _
|
||||
Optional reverse As Integer = 1)
|
||||
ActiveSheet.Sort.SortFields.Clear
|
||||
ActiveSheet.Sort.SortFields.Add _
|
||||
Key:=Range(theRange).Columns(column), _
|
||||
SortOn:=SortOnValues, _
|
||||
Order:=reverse, _
|
||||
DataOption:=ordering 'the optional parameter ordering and above reverse
|
||||
With ActiveSheet.Sort
|
||||
.SetRange Range(theRange)
|
||||
.Header = xlGuess
|
||||
.MatchCase = False
|
||||
.Orientation = xlTopToBottom
|
||||
.SortMethod = xlPinYin
|
||||
.Apply
|
||||
End With
|
||||
End Sub
|
||||
Public Sub main()
|
||||
'Sort the strings in the active sheet in Excel
|
||||
'Supply the range of cells to be sorted
|
||||
'Optionally specify ordering, default is 0,
|
||||
'which is normal sort, text and data separately;
|
||||
'ordering:=1 treats text as numeric data.
|
||||
'Optionally specify column number, default is 1
|
||||
'Optionally specify reverse, default is 1
|
||||
'which sorts in ascending order.
|
||||
'Specifying reverse:=2 will sort in descending order.
|
||||
optional_parameters theRange:="A1:C4", ordering:=1, column:=2, reverse:=1
|
||||
End Sub
|
||||
46
Task/Optional-parameters/Wren/optional-parameters.wren
Normal file
46
Task/Optional-parameters/Wren/optional-parameters.wren
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import "./sort" for Cmp, Sort
|
||||
import "./seq" for Lst
|
||||
|
||||
class TableSorter {
|
||||
// uses 'merge' sort to avoid mutating original table
|
||||
// column is zero based
|
||||
static sort(table, ordering, column, reverse) {
|
||||
if (ordering == null) ordering = Fn.new { |r1, r2| Cmp.string.call(r1[column], r2[column]) }
|
||||
var sorted = Sort.merge(table, ordering)
|
||||
if (reverse) Lst.reverse(sorted)
|
||||
return sorted
|
||||
}
|
||||
|
||||
// overloads to simulate optional parameters
|
||||
static sort(table) { sort(table, null, 0, false) }
|
||||
static sort(table, ordering) { sort(table, ordering, 0, false) }
|
||||
static sort(table, ordering, column) { sort(table, ordering, column, false) }
|
||||
}
|
||||
|
||||
var table = [
|
||||
["first", "1"],
|
||||
["second", "2"],
|
||||
["fourth", "4"],
|
||||
["fifth", "5"],
|
||||
["third", "3"]
|
||||
]
|
||||
|
||||
System.print("Original table:")
|
||||
System.print(table.join("\n"))
|
||||
|
||||
System.print("\nAfter lexicographic sort by first column:")
|
||||
var table2 = TableSorter.sort(table)
|
||||
System.print(table2.join("\n"))
|
||||
|
||||
System.print("\nAfter sorting in length order of first column:")
|
||||
var ordering = Fn.new { |r1, r2| (r1[0].count - r2[0].count).sign }
|
||||
table2 = TableSorter.sort(table, ordering)
|
||||
System.print(table2.join("\n"))
|
||||
|
||||
System.print("\nAfter lexicographic sort by second column:")
|
||||
table2 = TableSorter.sort(table, null, 1)
|
||||
System.print(table2.join("\n"))
|
||||
|
||||
System.print("\nAfter reverse lexicographic sort by second column:")
|
||||
table2 = TableSorter.sort(table, null, 1, true)
|
||||
System.print(table2.join("\n"))
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue