Coding Practice for F#

后端 未结 4 1153
礼貌的吻别
礼貌的吻别 2021-02-04 10:44

I have been dabbling with F# in Visual Studio 2010. I am a developer with more code/architecture design experience in object-oriented languages such as C# and Java.

T

4条回答
  •  轮回少年
    2021-02-04 11:35

    Most of the time deforesting without a specific reason, generally performance, is a bad idea. Which one of these do you see as easier to read and less error prone? Deforesting taken out of context just adds complexity and/or coupling to your code.

    let createPeople filter ppl =
        ppl 
        |> List.mapi (fun i x -> (i, x)) 
        |> List.filter filter
        |> List.map createPerson
    
    let createPeople filter ppl =
        [ for (index, name) in ppl |> List.mapi (fun i x -> (i, x)) do
              if filter (index, name) then
                  yield createPerson (index, string) ]
    
    let createPeople filter ppl = 
        (ppl |> List.mapi (fun i x -> (i, x)), []) 
        ||> List.foldBack (fun P acc -> if filter P then (createPerson P)::acc else acc) 
    

    Once you get used to the syntax function composition allows you to drop ppl.

    let createPeople filter =
        List.mapi (fun i x -> (i, x)) 
        >> List.filter filter
        >> List.map createPerson
    

    All of these use tupled data.

    let filter (id, name) = 
        id % 2 = 1
    
    let allFilter (id, name) = 
        true
    
    let createPerson (id, name) = 
        ()
    

提交回复
热议问题