C# lambda - curry usecases

后端 未结 6 1724
小蘑菇
小蘑菇 2021-01-31 12:04

I read This article and i found it interesting.

To sum it up for those who don\'t want to read the entire post. The author implements a higher order function named Curry

6条回答
  •  囚心锁ツ
    2021-01-31 12:46

    here's another example of how you might use a Curry function. Depending on some condition (e.g. day of week) you could decide what archive policy to apply before updating a file.

        void ArchiveAndUpdate(string[] files)
        {
            Func archiveCurry1 = (file) =>
                Archive1(file, "archiveDir", 30, 20000000, new[] { ".tmp", ".log" });
    
            Func archiveCurry2 = (file) =>
                Archive2("netoworkServer", "admin", "nimda", new FileInfo(file));
    
            Func archvieCurry3 = (file) => true;
    
            // backup locally before updating
            UpdateFiles(files, archiveCurry1);
    
            // OR backup to network before updating
            UpdateFiles(files, archiveCurry2);
    
            // OR do nothing before updating
            UpdateFiles(files, archvieCurry3);
        }
    
        void UpdateFiles(string[] files, Func archiveCurry)
        {
            foreach (var file in files)
            {
                if (archiveCurry(file))
                {
                    // update file //
                }
            }
        }
    
        bool Archive1(string fileName, string archiveDir, 
            int maxAgeInDays, long maxSize, string[] excludedTypes)
        {
            // backup to local disk
            return true;
        }
    
        bool Archive2(string sereverName, string username, 
            string password, FileInfo fileToArchvie)
        {
            // backup to network
            return true;
        }
    

提交回复
热议问题