Code Snippet
1
2
3
4
5
6
7
8
9
10
11
| let leftpad s n c =
let l = s |> String.length // string length from O(N) to O(1)
let c' = match c with | None -> "." | Some v -> v
match l <= n with
| true -> String.replicate (n-l) c' + s
| false -> s
leftpad "foo" 6 None
leftpad "foo" 3 None
leftpad "fooBar" 3 None
leftpad "foo" 6 (Some "?")
|
Code output:
References:
- Implementing Left-Pad in 13 Languages: