C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Let: In the "let result" statement we pipeline the three methods together with a name argument (with value "visitor").
Result: The three functions handle the string in order and the resulting text has been processed as expected.
PrintfnF# program that uses strings
// Three string manipulation functions.
let uppercase (x : string) = x.ToUpper()
let addGreeting (x : string) = "Hi " + x
let addComma (x : string) = x + ","
// Used to compute result.
let name = "visitor"
// Apply three string functions with pipeline operator on the name.
let result =
name
|> uppercase
|> addGreeting
|> addComma
// Write result.
printfn "%A" result
Output
"Hi VISITOR,"
Match: We use pattern-matching in the upperMap function to determine how to modify the letter.
MatchTip: With String.map we have an effective way to translate strings. For something like a ROT13 transformation this is ideal.
F# program that uses String.map
let value = "abcxyz"
// This function only uppercases 2 letters.
let upperMap = fun c ->
match c with
| 'a' -> 'A'
| 'x' -> 'X'
| _ -> c
// Apply our upperMap function with String.map.
let result = String.map upperMap value
printfn "%A" result
Output
"AbcXyz"