【发布时间】:2020-11-04 19:19:46
【问题描述】:
完全是新手问题。
我仍在尝试掌握 F# 中的“不可变”列表。
假设我有一个“访问”定义为:
module Visit =
type Model =
{
Name: string
}
let init row col cel =
{
Name = sprintf "My Name is row: %d col: %d cel: %d" row col cel
}
现在我定义一个“单元”,它可能会访问一次,也可能不会访问:
module Cell =
type Model =
{
Visit: Visit.Model option
}
let setVisit m =
{ m with Visit = Some( Visit.init 9 9 9) }
最后我定义了一个包含单元格列表的“列”:
module Column =
type Model =
{
Appointments: Cell.Model list
}
let updateCell (m:Model) =
let newList = m.Appointments |> List.mapi (fun index cell -> if index = 2 then Cell.setVisit cell else cell)
{m with Appointments = newList }
在 Column 模块中,“updateCell”函数被连接为调用第 3 个单元格的 Cell.setVisit。我的意图是替换第 3 个单元格持有的“访问”的“名称”。我的简单问题是:
- 这是正确的方法吗?
- 如果我要替换约会列表,这不会改变保存约会列表的“列”吗? (Column 是不可变的,对吧?)。
对不起,我的困惑。
TIA
【问题讨论】:
-
我想说,如果您正在学习函数式编程,请尝试使用不可变的数据结构来做到这一点,从而重新连接您的大脑,使其以这种方式思考。如果您真的需要这些额外的 pico 秒,那么可以,使用数组,或者您可以开始研究纯粹的替代方案。
标签: f#