Optional parameters: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: added quotes to output)
Line 666: Line 666:
[|""; "q"; "z"|]</pre>
[|""; "q"; "z"|]</pre>
=={{header|Go}}==
=={{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.
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,
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: Line 692:
column int
column int
less func(cell, cell) bool
less func(cell, cell) bool
}

func (c cell) String() string {
return fmt.Sprintf("%q", string(c))
}
}


Line 699: Line 703:
fmt.Println(row)
fmt.Println(row)
}
}
fmt.Println("")
fmt.Println()
}
}


Line 769: Line 773:
}</lang>
}</lang>
Output:
Output:
<pre>-- song
<pre>
-- song
[pail food]
["pail" "food"]
[pillbox nurse maids]
["pillbox" "nurse maids"]
[suitcase airedales]
["suitcase" "airedales"]
[bathtub chocolate]
["bathtub" "chocolate"]
[schooner ice cream sodas]
["schooner" "ice cream sodas"]


-- sorted on first column
-- sorted on first column
[bathtub chocolate]
["bathtub" "chocolate"]
[pail food]
["pail" "food"]
[pillbox nurse maids]
["pillbox" "nurse maids"]
[schooner ice cream sodas]
["schooner" "ice cream sodas"]
[suitcase airedales]
["suitcase" "airedales"]


-- reverse sorted on first column
-- reverse sorted on first column
[suitcase airedales]
["suitcase" "airedales"]
[schooner ice cream sodas]
["schooner" "ice cream sodas"]
[pillbox nurse maids]
["pillbox" "nurse maids"]
[pail food]
["pail" "food"]
[bathtub chocolate]
["bathtub" "chocolate"]


-- sorted by descending string length on second column
-- sorted by descending string length on second column
[schooner ice cream sodas]
["schooner" "ice cream sodas"]
[pillbox nurse maids]
["pillbox" "nurse maids"]
[suitcase airedales]
["suitcase" "airedales"]
[bathtub chocolate]
["bathtub" "chocolate"]
[pail food]</pre>
["pail" "food"]
</pre>


=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==