mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Minor code clean-up
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace Game.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides additional methods for the <see cref="ImmutableArray{T}"/> class.
|
||||
/// </summary>
|
||||
public static class ImmutableArrayExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Maps each element in an immutable array to a new value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TSource">
|
||||
/// The type of elements in the source array.
|
||||
/// </typeparam>
|
||||
/// <typeparam name="TResult">
|
||||
/// The type of elements in the resulting array.
|
||||
/// </typeparam>
|
||||
/// <param name="source">
|
||||
/// The source array.
|
||||
/// </param>
|
||||
/// <param name="selector">
|
||||
/// Function which receives an element from the source array and its
|
||||
/// index and returns the resulting element.
|
||||
/// </param>
|
||||
public static ImmutableArray<TResult> Map<TSource, TResult>(this ImmutableArray<TSource> source, Func<TSource, int, TResult> selector)
|
||||
{
|
||||
var builder = ImmutableArray.CreateBuilder<TResult>(source.Length);
|
||||
|
||||
for (var i = 0; i < source.Length; ++i)
|
||||
builder.Add(selector(source[i], i));
|
||||
|
||||
return builder.MoveToImmutable();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,7 @@ namespace Game
|
||||
/// <summary>
|
||||
/// Defines the set of companies that will be simulated in the game.
|
||||
/// </summary>
|
||||
private static ImmutableArray<Company> Companies = ImmutableArray.CreateRange(new[]
|
||||
private readonly static ImmutableArray<Company> Companies = ImmutableArray.CreateRange(new[]
|
||||
{
|
||||
new Company("INT. BALLISTIC MISSILES", "IBM", sharePrice:100),
|
||||
new Company("RED CROSS OF AMERICA", "RCA", sharePrice:85 ),
|
||||
|
||||
@@ -38,13 +38,13 @@ namespace Game
|
||||
},
|
||||
(parameters, previousDay) => previousDay with
|
||||
{
|
||||
Companies = ImmutableArray.CreateRange(
|
||||
previousDay.Companies.Select ((company, index) => AdjustSharePrice(
|
||||
Companies = previousDay.Companies.Map(
|
||||
(company, index) => AdjustSharePrice(
|
||||
random,
|
||||
company,
|
||||
parameters.trend,
|
||||
parameters.positiveSpike == index,
|
||||
parameters.negativeSpike == index)))
|
||||
parameters.negativeSpike == index))
|
||||
});
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ namespace Game
|
||||
/// The maximum number of days each trend should last.
|
||||
/// </param>
|
||||
public static IEnumerable<double> Trends(Random random, int minDays, int maxDays) =>
|
||||
random.Integers(minDays, maxDays + 1).SelectMany(days => Enumerable.Repeat(GenerateTrend(random), days));
|
||||
random.Integers(minDays, maxDays + 1).SelectMany(daysInCycle => Enumerable.Repeat(GenerateTrend(random), daysInCycle));
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random value for the market trend.
|
||||
@@ -143,7 +143,7 @@ namespace Game
|
||||
private static IEnumerable<int?> PriceSpikes(Random random, int companyCount, int minDays, int maxDays) =>
|
||||
random.Integers(minDays, maxDays + 1)
|
||||
.SelectMany(
|
||||
days => Enumerable.Range(0, days),
|
||||
(days, dayNumber) => dayNumber == 0 ? random.Next(companyCount) : default(int?));
|
||||
daysInCycle => Enumerable.Range(0, daysInCycle),
|
||||
(daysInCycle, dayNumber) => dayNumber == 0 ? random.Next(companyCount) : default(int?));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user