(continued from phosphorescence: Tips for F# pattern match (2))
- If a function takes one argument
- And if that function uses pattern matching with same one argument
In this case, we can syntax sugar with function keyword. With using this keyword, we can omit both function argument and pattern matching keyword.
Before:
[<Literal>]
let Person_01_name = "Robert";;
let person_01_nickname = "Bob";;
[<Literal>]
let Person_02_name = "William";;
let person_02_nickname = "Bill";;
let greet name =
match name with
| Person_01_name -> printfn "Hello, %s" person_01_nickname
| Person_02_name -> printfn "Hello, %s" person_02_nickname
| x -> printfn "Hello, %s" x;;
After:
[<Literal>]
let Person_01_name = "Robert";;
let person_01_nickname = "Bob";;
[<Literal>]
let Person_02_name = "William";;
let person_02_nickname = "Bill";;
let greet =
function
| Person_01_name -> printfn "Hello, %s" person_01_nickname
| Person_02_name -> printfn "Hello, %s" person_02_nickname
| x -> printfn "Hello, %s" x;;
No comments:
Post a Comment