From 1fdacd762a780befa5f70eb2c6d6f34fd307eb74 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Fri, 12 Jul 2024 14:16:31 +1000 Subject: [PATCH] Refactor `yield`ing methods to conventional LINQ --- .../movie/tasproj/StateDictionary.cs | 6 ++-- .../Extensions/ToolExtensions.cs | 10 +++--- .../Extensions/ReflectionExtensions.cs | 9 +---- .../Base Implementations/Axes/AxisDict.cs | 8 ++--- .../MemoryCallbackSystem.cs | 34 ++----------------- 5 files changed, 11 insertions(+), 56 deletions(-) diff --git a/src/BizHawk.Client.Common/movie/tasproj/StateDictionary.cs b/src/BizHawk.Client.Common/movie/tasproj/StateDictionary.cs index 4c4455d875..820edc0f43 100644 --- a/src/BizHawk.Client.Common/movie/tasproj/StateDictionary.cs +++ b/src/BizHawk.Client.Common/movie/tasproj/StateDictionary.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Linq; using BizHawk.Common; namespace BizHawk.Client.Common @@ -86,10 +87,7 @@ namespace BizHawk.Client.Common } public IEnumerator> GetEnumerator() - { - foreach (var kvp in _streams) - yield return new KeyValuePair(kvp.Key, this[kvp.Key]); - } + => _streams.Select(kvp => new KeyValuePair(kvp.Key, this[kvp.Key])).GetEnumerator(); public bool Remove(int key) { diff --git a/src/BizHawk.Client.EmuHawk/Extensions/ToolExtensions.cs b/src/BizHawk.Client.EmuHawk/Extensions/ToolExtensions.cs index c5b2399847..677ba79c0f 100644 --- a/src/BizHawk.Client.EmuHawk/Extensions/ToolExtensions.cs +++ b/src/BizHawk.Client.EmuHawk/Extensions/ToolExtensions.cs @@ -1,6 +1,7 @@ using System.IO; using System.Collections.Generic; using System.Globalization; +using System.Linq; using System.Windows.Forms; using BizHawk.Common; @@ -238,8 +239,7 @@ namespace BizHawk.Client.EmuHawk.ToolExtensions } public static IEnumerable MenuItems(this IMemoryDomains domains, Action setCallback, string selected = "", int? maxSize = null) - { - foreach (var domain in domains) + => domains.Select(domain => { var name = domain.Name; var item = new ToolStripMenuItem @@ -249,9 +249,7 @@ namespace BizHawk.Client.EmuHawk.ToolExtensions Checked = name == selected }; item.Click += (o, ev) => setCallback(name); - - yield return item; - } - } + return item; + }); } } diff --git a/src/BizHawk.Common/Extensions/ReflectionExtensions.cs b/src/BizHawk.Common/Extensions/ReflectionExtensions.cs index 3f08b55d05..a91b1154cb 100644 --- a/src/BizHawk.Common/Extensions/ReflectionExtensions.cs +++ b/src/BizHawk.Common/Extensions/ReflectionExtensions.cs @@ -94,14 +94,7 @@ namespace BizHawk.Common.ReflectionExtensions /// Takes an enum Type and generates a list of strings from the description attributes /// public static IEnumerable GetEnumDescriptions(this Type type) - { - var vals = Enum.GetValues(type); - - foreach (var v in vals) - { - yield return v.GetDescription(); - } - } + => Enum.GetValues(type).Cast().Select(static v => v.GetDescription()); public static T GetAttribute(this object o) { diff --git a/src/BizHawk.Emulation.Common/Base Implementations/Axes/AxisDict.cs b/src/BizHawk.Emulation.Common/Base Implementations/Axes/AxisDict.cs index bc054dfb83..917dc52434 100644 --- a/src/BizHawk.Emulation.Common/Base Implementations/Axes/AxisDict.cs +++ b/src/BizHawk.Emulation.Common/Base Implementations/Axes/AxisDict.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; +using System.Linq; namespace BizHawk.Emulation.Common { @@ -59,12 +60,7 @@ namespace BizHawk.Emulation.Common public bool ContainsKey(string key) => _keys.Contains(key); public IEnumerator> GetEnumerator() - { - foreach (var key in _keys) - { - yield return new(key, _specs[key]); - } - } + => _keys.Select(key => new KeyValuePair(key, _specs[key])).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); diff --git a/src/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs b/src/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs index 9f2433e7c3..37bca8e70c 100644 --- a/src/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs +++ b/src/BizHawk.Emulation.Common/Base Implementations/MemoryCallbackSystem.cs @@ -218,40 +218,10 @@ namespace BizHawk.Emulation.Common } public IEnumerator GetEnumerator() - { - foreach (var imc in _reads) - { - yield return imc; - } - - foreach (var imc in _writes) - { - yield return imc; - } - - foreach (var imc in _execs) - { - yield return imc; - } - } + => _reads.Concat(_writes).Concat(_execs).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() - { - foreach (var imc in _reads) - { - yield return imc; - } - - foreach (var imc in _writes) - { - yield return imc; - } - - foreach (var imc in _execs) - { - yield return imc; - } - } + => GetEnumerator(); } public class MemoryCallback : IMemoryCallback