Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
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"))
|
||||
Loading…
Add table
Add a link
Reference in a new issue