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:

> 
val leftpad : s:string -> n:int -> c:string option -> string

> val it : string = "...foo"
> val it : string = "foo"
> val it : string = "fooBar"
> val it : string = "???foo"

References: