Skip to content

Commit

Permalink
Refactor yielding methods to conventional LINQ
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz committed Jul 12, 2024
1 parent dfe8b13 commit 1fdacd7
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 56 deletions.
6 changes: 2 additions & 4 deletions src/BizHawk.Client.Common/movie/tasproj/StateDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using BizHawk.Common;

namespace BizHawk.Client.Common
Expand Down Expand Up @@ -86,10 +87,7 @@ public void CopyTo(KeyValuePair<int, byte[]>[] array, int arrayIndex)
}

public IEnumerator<KeyValuePair<int, byte[]>> GetEnumerator()
{
foreach (var kvp in _streams)
yield return new KeyValuePair<int, byte[]>(kvp.Key, this[kvp.Key]);
}
=> _streams.Select(kvp => new KeyValuePair<int, byte[]>(kvp.Key, this[kvp.Key])).GetEnumerator();

public bool Remove(int key)
{
Expand Down
10 changes: 4 additions & 6 deletions src/BizHawk.Client.EmuHawk/Extensions/ToolExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;

using BizHawk.Common;
Expand Down Expand Up @@ -238,8 +239,7 @@ public static void HandleLoadError(this RecentFiles recent, IMainFormForTools ma
}

public static IEnumerable<ToolStripItem> MenuItems(this IMemoryDomains domains, Action<string> setCallback, string selected = "", int? maxSize = null)
{
foreach (var domain in domains)
=> domains.Select(domain =>
{
var name = domain.Name;
var item = new ToolStripMenuItem
Expand All @@ -249,9 +249,7 @@ public static IEnumerable<ToolStripItem> MenuItems(this IMemoryDomains domains,
Checked = name == selected
};
item.Click += (o, ev) => setCallback(name);

yield return item;
}
}
return item;
});
}
}
9 changes: 1 addition & 8 deletions src/BizHawk.Common/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,7 @@ public static T GetEnumFromDescription<T>(this string description)
/// Takes an enum Type and generates a list of strings from the description attributes
/// </summary>
public static IEnumerable<string> GetEnumDescriptions(this Type type)
{
var vals = Enum.GetValues(type);

foreach (var v in vals)
{
yield return v.GetDescription();
}
}
=> Enum.GetValues(type).Cast<Enum>().Select(static v => v.GetDescription());

public static T GetAttribute<T>(this object o)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;

namespace BizHawk.Emulation.Common
{
Expand Down Expand Up @@ -59,12 +60,7 @@ public void Clear()
public bool ContainsKey(string key) => _keys.Contains(key);

public IEnumerator<KeyValuePair<string, AxisSpec>> GetEnumerator()
{
foreach (var key in _keys)
{
yield return new(key, _specs[key]);
}
}
=> _keys.Select(key => new KeyValuePair<string, AxisSpec>(key, _specs[key])).GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,40 +218,10 @@ private void OnCallbackRemoved(object sender, IMemoryCallback callback)
}

public IEnumerator<IMemoryCallback> 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
Expand Down

0 comments on commit 1fdacd7

Please sign in to comment.