【问题标题】:F# nested recordsF# 嵌套记录
【发布时间】:2021-06-06 08:05:24
【问题描述】:
  1. 嵌套记录

记录是否类似于字典,它是带有名称的对象树? 或者记录只是简单类型的列表

let r = { b = { a = 2 } } // is this possible? if not, how to achieve? is this useful in f#?

对我来说,有区别的工会有可能实现类似的行为(下面的代码)


// Discriminated union for a card's suit 

type Suit = | Heart | Diamond | Spade | Club 
let suits = [Heart; Diamond; Spade; Club]
suits

// Discriminated union for playing cards

type PlayingCard = 
    | Ace of Suit 
    | King of Suit 
    | Queen of Suit
    | Jack of Suit 
    | ValueCard of int * Suit

// generating a deck of cards

let deckOfCards = 
    [
        for suit in [Spade; Club; Heart; Diamond] do   
            yield Ace(suit)
            yield King(suit)
            yield Queen(suit)
            yield Jack(suit)
            for value in 2..10 do
                yield ValueCard(value, suit)
    ]

它有点类似于 python 或 idk 中的字典。下面的代码是虚拟的


type Customer = 
    {
        FirstName : string
        Contacts = 
            {
                WorkPhone : string
                MobilePhone : string
            }
        
    }

【问题讨论】:

    标签: functional-programming f#


    【解决方案1】:

    可以使用anonymous records创建嵌套类型:

    type Customer = 
        {
            FirstName : string
            Contacts :
                {|
                    WorkPhone : string
                    MobilePhone : string
                |}
        }
    
    let customer =
        {
            FirstName = "John"
            Contacts =
                {|
                    WorkPhone = "123-456-7890"
                    MobilePhone = "234-567-8901"
                |}
        }
    

    【讨论】:

      【解决方案2】:

      是的,您可以有嵌套记录,但就像在您的示例中使用可区分联合一样,您需要为嵌套类型命名:

      type CustomerConracts =
          {
              WorkPhone : string
              MobilePhone : string
          }
      
      type Customer = 
          {
              FirstName : string
              Contacts: CustomerConracts       
          }
      
      let c = { FirstName = "John", Contacts = { WorkPhone = "123", Mobile phone = "098" } }
      

      【讨论】:

      • 您可以使用匿名记录,因此您实际上不必命名嵌套类型。
      【解决方案3】:

      您可以在代码中看到一些模式,但记录与字典不同。您可以将它们视为具有强类型公共属性的类。如果您需要创建字典,则必须使用可用的地图/字典类之一或实现自己的。以 Map 类型为例。 https://fsharp.github.io/fsharp-core-docs/reference/fsharp-collections-fsharpmap-2.html

      type Contact = 
          {
              WorkPhone : string
              MobilePhone : string
          }
      
      type Customer = 
          {
              FirstName : string
              Contacts : Contact
          }
      
      
      let cust : Customer = 
          { 
            FirstName = "Joe"
            Contacts = { WorkPhone="1800131313"; MobilePhone="0863331311" } 
          }
      
      

      上面的代码显示您可以嵌套记录类型。除了按照@brianberns 的建议使用匿名记录外,您还可以声明您计划嵌套的数据类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-10
        • 1970-01-01
        • 2021-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多