F#
let s = "bugs 42 bunny"
s.Count(fun c -> Char.IsLetter(c))
s.Where(fun c -> Char.IsLetter(c)).ToArray()
s.Where(Char.IsLetter).ToArray()
s.Count(Char.IsLetter) // error为什么只有最后一行无法编译:
错误FS0002:这个函数需要太多的参数,或者在不需要函数的上下文中使用
发布于 2014-05-30 18:40:53
我认为这是类型推断wrt成员重载的边缘情况。Count和Where的不同之处在于前者有两个重载,具有不同数量的参数。
您可以通过指定从F#函数到System.Func<_, _>的转换来解决这个问题。
s.Count(Func<_, _>(Char.IsLetter))当然,它甚至比相应的版本更丑:
s.Count(fun c -> Char.IsLetter(c))您可以在https://visualfsharp.codeplex.com/workitem/list/basic上提交一个bug,以便在F# vNext中修复它。
注意,在F#中,您不经常使用Linq函数。你可以这样做:
s |> Seq.sumBy (fun c -> if Char.IsLetter c then 1 else 0)或
s |> Seq.filter Char.IsLetter |> Seq.length发布于 2014-05-30 18:40:47
https://stackoverflow.com/questions/23960985
复制相似问题