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

Pattern matching not completed #58

Open

Description

@DickBaker

[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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions