Optional parameters: Difference between revisions

Content added Content deleted
(→‎{{header|F#}}: added F#)
Line 552: Line 552:


end program UsingTest</lang>
end program UsingTest</lang>

=={{header|F_Sharp|F#}}==
{{trans|Oz}}

F# supports optional parameters for member functions only, not for free-standing functions.

Optional parameters are marked by using a question mark in front of the identifier. Their values are passed as option types, i.e. as <code>Some value</code> or <code>None</code>. The helper function <code>defaultArg</code> can be used to specify default values. In the example below, we use shadowing in order to reuse the identifiers <code>ordering</code>, <code>column</code> and <code>reverse</code>.

<lang fsharp>type Table(rows:string[][]) =
// in-place sorting of rows
member x.Sort(?ordering, ?column, ?reverse) =
let ordering = defaultArg ordering compare
let column = defaultArg column 0
let reverse = defaultArg reverse false

let factor = if reverse then -1 else 1
let comparer (row1:string[]) (row2:string[]) =
factor * ordering row1.[column] row2.[column]

Array.sortInPlaceWith comparer rows
member x.Print() =
for row in rows do printfn "%A" row

// Example usage
let t = new Table([| [|"a"; "b"; "c"|]
[|""; "q"; "z"|]
[|"can"; "z"; "a"|] |])

printfn "Unsorted"; t.Print()

t.Sort()
printfn "Default sort"; t.Print()

t.Sort(column=2)
printfn "Sorted by col. 2"; t.Print()

t.Sort(column=1)
printfn "Sorted by col. 1"; t.Print()

t.Sort(column=1, reverse=true)
printfn "Reverse sorted by col. 1"; t.Print()

t.Sort(ordering=fun s1 s2 -> compare s2.Length s1.Length)
printfn "Sorted by decreasing length"; t.Print()</lang>


=={{header|J}}==
=={{header|J}}==