简体   繁体   English

F#伤害了我的大脑......任何人都可以帮助将C#翻译成F#吗?

[英]F# hurts my brain… can anyone help translate C# to F#?

I found a quick project I thought would be perfect for learning F#. 我发现了一个我认为非常适合学习F#的快速项目。 However I just cannot wrap my brain around it for some reason. 然而,由于某种原因,我无法将我的大脑包裹起来。 After hours of tutorials and even some movies I still just... don't get it. 经过几个小时的教程,甚至一些电影,我仍然只是......不明白。

So I wanted to start versioning our stored procedures at work using Enterprise Manager "Generate Scripts" and wanted to blank out the script date. 所以我想使用Enterprise Manager“Generate Scripts”开始在工作中对我们的存储过程进行版本控制,并希望删除脚本日期。 I gave up and did it in C# but now I'm REALLY curious and am hoping for some insight. 我放弃了,并在C#中做到了,但现在我真的很好奇,我希望有一些见解。

I am not completely empty handed, here is my C# code: 我不是空手而归,这是我的C#代码:

string path = @"C:\Subversion\CompanyName\SQL\DBName";
string fileSpec = "*.sql";
string pattern = @"Script Date: \d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}";
string replace = "Script Date: ";

foreach (var file in Directory.EnumerateFiles(path, fileSpec))
{
    string content = File.ReadAllText(file);

    content = Regex.Replace(content, pattern, replace);

    File.WriteAllText(file, content, Encoding.Unicode);
}

I am guessing there is some cool looking 2-3 line solution in F#... I'd love to see it. 我猜在F#中有一些很酷的2-3线解决方案...我很乐意看到它。

Thx for the tips! Thx的提示! I have updated it to match what is being done below to make the visual comparison potentially more enlightening. 我更新了它以匹配下面的内容,使视觉比较更具启发性。 Also great comments below. 下面还有很棒的评论。

let path = @"C:\Subversion\CompanyName\SQL\DBName"
let fileSpec = "*.sql"
let pattern = @"Script Date: \d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}"
let replace = "Script Date: "

Directory.EnumerateFiles (path, fileSpec)
|> Seq.iter (fun file ->
    let content = Regex.Replace (File.ReadAllText file, pattern, replace)
    File.WriteAllText (file, content, Encoding.Unicode))

Note that your C# code is ( EDIT : was) also far more verbose than necessary. 请注意,您的C#代码编辑 :WAS)也远远超过必要的繁琐。

Pretty much all you need to do is to replace type name in variable declarations (where you could write var in C#) with the let keyword, remove all semicolons and curly braces and replace foreach with 您需要做的就是使用let关键字替换变量声明中的类型名称(可以在C#中编写var ),删除所有分号和花括号,并将foreach替换为
for file in files do . for file in files do You're also using some namespaces, so in F#, you need something like: 您还使用了一些名称空间,因此在F#中,您需要以下内容:

open System.IO
open System.Text.RegularExpressions

You're using one mutable variable ( content ), but you don't really need to mutate it. 你正在使用一个可变变量( content ),但你真的不需要改变它。 You can just use a different variable (such as newContent ) to store the result of replacement. 您可以使用其他变量(例如newContent )来存储替换结果。 So, the middle part will be: 所以,中间部分将是:

let content = File.ReadAllText(file)  // No need for a stream reader here!

let pattern = @"Script Date: \d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}"
let replace = "Script Date: "    
let newContent = Regex.Replace(content, pattern, replace)

You could also use the same variable name, which means that you're declaring a new (immutable) variable that hides the previous one in the rest of the scope (see this SO answer for more information ) 您也可以使用相同的变量名称,这意味着您将声明一个新的(不可变的)变量,该变量会隐藏范围其余部分中的前一个变量( 有关详细信息,请参阅此SO答案

I would also change the last bit (even in C#!) to close the stream even if an exception occurs. 即使发生异常,我也会改变最后一位(即使在C#中!)来关闭流。 In C#, you would use using . 在C#中,你可以使用using To write the same thing in F#, you can use the use keyword: 要在F#中编写相同的内容,可以使用use关键字:

use fileStream = new FileStream(file, FileMode.Truncate, FileAccess.Write, FileShare.None)
use writer = new StreamWriter(fileStream, Encoding.Unicode)
writer.Write(content)
writer.Flush()

The use keyword ensures that the object is disposed (closed) when it goes out of scope - that is, at the end of the for loop iteration (in this case). use关键字确保当对象超出范围时(也就是在for循环迭代结束时(在这种情况下))处置(关闭)对象。

Trivial. 不重要的。 Compile. 编译。 Open in Reflector. 在Reflector中打开。 Choose decompiler language to F#. 选择反编译语言为F#。

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

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