I am just learning F # and converting a library of C # extension methods to F #. I am currently working on the implementation of the ConvertFirstLetterToUppercase function based on the C # implementation below :
public static string ConvertFirstLetterToUppercase(this string value) { if (string.IsNullOrEmpty(value)) return value; if (value.Length == 1) return value.ToUpper(); return value.Substring(0, 1).ToUpper() + value.Substring(1); }
F # implementation
[<System.Runtime.CompilerServices.ExtensionAttribute>] module public StringHelper open System open System.Collections.Generic open System.Linq let ConvertHelper (x : char[]) = match x with | [| |] | null -> "" | [| head; |] -> Char.ToUpper(head).ToString() | [| head; _ |] -> Char.ToUpper(head).ToString() + string(x.Skip(1).ToArray()) [<System.Runtime.CompilerServices.ExtensionAttribute>] let ConvertFirstLetterToUppercase (_this : string) = match _this with | "" | null -> _this | _ -> ConvertHelper (_this.ToCharArray())
Can someone show me a more concise implementation using the more natural F # syntax?
string f #
Norman h
source share