Domanda

stiamo cercando di costruire il campione Haskell-MaybeMonad da http: //www.haskell .org / all_about_monads / html / maybemonad.html in F #.

L'idea è quella di cercare un mailAddress in due dizionari. Se una delle due ricerche restituisce un risultato guardiamo il terzo.

let bindM x k =
    match x with
    | Some value -> k value
    | None   -> None

let returnM x = Some x

type MaybeBuilder() =
     member this.Bind(x, k) = bindM x k
     member this.Return(x)  = returnM x
     member this.ReturnFrom(x) = x
     member this.Delay(f)   = f()

let maybe = MaybeBuilder()

//Sample dictionaries
let fullNamesDb = 
    [("Bill Gates", "billg@microsoft.com")    
     ("Bill Clinton", "bill@hope.ar.us")
     ("Michael Jackson", "mj@wonderland.org")
     ("No Pref Guy", "guy@nopref.org")]
       |> Map.ofList

let nickNamesDb =
    [("billy", "billg@microsoft.com")
     ("slick willy", "bill@hope.ar.us")
     ("jacko", "mj@wonderland.org") ]
        |> Map.ofList

let prefsDb =
    [("billg@microsoft.com", "HTML")
     ("bill@hope.ar.us", "Plain")
     ("mj@wonderland.org", "HTML")]
        |> Map.ofList


let mplus m1 m2 = if m1 <> None then m1 else m2
let (+) = mplus

let lookUp name = maybe {
    let! combined = fullNamesDb.TryFind name + nickNamesDb.TryFind name
    return! prefsDb.TryFind combined
}

let billGatesPref = lookUp "Bill Gates" |> printfn "%A" // Some "HTML"
let billyPref = lookUp "billy" |> printfn "%A" // Some "HTML"
let billClintonPref = lookUp "Bill Clinton" |> printfn "%A" // Some "Plain"
let steffenPref = lookUp "Steffen" |> printfn "%A" // None
let noPref = lookUp "No Pref Guy" |> printfn "%A" // None

System.Console.ReadKey() |> ignore

Il problema è che eseguiamo la seconda ricerca, anche se il primo restituisce un risultato. La cosa bella di Haskell è qui, che valuta pigro. Ora siamo alla ricerca di qualcosa di simile in F #. Abbiamo provato il seguente ma sembra brutto e sembra rompere l'idea di incapsulare la logica forse nel generatore:

let mplus m1 m2 = if m1 <> None then m1 else m2()
let (+) = mplus

let lookUp name = maybe {
    let! combined = fullNamesDb.TryFind name + fun _ -> nickNamesDb.TryFind name
    return! prefsDb.TryFind combined
}

C'è una soluzione migliore?

Saluti,   forki

È stato utile?

Soluzione

È possibile implementare metodi aggiuntivi Run / Combina in modo MaybeBuilder risultato sarà il seguente:

let bindM x k =
match x with
| Some value -> k value
| None   -> None

let returnM x = Some x

type MaybeBuilder() =
    member this.Bind(x, k) = bindM x k
    member this.Return(x)  = returnM x
    member this.ReturnFrom(x) = x
    member this.Delay(f)   = f
    member this.Combine(a, b) = if Option.isSome a then a else b()
    member this.Run(f) = f()

let maybe = MaybeBuilder()

//Sample dictionaries (the same with original sample)
let fullNamesDb = ... 
let nickNamesDb = ...
let prefsDb = ....

let lookUp name = 
    let findName m = maybe {
        let! v = Map.tryFind name m
        return! prefsDb.TryFind v 
        }

    maybe {
        return! findName fullNamesDb
        return! findName nickNamesDb 
    }


let billGatesPref = lookUp "Bill Gates" |> printfn "%A" // Some "HTML"
let billyPref = lookUp "billy" |> printfn "%A" // Some "HTML"
let billClintonPref = lookUp "Bill Clinton" |> printfn "%A" // Some "Plain"
let steffenPref = lookUp "Steffen" |> printfn "%A" // None
let noPref = lookUp "No Pref Guy" |> printfn "%A" // None

Altri suggerimenti

C'è sempre Lazy , che è effettivamente quello che avete qui ma con sintassi diversa:

let mplus m1 (m2 : Lazy<'a option>) =
    match m1 with
    | Some _ as m -> m
    | None -> m2.Force()

let (+) = mplus

let lookUp name = maybe {
    let! combined = fullNamesDb.TryFind name + lazy (nickNamesDb.TryFind name)
    return! prefsDb.TryFind combined
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top