F# ≥ C# (Tuple and Swap)

本文对比了F#与C#中的元组(Tuple)概念及交换(Swap)操作。F#通过简洁的语法实现任意类型的值交换,利用元组特性简化了代码,提高了效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在F#中,有一个被称为元组(Tuple)的概念,“元组”是一些未命名但经过排序的值的分组,这些值可能具有不同的类型,这篇文章就来将它与C#之中的交换(Swap)做一次比较。

F#,作为一门新兴的语言,相比C#来说,有它更为简单的实现方式,它能让代码变得更加的简洁,也能减少程序的bug,这就是我如此喜欢F#的原因。接下来我们举一些例子来描述F#比C#做的更好的地方,今天这一篇我们谈谈元组与交换之间的区别。

Swap是我能想到的最简单的操作,现在,我想要一个通用的交换方法,它能处理任何类型,在C#中,你可以使用对象类型或者泛型来实现,但无论如何,你必须使用一个临时变量(虽然有些方法可以不通过临时变量来实现这个功能,但不太容易想到或者看懂),然而,F#则能够很简单的实现这个功能:

let swap(a,b) = (b,a)

传递的参数是(a,b)然后我们可以直接得到返回值(b,a),这难道不简单么?

唯一令我感到迷惑的是(a,b)是一个特殊的类型,这就是元组(Tuple),因此,你可以这么理解,在实现交换功能的时候只需要一个单独的参数这就是元组。

元组也支持超过2个参数的方式,这样,你也可以做如下交换:

let swap2(a,b,c,d,e) = (a,c,b,e,d)

同时,你也可以在元组中按你自己的意愿做任意的交换。

注:此文为译文,如有兴趣请查看blogspot中博主的原文:http://apollo13cn.blogspot.com/2011/11/f-c-tuple-and-swap.html

 

转载于:https://www.cnblogs.com/Jennifer/archive/2012/11/07/2759053.html

请帮我分析以下代码的结构和功能,using ClusterAlgorithm.commonType; using ClusterAlgorithm.utils; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClusterAlgorithm.common { class DeadLockTool { public static bool isEnableSeq(List<string> seq, PetriNet PN, ScheduleState scheduleState, SearchState searchState) { bool enable = true; if (PN.IsManuMode) { return enable; } List<int> mark = new List<int>(searchState.mark); Dictionary<string, List<int>> rotate_mark = CopyTool.CloneDictionaryList(searchState.rotate_mark); // <> weiwei 20250114 for deadlock misdetection problem in scenarios with revisit,conflict and complicated routes #if false foreach (string t in seq) { if (!isEnableT(t, PN, scheduleState, searchState, mark, rotate_mark)) { enable = false; break; } Tuple<List<int>, Dictionary<string, List<int>>> rlt2 = PN.FireT(mark, t, rotate_mark); mark = rlt2.Item1; rotate_mark = rlt2.Item2; } #else if(AlgorithmSwitcher.getInstance().strategy == Strategy.Pull && seq.Count>0 && isLastTPlace(seq, PN)) { //Pull模式下死锁控制(调度模式在Job过程中不能动态切换, push控制铺满片的情况下,切换成Pull会报死锁) if (!isSafeT(seq[seq.Count-1], PN, scheduleState, mark, rotate_mark, seq)) { enable = false; } } else { //Swap或Push模式下死锁控制 foreach (string t in seq) { if (!isSafeT(t, PN, scheduleState, mark, rotate_mark, new List<string>())) { enable = false; break; } Tuple<List<int>, Dictionary<string, List<int>>> rlt2 = PN.FireT(mark, t, rotate_mark); mark = rlt2.Item1; rotate_mark = rlt2.Item2; } } #endif return enable; } /// <summary> /// 是否tseq的最后一个是放片变迁 /// </summary> /// <param name="tseq"></param> /// <param name="PN"></param> /// <returns></returns> private static bool isLastTPlace(List<string> tseq, PetriNet PN) { if (tseq.Count <= 0) { return false; } bool isLastPlace = false; foreach (string m in PN.PredVertex[tseq[tseq.Count-1]]) { //前序是移动库所,则一定是放片变迁 if (m[0] == 'm') { isLastPlace = true; break; } } return isLastPlace; } // <> weiwei 20250114 for deadlock misdetection problem in scenarios with revisit,conflict and complicated routes /// <summary> /// 判断单个变迁或整个Pull序列是否安全 /// </summary> /// <param name="t">单个变迁(push)或Pull的最后一个变迁</param> /// <param name="PN">petri网对象</param> /// <param name="scheduleState"></param> /// <param name="mark"></param> /// <param name="rotate_mark"></param> /// <param name="fullSeq"></param> /// <returns></returns> public static bool isSafeT(string tEnd, PetriNet PN, ScheduleState scheduleState, List<int> mark, Dictionary<string, List<int>> rotate_mark, List<string> fullSeq) { List<int> newMark = new List<int>(); bool isEndOfPull = false; if (fullSeq==null || fullSeq.Count<=0) { Tuple<List<int>, Dictionary<string, List<int>>> rlt2 = PN.FireT(mark, tEnd, rotate_mark); newMark = rlt2.Item1; isEndOfPull = false; } else { //pull模式下整个Pull序列更新 Tuple<List<int>, Dictionary<string, List<int>>> rlt2 = PN.FireSeq(mark, fullSeq, rotate_mark); newMark = rlt2.Item1; isEndOfPull = true; } return IsSafeDeepSearch(tEnd, PN, scheduleState, newMark, new SortedDictionary<string, int>(), isEndOfPull); } // <> weiwei 20250114 for deadlock misdetection problem in scenarios with revisit,conflict and complicated routes /// <summary> /// 迭代死锁控制(暂未支持含中心资源的场景) /// </summary> /// <param name="PN"></param> /// <param name="markAfterT"></param> /// <param name="t"></param> /// <param name="rListSaturate"></param> /// <returns></returns> private static bool IsSafeDeepSearch(string t, PetriNet PN, ScheduleState scheduleState, List<int> markAfterT, SortedDictionary<string, int> rListSaturate, bool isEndOfPull) { if (PN.TpredR.ContainsKey(t) == false) { //安全 return true; } string res_t = PN.TpredR[t]; if (markAfterT[PN.Ps.IndexOf(res_t)] > 0) { //安全 return true; } else { if (rListSaturate.ContainsKey(res_t)) { //饱和站点第二次出现,已饱和 return false; } rListSaturate[res_t] = 0; bool anyRtPathUnsaturated = false; foreach (string t1_retnRes in PN.PredVertex[res_t]) { //impossible if (t1_retnRes[0] != 't' || PN.TpredP.ContainsKey(t1_retnRes) ==false){continue;} //获得PullSeq列表 List<List<string>> pullSeqsofT1 = new List<List<string>>(); bool nextIsEndOfPull = false; if (isEndOfPull && scheduleState.PullSeqs.ContainsKey(t1_retnRes)) { nextIsEndOfPull = true; foreach (string seqStr in scheduleState.PullSeqs[t1_retnRes]) { if (seqStr.Contains("pull")) { List<string> pullSeq = seqStr.Split(';')[0].Split(',').ToList(); pullSeqsofT1.Add(pullSeq); } } } if (pullSeqsofT1.Count <= 0) { //非Pull模式下死锁控制 pullSeqsofT1.Add(new List<string> { t1_retnRes }); } foreach(List<string> t1seq in pullSeqsofT1) { if (markAfterT[PN.Ps.IndexOf(PN.TpredP[t1seq[0]])] <= 0) { //前置过程库所没有占用rest中的资源 continue; } //分支 SortedDictionary<string, int> tempRListSaturate = new SortedDictionary<string, int>(rListSaturate); if (IsSafeDeepSearch(t1seq[t1seq.Count-1], PN, scheduleState, markAfterT, tempRListSaturate, nextIsEndOfPull)) { //找到任何一个变迁后续可以走得通 anyRtPathUnsaturated = true; break; } } } return anyRtPathUnsaturated; } }
最新发布
06-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值