Jump to content

Optional parameters: Difference between revisions

→‎{{header|Go}}: added quotes to output
(→‎{{header|Go}}: added quotes to output)
Line 666:
[|""; "q"; "z"|]</pre>
=={{header|Go}}==
The most idiomatic way to write this particular sorting function would be a single function that required all three parameters. Wherever practical in Go, the zero value is used as a default, and that seems meaningful in this situation. Calling t.sort(nil, 0, false) to "take the defaults" would make sense. This approach is probably closest to "positional parameters" mentioned in the task description.
 
In the spirit of the task though, another solution would be to pass a struct with the three "parameters" as fields. While Go does not have named function parameters, it ''does'' have named fields in struct literals. Given,
Line 692:
column int
less func(cell, cell) bool
}
 
func (c cell) String() string {
return fmt.Sprintf("%q", string(c))
}
 
Line 699 ⟶ 703:
fmt.Println(row)
}
fmt.Println("")
}
 
Line 769 ⟶ 773:
}</lang>
Output:
<pre>-- song
-- song
["pail" "food"]
["pillbox" "nurse maids"]
["suitcase" "airedales"]
["bathtub" "chocolate"]
["schooner" "ice cream sodas"]
 
-- sorted on first column
["bathtub" "chocolate"]
["pail" "food"]
["pillbox" "nurse maids"]
["schooner" "ice cream sodas"]
["suitcase" "airedales"]
 
-- reverse sorted on first column
["suitcase" "airedales"]
["schooner" "ice cream sodas"]
["pillbox" "nurse maids"]
["pail" "food"]
["bathtub" "chocolate"]
 
-- sorted by descending string length on second column
["schooner" "ice cream sodas"]
["pillbox" "nurse maids"]
["suitcase" "airedales"]
["bathtub" "chocolate"]
["pail" "food"]</pre>
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
1,707

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.