Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Pattern matching not completed #58

Open
DickBaker opened this issue Apr 2, 2020 · 0 comments
Open

Pattern matching not completed #58

DickBaker opened this issue Apr 2, 2020 · 0 comments

Comments

@DickBaker
Copy link

[cf. https://www.youtube.com/watch?v=aUbXGs7YTGo&lc=UgzSxrWBQvVlwJ_E3Zp4AaABAg]

Surprisingly nobody (at NDC or in YT comments) has challenged Bill who at offset 44:48 & 45:40 declared victory with this solution
public decimal PeakTimePremium(DateTime timeOfToll, bool inbound) =>
(IsWeekDay(timeOfToll), GetTimeBand(timeOfToll), inbound) switch
{
(true, TimeBand.MorningRush, true) => 2.00m,
(true, TimeBand.EveningRush, false) => 2.00m,
(true, TimeBand.MorningRush, false) => 1.00m,
(true, TimeBand.EveningRush, true) => 1.00m,
(true, TimeBand.Daytime, _) => 1.50m,
(true, TimeBand.Overnight, _) => 0.75m,
(false, _, _) => 1.00m,
};
and the MS docs show very similar at https://github.com/dotnet/try-samples/blob/master/csharp8/patterns-peakpricing.md

For goodness sake please refactor the explicit special conditions first and then drop through to the simple default 1.0m result
public decimal PeakTimePremium(DateTime timeOfToll, bool inbound) =>
(IsWeekDay(timeOfToll), GetTimeBand(timeOfToll), inbound) switch
{
(true, TimeBand.MorningRush, true) => 2.00m,
(true, TimeBand.EveningRush, false) => 2.00m,
(true, TimeBand.Daytime, _) => 1.50m,
(true, TimeBand.Overnight, ) => 0.75m,
(
, _, _) => 1.00m
};
which matches the special cases and dangling default (else) acts as catch-all for the rest. This distils the spaghetti 50 lines to an even simpler (and clearer) conclusion.

Fortunately the actual PeakTimePremium code at https://github.com/dotnet/try-samples/blob/master/csharp8/ExploreCsharpEight/Patterns.cs has now advanced

  • but please can the docs catch-up ?

P.S. Don't declare victory too early (didn't we learn that from Gulf War-1?)
PPS see attached graphic for those who think better visually
WagnerVille.xlsx

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant