简体   繁体   English

在 F# 中创建自定义类型列表并创建该列表的两个序列

[英]Create a list of custom type in F# and create two sequences of that list

I have created my own type in F# called Accounts and I have then created objects for each account.我在 F# 中创建了自己的类型,称为 Accounts,然后为每个帐户创建了对象。

type Account() =   

  let AccountNumber = ""
  let mutable Balance:float = 0.0

Every account has two fields, AccountNumber (string) and Balance (float).每个帐户都有两个字段,AccountNumber(字符串)和 Balance(浮点数)。

I have then created an object for every account that holds the AccountName and the Balance.然后,我为每个持有 AccountName 和 Balance 的账户创建了一个 object。

let acc1 = new Account()  
acc1.Insert("John",10.0)

let acc2 = new Account()  
acc2.Insert("Mike",50.0)

How do I create a list that holds each account (object)?如何创建一个包含每个帐户(对象)的列表? I have tried the following:我尝试了以下方法:

let AccountList : Account list = [acc1;  acc2 ;  acc3; acc4 ; acc5; acc6]
let AccountList : Account obj list = [acc1;  acc2 ;  acc3; acc4 ; acc5; acc6]

I cannot solve the problem using the above method because I have to create two sequences from the list:我无法使用上述方法解决问题,因为我必须从列表中创建两个序列:

Sequence 1: All accounts with a balance greater or equal to zero and less than 50 Sequence 2: All accounts with a balance above 50序列 1:余额大于等于 0 且小于 50 的所有账户 序列 2:余额大于 50 的所有账户

How do I create a list of my custom type in F# and how do I create two sequences of that list?如何在 F# 中创建我的自定义类型的列表,以及如何创建该列表的两个序列?

It is not clear what exactly are you struggling with.目前尚不清楚你到底在挣扎什么。 However, the following simple example should illustrate most of the key ideas that you probably need to use.但是,以下简单示例应该说明您可能需要使用的大多数关键思想。 First, here is a small version of your Account class (note that I would normally use an immutable record, but I kept it the way you did it):首先,这是您的Account class 的小版本(请注意,我通常会使用不可变记录,但我按照您的方式保留它):

type Account(balance:float) =   
  let mutable balance = balance
  member x.Balance = balance
  member x.Add(difference) = 
    balance <- balance + difference

I do not see what issue you have with creating the list.我看不出您在创建列表时遇到了什么问题。 The following works just fine:以下工作正常:

let acc1 = Account(100.0)
let acc2 = Account(10.0)

let accountList = [acc1; acc2]

Now, to answer the question about finding accounts with balance over 50, you can use the List.filter function to create a new filtered list:现在,要回答有关查找余额超过 50 的帐户的问题,您可以使用List.filter function 创建一个新的过滤列表:

let above50 = 
  accountList |> List.filter (fun acc ->
    acc.Balance > 50.0)

EDIT If you wanted to use a record instead, then you would define the type as:编辑如果您想改用记录,那么您可以将类型定义为:

type Account = { Balance : float }

And create a value using:并使用以下方法创造价值:

let acc1 = { Balance = 100.0 }

So I created this answer for the other one but I was waiting on the comment to see if I would answer.所以我为另一个人创建了这个答案,但我正在等待评论,看看我是否会回答。 And the homework like aspect of this:)和家庭作业这样的方面:)

So if you have criteria that bucket an account and want to do that in a single pass, you might want to look at groupBy .因此,如果您有存储帐户的标准并希望一次性完成,您可能需要查看groupBy Here I use a boolean because there are only 2 possibilities but numbers or a discriminated union are good candidates.在这里,我使用boolean因为只有两种可能性,但数字或有区别的联合是很好的候选者。

open System

type Account(accountNumber:string, startingBalance:Int64) =
    let mutable balance = startingBalance

    member _.Balance = balance
    member _.Deposit amount = balance <- balance + amount
    member _.Withdraw amount = balance <- balance - amount
    override _.ToString() = accountNumber

let allAccounts = [Account("ABC1", 10L); Account("ABC2", 50L)]

let grouped = allAccounts |> List.groupBy (fun a -> a.Balance >= 50L) |> Map.ofList

let under50 = grouped |> Map.tryFind false |> Option.defaultValue []
let overIncl50 = grouped |> Map.tryFind true |> Option.defaultValue []

printfn "Under: %A" under50
printfn "Over: %A" overIncl50

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM