小编pes*_*ino的帖子

如何在F#中定义执行顺序?

在纯函数式编程中,执行顺序无关紧要,因此未确定(即由编译器决定).如果你有副作用,执行的顺序很重要.那么如何在F#中定义?

我有一个函数,递归删除给定路径的所有空子文件夹.此外,如果它们的名称在给定列表中,它将删除其中包含的一些文件.

算法很简单:

  1. 删除列表中的所有文件.
  2. 对子文件夹进行递归调用.
  3. 如果文件夹为空,请删除该文件夹.这必须是最后一步.

此外,该函数返回已删除元素的数量作为元组(已删除文件夹的数量,已删除文件的数量).

这是我的代码:

let rec DeleteEmptyFolders path filenames =
    // Deletes a file or folder using the given function.
    // Returns 1 if the file could be deleted, otherwise 0.
    let Delete delete name =
        try
            delete name;
            1
        with
            | _ -> 0

    // The function result (number of deleted folders and files).
    let deletedFolders (a, _) = a
    let deletedFiles   (_, a) = a

    let accumulator a b = 
        ( deletedFolders a + …
Run Code Online (Sandbox Code Playgroud)

f# functional-programming

3
推荐指数
1
解决办法
381
查看次数

如果字符串存在于大型对象列表中,那么比较最快(性能)的方法是什么?

目前我有包含两个字符串的对象:

class myClass
{
    public string string1 { get; set; }
    public string string2 { get; set; }

    public bool MatcheString1(string newString)
    {
        if (this.string1 == newString)
        {
            return true;
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我有一个第二个类,使用List列出上述对象.

class URLs : IEnumerator, IEnumerable
{
    private List<myClass> myCustomList;
    private int position = -1;

    //  Constructor
    public URLs()
    {
        myCustomList = new List<myClass>();
    }
}
Run Code Online (Sandbox Code Playgroud)

在那个类中,我正在使用一种方法来检查列表中是否存在字符串

//  We can also check if the URL string is present in the collection
public bool ContainsString1(string newString) …
Run Code Online (Sandbox Code Playgroud)

c# string list

3
推荐指数
1
解决办法
126
查看次数

标签 统计

c# ×1

f# ×1

functional-programming ×1

list ×1

string ×1