F# 函数参数为null,即使传递了非null参数
这里是新手,很抱歉标题不好,我不知道还能用什么来形容它。F# 函数参数为null,即使传递了非null参数,f#,F#,这里是新手,很抱歉标题不好,我不知道还能用什么来形容它。 我有一个很奇怪的问题。以下是相关的代码片段: let calcRelTime (item :(string * string * string)) = tSnd item |>DateTime.Parse |> fun x -> DateTime.Now - x |> fun y -> (floor y.TotalMinutes).ToString() |&g
我有一个很奇怪的问题。以下是相关的代码片段:
let calcRelTime (item :(string * string * string)) =
tSnd item
|>DateTime.Parse
|> fun x -> DateTime.Now - x
|> fun y -> (floor y.TotalMinutes).ToString()
|>makeTriple (tFst item) (tTrd item) //makeTriple switches y & z. How do I avoid having to do that?
let rec getRelativeTime f (l :(string * string * string) list) =
match l with
| [] -> f
| x :: xs -> getRelativeTime (List.append [calcRelTime x] f) xs
我使用VisualStudio逐步介绍了它,它清楚地显示了getRelativeTime中的x是一个具有格式良好的日期时间字符串的3元组。但是,当我转到calcRelTime时,项为空。所有结果都返回一个包含原始datetime字符串的3元组,而不是一个总分钟数超过的元组。任何地方都没有其他错误,直到datetime字符串命中预期为整数字符串的函数
任何帮助都将不胜感激!(以及关于这些功能的任何其他F#风格提示/建议)。
项
为空,因为它尚未由其部分构造出来。F#编译器将元组参数编译为单独的实际(IL级别)参数,而不是一个类型为Tuple
的参数。如果您在ILSpy中查看编译后的代码,您将看到以下签名(使用C#语法):
也许如果你分享你的测试代码,我可以告诉你更多
最后,您所做的事情可以简单得多:您不需要自己编写递归循环,列表
模块中的许多函数已经为您完成了递归循环,您不需要接受元组,然后使用tFst
、tSnd
和tTrd
解构它,编译器可以为您执行以下操作:
let getRelativeTime lst =
let calcRelTime (x, time, y) =
let parsed = DateTime.Parse time
let since = DateTime.Now - parsed
let asStr = (floor since.TotalMinutes).ToString()
(x, asStr, y)
List.map calRelTime lst
函数的签名是val getRelativeTime:list:('a*string*'b)list->('a*'b*string)list
您可以将函数声明中的项
解构为(a、b、c)
,这样就不必使用函数tFst
、tSnd
和tTrd
列表模块有一个函数map
,它将一个函数应用于列表中的每个元素,并返回一个带有映射值的新列表
> getRelativeTime [] [("x","05/01/2015","y")]
val it : (string * string * string) list = [("x", "y", "17305")]
let getRelativeTime lst =
let calcRelTime (x, time, y) =
let parsed = DateTime.Parse time
let since = DateTime.Now - parsed
let asStr = (floor since.TotalMinutes).ToString()
(x, asStr, y)
List.map calRelTime lst
let getRelativeTime' list =
let calc (a, b, c) = (a, c, (floor (DateTime.Now - (DateTime.Parse b)).TotalMinutes).ToString())
list |> List.map calc