简体   繁体   English

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

[英]How would this C# copy object routine translate to F#?

I think this code I wrote to copy object property trees is pretty functional - does the F# version bring another level of succinctness? 我认为我编写的用于复制对象属性树的代码非常实用 - 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));
    }
}

Here's the C# copy function translated to F#: 这是转换为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))
    )

Light syntax 轻巧的语法

F# uses a light syntax where white space is significant, which reduces the number of lines taken by curly braces. F#使用轻量级语法,其中空格很重要,这减少了大括号占用的行数。 I counted 28 lines in the C# code versus 13 in the F# code. 我在C#代码中计算了28行,而在F#代码中计算了13行。

Type inference 类型推断

The F# copy function requires only a single type annotation. F# copy功能只需要一个类型的注释。 Like C#, F# is a statically typed language, however F#'s type inference is not limited to local variables . 与C#一样,F#是一种静态类型语言,但F#的类型推断不仅限于局部变量

Nested functions 嵌套函数

F# supports nested functions allowing the properties function to be defined within the body of the Copy function. F#支持嵌套函数,允许在Copy函数的主体内定义properties函数。 This could be also be done with C# by defining a lambda function of type Func<object,IEnumerable<PropertyInfo>> but it is considerably less succinct. 这也可以通过定义类型为Func<object,IEnumerable<PropertyInfo>>lambda函数来使用C#来完成,但它相当简洁。

Query syntax 查询语法

F# 3's query expressions provide a succinct syntax similar to LINQ in C#. F#3的查询表达式提供了类似于C#中LINQ的简洁语法。

Pipelining 流水线

The F# pipe forward operator (|>) enables function calls to be chained together as successive operations, often removing the need for temporary variable declarations. F# 管道转发运算符 (|>)使函数调用能够作为连续操作链接在一起,通常不需要临时变量声明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM