F# 模块还是类?
考虑到这两种方法: 方法1F# 模块还是类?,f#,functional-programming,F#,Functional Programming,考虑到这两种方法: 方法1 module DomainCRUD = let getWhere collection cond = ... module DomainService = let getByCustomerId f customerId = f(fun z -> z.CustomerId = customerId) // USAGE: let customerDomains = DomainCRUD.getWhere collection
module DomainCRUD =
let getWhere collection cond = ...
module DomainService =
let getByCustomerId f customerId =
f(fun z -> z.CustomerId = customerId)
// USAGE:
let customerDomains = DomainCRUD.getWhere collection
|> DomainService.getByCustomerId customerId
方法2
type DomainCRUD(collection) =
member x.GetWhere cond = ...
type DomainService(CRUD) =
member x.GetByCustomerId customerId =
CRUD.GetWhere(fun z -> z.CustomerId = customerId)
// USAGE:
let domainService = new DomainService(new DomainCRUD(collection))
let customerDomains = _domainService.GetByCustomerId(customerId)
哪一个最适合函数式编程?我假设方法1
会,但是每次调用DomainCRUD.GetWhere collection
感觉有点多余
哪一种是最灵活、最“易读”的方法1,原因如下:
打开DomainCRUD
即可省略模块限定DomainCRUD.GetWhere
如果你打算使用所有来自F#only的方法,我会说方法1。如果您想查看典型的F#程序结构,可以检查或其他。例如(在Deedle中),在帧上有和包含大多数操作。几乎没有必要复制#1。类可以有curry成员函数。我不知道!谢谢你的提示。