Optional parameters: Difference between revisions

Content added Content deleted
(\Removed duplicate entry...)
(Added Elixir)
Line 20: Line 20:
See also:
See also:
* [[Named Arguments]]
* [[Named Arguments]]

=={{header|Ada}}==
=={{header|Ada}}==


Line 483: Line 484:
return 0;
return 0;
}</lang>
}</lang>



=={{header|D}}==
=={{header|D}}==
Line 613: Line 613:


Named parameters are not builtin, but map-patterns may be used as a substitute. (TODO: Example of this) [[Category:E examples needing attention]]
Named parameters are not builtin, but map-patterns may be used as a substitute. (TODO: Example of this) [[Category:E examples needing attention]]

=={{header|Elixir}}==
{{trans|Ruby}}
<lang elixir>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</lang>

{{out}}
<pre>
sort defaults [{"0789", "123", "456"}, {"123", "456", "0789"}, {"456", "0789", "123"}]
& reverse [{"456", "0789", "123"}, {"123", "456", "0789"}, {"0789", "123", "456"}]
sort column 2 [{"123", "456", "0789"}, {"456", "0789", "123"}, {"0789", "123", "456"}]
& reverse [{"0789", "123", "456"}, {"456", "0789", "123"}, {"123", "456", "0789"}]
sort numeric [{"123", "456", "0789"}, {"456", "0789", "123"}, {"0789", "123", "456"}]
& reverse [{"0789", "123", "456"}, {"456", "0789", "123"}, {"123", "456", "0789"}]
</pre>


=={{header|Erlang}}==
=={{header|Erlang}}==
Line 666: Line 710:
reverse [{"0789","123","456"},{"456","0789","123"},{"123","456","0789"}]
reverse [{"0789","123","456"},{"456","0789","123"},{"123","456","0789"}]
</pre>
</pre>



=={{header|Fortran}}==
=={{header|Fortran}}==
Line 852: Line 895:
[|"a"; "b"; "c"|]
[|"a"; "b"; "c"|]
[|""; "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. This approach is probably closest to "positional parameters" mentioned in the task description.
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.
Line 1,059: Line 1,103:
sorter Nothing Nothing Nothing
sorter Nothing Nothing Nothing
</lang>
</lang>

=={{header|Icon}} and {{header|Unicon}}==
=={{header|Icon}} and {{header|Unicon}}==
Optional named parameters are not the norm in Icon/Unicon. In the example below ''bubblesortf'' would be a version of [[Sorting_algorithms/Bubble_sort#Icon_and_Unicon|Bubble Sort]] modified to sort on a column number (Ordering is already supported). It could equally be replaced by any similarly modified Rosetta sort. The use of ''reverse'' on a list is a Unicon extension; in Icon a procedure from the IPL must be linked.
Optional named parameters are not the norm in Icon/Unicon. In the example below ''bubblesortf'' would be a version of [[Sorting_algorithms/Bubble_sort#Icon_and_Unicon|Bubble Sort]] modified to sort on a column number (Ordering is already supported). It could equally be replaced by any similarly modified Rosetta sort. The use of ''reverse'' on a list is a Unicon extension; in Icon a procedure from the IPL must be linked.
Line 1,310: Line 1,355:
["","q","z"]
["","q","z"]
["a","b","c"] </lang>
["a","b","c"] </lang>



=={{header|Lasso}}==
=={{header|Lasso}}==
Line 1,552: Line 1,596:
{T sort(column:2 reverse:true)}
{T sort(column:2 reverse:true)}
{T sort(ordering:fun {$ A B} {Length B} < {Length A} end)}</lang>
{T sort(ordering:fun {$ A B} {Length B} < {Length A} end)}</lang>



=={{header|PARI/GP}}==
=={{header|PARI/GP}}==