F#函数的交互式传递参数数组 [英] F# Interactive Pass ParamArray of Functions
问题描述
我正在尝试在 James Hugard's 帖子上进行扩展
I'm trying to expand on James Hugard's post How do I plot a data series in F#? and I'm running into a glitch when using a variable number of function arguments. If I specifically name out the functions for Hugard's LineChartForm like in this example, the code works as expected when loaded into the F# Interactive Interpreter:
(* This code is based off of a Stack Overflow post by
James Hugard @
https://stackoverflow.com/questions/3276357/how-do-i-plot-a-data-series-in-f
*)
module HuggardPlot
#r "System.Windows.Forms.DataVisualization"
open System.Windows.Forms
open System.Windows.Forms.DataVisualization.Charting
type LineChartForm( title, minX, maxX, func1, func2) =
inherit Form( Text=title )
let chart = new Chart(Dock=DockStyle.Fill)
let area = new ChartArea(Name="Area1")
(* Add the first plot Hugard style *)
let series = new Series()
do series.ChartType <- SeriesChartType.Line
do for i in minX .. maxX do
series.Points.AddXY(i, func1(i)) |> ignore
do series.ChartArea <- "Area1"
do chart.Series.Add( series )
(* Add the second plot Hugard style *)
let series2 = new Series()
do series2.ChartType <- SeriesChartType.Line
do for i in minX .. maxX do
series2.Points.AddXY(i, func2(i)) |> ignore
do series2.ChartArea <- "Area1"
do chart.Series.Add( series2 )
(* Add area1 to the plot *)
do chart.ChartAreas.Add(area)
do base.Controls.Add( chart )
(* Convenience method to make it easier to plot functions *)
let plotTwoFunctions minX maxX f1 f2 =
let LCF = new LineChartForm("lines", minX, maxX, f1, f2);
LCF.Show();
On the other hand, if I try to use the technique in the Parameter Arrays section of http://msdn.microsoft.com/en-us/library/dd233213.aspx to pass a variable number of functions via a ParamArray with the following code:
(* This code is based off of a Stack Overflow post by
James Hugard @
https://stackoverflow.com/questions/3276357/how-do-i-plot-a-data-series-in-f
*)
module HuggardPlot
#r "System.Windows.Forms.DataVisualization"
open System
open System.Windows.Forms
open System.Windows.Forms.DataVisualization.Charting
type LineChartForm(
title,
minX,
maxX,
[<ParamArray>] funcs: Object[]) =
inherit Form( Text=title )
let chart = new Chart(Dock=DockStyle.Fill)
let area = new ChartArea(Name="Area1")
do for func in funcs do
(* Add the first plot Hugard style *)
let series = new Series()
do series.ChartType <- SeriesChartType.Line
do for i in minX .. maxX do
series.Points.AddXY(i, func(i)) |> ignore
do series.ChartArea <- "Area1"
do chart.Series.Add( series )
(* Add area1 to the plot *)
do chart.ChartAreas.Add(area)
do base.Controls.Add( chart )
(* Convenience method to make it easier to plot functions *)
let plotTwoFunctions minX maxX f1 f2 =
let LCF = new LineChartForm("lines", minX, maxX, f1, f2);
LCF.Show();
我在第27行出现错误
series.Points.AddXY(i, func(i)) |> ignore
指出此值不是函数,不能应用",在"func"下呈红色波浪状.
I get an error on line 27
series.Points.AddXY(i, func(i)) |> ignore
stating "This value is not a function and cannot be applied" with a red squiggly under "func".
我怀疑这与我使用[<ParamArray>] funcs: Object[]
作为Hugard先生的LineChartForm Type的参数定义有关.
I suspect this has something to do with the fact that I am using [<ParamArray>] funcs: Object[]
as a parameter definition to Mr. Hugard's LineChartForm Type.
我应该将[<ParamArray>] funcs: Object[]
更改为什么,以便第二个代码示例的第27行将"func"识别为函数?
What should I change [<ParamArray>] funcs: Object[]
to so line 27 of the second code example will recognize "func" as a function?
推荐答案
如果要创建采用params
函数数组的方法,则需要将参数定义为[<ParamArray>] funcs: (float -> float)[]
.在您的原始版本中,类型为Object[]
,表示数组的各个元素都是对象-如果将元素的类型更改为函数,则F#将识别出可以调用它们.
If you want to create a method that takes params
array of functions, then you need to define the parameter as [<ParamArray>] funcs: (float -> float)[]
. In your original version, the type was Object[]
meaning that individual elements of the array were objects - if you change the type of elements to functions, then F# will recognize that you can call them.
但是,如果您只是对绘制F#数据感兴趣,那么WinForms DataVisualization库已经有了一个很好的包装器,称为文档(如果您使用该库的最新版本,则FSharpChart
已重命名为Chart
).
However, if you're simply interested in charting F# data, then there is already a good wrapper for the WinForms DataVisualization library called F# Chart and available on GitHub. There is also a comprehensive documentation available on MSDN (if you use the latest version of the library, then FSharpChart
has been renamed to just Chart
).
要创建一个在指定范围内比较两个函数的绘图,只需编写以下内容:
To create a plot comparing two functions over a specified range, you can simply write:
let plotTwoFunctions minX maxX f1 f2 =
Chart.Combine
[ Chart.Line [ for x in minX .. maxX -> x, f1 x ]
Chart.Line [ for x in minX .. maxX -> x, f2 x ] ]
这将创建两个单独的折线图(具有由两个函数生成的数据),然后使用Chart.Combine
将它们组合成一个图表.
This creates two individual line charts (with data generated by the two functions) and then combines them into a single chart using Chart.Combine
.
这篇关于F#函数的交互式传递参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!