Մι∩z•thedev · Follow
Published in Coding · 6 min read · 1 day ago
__
👏65k 💬321 🔖 ⤴️
__
There are really nice ways to express ourselves in C#.
Here are a couple of them I like.
public record class Location(int PositionX, int PositionY)
{
Location MoveEast() => this with
{
PositionX = PositionX + 1
};
}
foreach ((var p, var quantity) in _productQuantities)
public static implicit operator Location(
(int PositionX, int PositionY) location
)
=> new(location.PositionX, location.PositionY);
public RoverStatus DryRun(MoveEnum move) => move switch
{
MoveEnum.R => TurnRight(),
MoveEnum.L => TurnLeft(),
MoveEnum.M => Direction switch
{
DirectionEnum.N => MoveNorth(),
DirectionEnum.S => MoveSouth(),
DirectionEnum.E => MoveEast(),
DirectionEnum.W => MoveWest(),
_ => throw new NotImplementedException()
},
_ => throw new NotImplementedException()
};
[Obsolete("deprecated, please use blabla instead.")]
public enum DirectionEnum { N, W, S, E }
private static readonly string AllDirections =
Enum.GetValues(typeof(DirectionEnum))
.Cast<DirectionEnum>().Select(x => $"{x}")
.Aggregate((x, y) => x + y);
private static readonly Regex StatusRx =
new(@$"(?<PositionX>[+-]?\d+) +(?<PositionY>[+-]?\d+) +(?<Direction>[{AllDirections}])");
public abstract record RecordWithValidation
{
protected RecordWithValidation() => Validate();
protected virtual void Validate() { }
}
[Fact]
public void status_of_new_Rover__by_string()
{
var controller = new MissionController("9 9");
var rover = controller.AddRover("5 5 E");
Check.That(rover.PrintDispatch()).IsEqualTo("5 5 E");
}
[Theory]
[InlineData(0)]
[InlineData(-1)]
public void width_zero_or_negative__by_value(int Width)
{
int MaximumX = Width - 1, MaximumY = 5;
FluentActions.Invoking(() => new MissionController(MaximumX, MaximumY))
.Should().Throw<Exception>()
.WithMessage("plateau has invalid corner -- MaximumX must be 1 or greater odd");
}
using tuples, no need to do complex prime xor
public override int GetHashCode() => (X, Y, Z).GetHashCode();