【发布时间】:2014-03-15 19:27:21
【问题描述】:
F# 新手在这里并试图找到做事的“功能性方式”。
我有一个记录数组
type Node = {Value: int; Height: int; Width: int}
let dummyData = [|
{Value = 1000; Height = 200; Width = 200}
{Value = 500; Height = 1000; Width = 500}
{Value = 2000; Height = 500; Width = 100}
|]
我正在寻找记录中每个标签的最小值。对于每个标签,我可以找到写一个函数来使用 Array.minBy 找到最小值,例如:
let findMinHeight xs =
let item =
xs |> Array.minBy (fun x -> x.Height)
item.Height
但这感觉很麻烦,因为在这个例子中,我需要编写基本相同的函数 3 次,每个标签一次。 是否可以构造一个函数,该函数将数组与特定标签一起接收并返回适当的最小值?
我希望能够写出类似的东西:
let minHeight = findMin dummyData Height
let minWidth = findMin dummyData Width
let minValue = findMin dummyData Value
【问题讨论】:
标签: lambda f# functional-programming record