-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from polyadic/implement-divide-money-expressions
Implement divide money expressions
- Loading branch information
Showing
11 changed files
with
95 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using System; | ||
using System.Globalization; | ||
|
||
namespace Funcky.Test; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System.Runtime.Serialization; | ||
|
||
namespace Funcky; | ||
|
||
public class MissingExchangeRateException : Exception | ||
{ | ||
public MissingExchangeRateException() | ||
: base("If you calculate with more than one currency, you have to define the exchange rate and target in the evaluation context.") | ||
{ | ||
} | ||
|
||
public MissingExchangeRateException(string? message) | ||
: base(message) | ||
{ | ||
} | ||
|
||
public MissingExchangeRateException(string? message, Exception? innerException) | ||
: base(message, innerException) | ||
{ | ||
} | ||
|
||
protected MissingExchangeRateException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
using Funcky.Monads; | ||
|
||
namespace Funcky; | ||
|
||
public static class MoneyDivisionExtension | ||
{ | ||
public static IMoneyExpression Divide(this IMoneyExpression dividend, decimal divisor) | ||
=> new MoneyProduct(dividend, 1.0m / divisor); | ||
|
||
public static decimal Divide(this IMoneyExpression dividend, IMoneyExpression divisor, Option<MoneyEvaluationContext> context = default) | ||
=> Divide( | ||
dividend.Evaluate(context), | ||
divisor.Evaluate(context)); | ||
|
||
private static decimal Divide(Money dividend, Money divisor) | ||
=> dividend.Currency == divisor.Currency | ||
? dividend.Amount / divisor.Amount | ||
: throw new MissingExchangeRateException(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters