代码之家  ›  专栏  ›  技术社区  ›  SantiClaus

如何使用映射函数?

f#
  •  0
  • SantiClaus  · 技术社区  · 8 年前

    所以我有许多已经定义的类型,以及将数据映射到这些记录/类型中的函数。现在,我需要将每个记录/类型的内容映射到一个“主”记录中,该记录将基于一个键(在本例中为状态)包含以前记录中的所有内容。我不知道该怎么做,但我在下面尝试了一下,并提供了更多的代码来提供上下文。有什么建议吗?如果您需要更多信息,请发表评论。

    我有这样定义的类型:

    type StateEdu = 
        { State : string
          Education : int
          Income : float }
    
    type StateFamily =
        { State : string
          PctMoreThan4Children : float
          PctFamilyMorethan3 : float }
    

    但现在我需要这样做(当我运行它时,这不起作用):

    let stateall = statemap.Keys
    
    let statedatamap =
     stateall
     |> Seq.map (fun state ->
        state,
        {State = state
         StateEdu = StateEdu.[state]
         StateFamily = StateFamily.[state]
         })
    |> Map.ofSeq
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   s952163    8 年前

    (我添加了类型并修正了一些错误)

    type StateEdu = 
        { State : string
          Education : int
          Income : float }
    
    type StateFamily =
        { State : string
          PctMoreThan4Children : float
          PctFamilyMorethan3 : float }
    
    //I added this new type so this is value of the final map.    
    type State2 = 
        { State : string
          StateEdu : StateEdu
          StateFamily : StateFamily }
    
    //Instead providing the csv it's better to minimize the example, so here are the two record lists.      
    let stateEdu = 
      [{State = "TX"; Income = 51522.; Education = 0}
       {State = "AL"; Income = 6481.; Education = 1}
       {State = "MO"; Income = 78921.; Education = 1}
       {State = "TN"; Income = 12000.; Education = 4}
       {State = "PA"; Income = 79850.; Education = 2}
       {State = "NY"; Income = 79215.; Education = 1}
       {State = "CA"; Income = 79045.; Education = 2}]
    
    let datafamily = 
      [{State = "TX"; PctMoreThan4Children = 51.52; PctFamilyMorethan3 = 65.0}
       {State = "AL"; PctMoreThan4Children = 64.00; PctFamilyMorethan3 = 51.4}
       {State = "MO"; PctMoreThan4Children = 78.92; PctFamilyMorethan3 = 25.1}
       {State = "TN"; PctMoreThan4Children = 12.00; PctFamilyMorethan3 = 62.1}
       {State = "PA"; PctMoreThan4Children = 8.50; PctFamilyMorethan3 = 41.2}
       {State = "NY"; PctMoreThan4Children = 25.15; PctFamilyMorethan3 = 31.0}
       {State = "CA"; PctMoreThan4Children = 79.5; PctFamilyMorethan3 = 50.5}]
    
    
    let stateedu =
         stateEdu
         |> Seq.map (fun x -> x.State,x)
         |> Map.ofSeq
    
    let datafam =
         datafamily
         |> Seq.map (fun x -> x.State,x)
         |> Map.ofSeq
    
    //This is one way to quickly extract the keys
    let stateall = stateedu |> Map.toSeq |> Seq.map fst  
    
    //We go through all the keys
    let statedatamap =
     stateall
     |> Seq.map (fun state ->
        state,
        {State = state //this is the State2type
         StateEdu = stateedu.[state] 
         StateFamily = datafam.[state]
         })
    |> Map.ofSeq
    

    stateedu=state=“al”; pctmorethan4children=64.0; (“CA”,state=“CA”; stateedu=state=“ca”; stateFamily=state=“ca”; pctfamilymorethan3=50.5;); (“NY”,state=“NY”; 教育=1; (“pa”,state=“pa”; stateFamily=state=“pa”; (“tn”,state=“tn”; 教育=4; PCTMorethan4儿童=12.0; (“tx”,state=“tx”; 收入=51522.0;; PCTMorethan4儿童=51.52;

    statedu.[state].Education 例如。