这个C#复制对象例程如何转换为F#?

Aar*_*ide 3 c# f# c#-to-f#

我认为我编写的用于复制对象属性树的代码非常实用 - F#版本是否带来了另一层次的简洁性?

public static class CopyUtility
{
    public static void Copy(object source, object target)
    {
        (
            from s in Properties(source)
            from t in Properties(target)
            where Matches(s, t)
            select Action(t, target, s.GetValue(source, null))
        )
        .ToList().ForEach(c => c());
    }

    static IEnumerable<PropertyInfo> Properties(object source)
    {
        return source.GetType().GetProperties().AsEnumerable();
    }

    static bool Matches(PropertyInfo source, PropertyInfo target)
    {
        return source.Name == target.Name;
    }

    static Action Action(PropertyInfo source, object target, object value)
    {
        if (value.GetType().FullName.StartsWith("System."))
            return () => source.SetValue(target, value, null);
        else
            return () => Copy(value, source.GetValue(target, null));
    }
}
Run Code Online (Sandbox Code Playgroud)

Phi*_*ord 12

这是转换为F#的C#复制功能:

module CopyUtility

let rec copy source target =
    let properties (x:obj) = x.GetType().GetProperties()
    query {
        for s in properties source do
        join t in properties target on (s.Name = t.Name)
        select s }
    |> Seq.iter (fun s ->
        let value = s.GetValue(source,null)
        if value.GetType().FullName.StartsWith("System.") 
        then s.SetValue(target, value, null)            
        else copy value (s.GetValue(target,null))
    )
Run Code Online (Sandbox Code Playgroud)

轻巧的语法

F#使用轻量级语法,其中空格很重要,这减少了大括号占用的行数.我在C#代码中计算了28行,而在F#代码中计算了13行.

类型推断

F#copy函数只需要一个类型的注释.与C#一样,F#是一种静态类型语言,但F#的类型推断不仅限于局部变量.

嵌套函数

F#支持嵌套函数,允许properties在Copy函数体内定义函数.这也可以通过定义类型的lambda函数用C#来完成,Func<object,IEnumerable<PropertyInfo>>但它相当简洁.

查询语法

F#3的查询表达式提供了类似于C#中LINQ的简洁语法.

流水线

F#管道转发运算符(|>)使函数调用能够作为连续操作链接在一起,通常不需要临时变量声明.