2016-02-12 10 views
5

F # ha le proprie librerie di manipolazione delle stringhe?F # ha le sue librerie di manipolazione delle stringhe?

Mentre sto cercando di imparare F #, mi trovo a utilizzare i metodi System.string esistenti?

Devo fare questo?

Codice:

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

funzione Aggiornamento da:

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" };; 

a:

let formatPhone phoneNumber = 

    printf "+%i %s %s" phoneNumber.CountryCode 
         phoneNumber.Number.[0..3] 
         phoneNumber.Number.[4..8] 

formatPhone { CountryCode=44; Number="123456789" };; 
+2

creo così piccole funzioni * * anti-corruzione come 'getLeadingCharacters' tutto il tempo. Mi sembra perfettamente normale. –

+0

"Anti-corruzione"? –

+1

Adottato dal termine * strato anticorruzione * da [DDD] (http://amzn.to/WBCwx7). –

risposta

8

No, F # non ha uno specifico String library che duplica la libreria .NET. Ha un string module con funzioni stringa aggiuntive.

Sì, utilizzare le funzioni .NET.

Il fatto che F # possa utilizzare tutta la libreria .NET è una delle sue funzioni più forti. A prima vista sembra strano mescolare le funzioni usando i parametri al curry con le funzioni di .NET usando i parametri della tupla.

Ecco perché in NuGet verranno visualizzati pacchetti con estensione FSharp. per esempio. MathNet Numerics e MathNet Numerics FSharp. Queste sono funzioni wrapper che consentono l'uso idiatico di F # della libreria .NET.

Quando si cercano funzioni e metodi da utilizzare con F # uso spesso questo trucco. Per cercare .NET utilizzare class come parola chiave e per cercare il codice specifico F # utilizzare module come parola chiave.

Ad esempio:

Google: MSDN string class
Primo prodotto: String Class

Google: MSDN string module
Primo prodotto: Core.String Module (F#)

+0

Sei un patrimonio di conoscenza. Come posso seguirti? Ad esempio, YouTube, Blog, Podcast, ecc ... –

+2

Ricevo la maggior parte delle mie conoscenze seguendo le [migliori risposte di F #] (http://stackoverflow.com/tags/f%23/topusers) e leggendo la maggior parte delle nuove domande qui. Leggo molti articoli di ricerca su [CiteSeer] (http://citeseer.ist.psu.edu/index) e scrivo un sacco di codice. Ho passato gli ultimi mesi solo sulle basi delle reti neurali e mi sono imbattuto in problemi, anche se richiedono una settimana o più per risolverli. Recentemente ho imparato che uno dei modi migliori per eseguire il debug delle reti neurali è con le immagini visive e non guardando i valori di tutti i pesi e le distorsioni. –

Problemi correlati