小编Sre*_*ree的帖子

从列表映射到另一种类型 - 有没有更好的方法来做到这一点?

let claimsList  =  
          [ {|cType = "uid"; cValue="1"|};
            {|cType = "name"; cValue="N1"|}; 
            {|cType = "sid"; cValue="sid1"|}; 
            {|cType = "email"; cValue="email@N1.com"|}; 
            {|cType = "company"; cValue="Company1"|}]

let email = claimsList
          |> List.tryFind(fun c -> c.cType = "email")
          |> Option.map(fun c -> c.cValue)
let company = claimsList
            |> List.tryFind(fun c -> c.cType = "company")
            |> Option.map(fun c -> c.cValue)
let userId = claimsList
           |> List.tryFind(fun c -> c.cType = "userId")
           |> Option.map(fun c -> c.cValue)

(email, userId, company)
Run Code Online (Sandbox Code Playgroud)

我不喜欢我多次迭代列表并且看起来很复杂的事实。有没有办法简化这个?

f#

5
推荐指数
1
解决办法
50
查看次数

标签 统计

f# ×1