Czy F # ma własne biblioteki manipulacji ciągami?Czy F # ma własne biblioteki manipulacji ciągami?
Próbuję nauczyć się F #, używam istniejących metod System.string?
Czy powinienem to robić?
Kod:
open System
type PhoneNumber =
{ CountryCode:int
Number:string }
// b. Create a function formatPhone that accepts a PhoneNumber record and formats it to look like something like this: "+44 1234 456789"
let formatPhone phoneNumber =
let getLeadingCharacters (length:int) (text:string) =
text.Substring(0, length)
let getLastCharacters (length:int) (text:string) =
text.Substring(text.Length - length, length)
printf "+%i %s %s" phoneNumber.CountryCode
(phoneNumber.Number |> getLeadingCharacters 4)
(phoneNumber.Number |> getLastCharacters 6)
formatPhone { CountryCode=44; Number="123456789" };;
UPDATE
Updated funkcja od:
let formatPhone phoneNumber =
let getLeadingCharacters (length:int) (text:string) =
text.Substring(0, length)
let getLastCharacters (length:int) (text:string) =
text.Substring(text.Length - length, length)
printf "+%i %s %s" phoneNumber.CountryCode
(phoneNumber.Number |> getLeadingCharacters 4)
(phoneNumber.Number |> getLastCharacters 6)
formatPhone { CountryCode=44; Number="123456789" };;
do:
let formatPhone phoneNumber =
printf "+%i %s %s" phoneNumber.CountryCode
phoneNumber.Number.[0..3]
phoneNumber.Number.[4..8]
formatPhone { CountryCode=44; Number="123456789" };;
tworzę takie małe * funkcje antykorupcyjnych * jak 'getLeadingCharacters' cały czas. Wydaje mi się to zupełnie normalne. –
"Przeciw korupcji"? –
Przyjęto termin * warstwa przeciwdziałająca korupcji * z [DDD] (http://amzn.to/WBCwx7). –