Optional parameters: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(4 intermediate revisions by 2 users not shown)
Line 872:
 
Named parameters are not builtin, but map-patterns may be used as a substitute. (TODO: Example of this) [[Category:E examples needing attention]]
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
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();
}
}
</syntaxhighlight>
 
{{out}}
<pre>
original input:
[c, x, i]
[a, y, p]
[b, z, a]
 
by default sort on column 0:
[a, y, p]
[b, z, a]
[c, x, i]
 
by column 2:
[b, z, a]
[c, x, i]
[a, y, p]
 
by column 2 reversed:
[a, y, p]
[c, x, i]
[b, z, a]
</pre>
 
=={{header|Elixir}}==
Line 2,899 ⟶ 2,972:
sorter(the_data,"numeric");</syntaxhighlight>
 
=={{header|UnixUNIX Shell}}==
{{works with|bash|4.2}}
<syntaxhighlight lang="bash">#!/usr/bin/env bash
Line 3,065 ⟶ 3,138:
{{libheader|Wren-seq}}
Wren doesn't support optional parameters as such but does support method overloading by ''arity'' (i.e. number of parameters) which makes it easy to simulate them.
<syntaxhighlight lang="ecmascriptwren">import "./sort" for Cmp, Sort
import "./seq" for Lst
 
9,488

edits