chore: add example to demonstrate some features
This commit is contained in:
10
examples/02_demonstration/demo.midas
Normal file
10
examples/02_demonstration/demo.midas
Normal file
@@ -0,0 +1,10 @@
|
||||
predicate in_range(min: float, max: float)(v: float) = min <= v & v <= max
|
||||
predicate is_ratio = in_range(0, 1)
|
||||
|
||||
type Money = float
|
||||
type Price[T <: Money] = T where _ >= 0
|
||||
|
||||
type EUR = Money
|
||||
type USD = Money
|
||||
|
||||
type Reduction = float where is_ratio(_)
|
||||
19
examples/02_demonstration/demo.py
Normal file
19
examples/02_demonstration/demo.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from typing import TypeVar, cast
|
||||
|
||||
from demo_stubs import EUR, USD, Money, Price, Reduction
|
||||
|
||||
T = TypeVar("T", bound=Money)
|
||||
|
||||
|
||||
def apply_reduction(amount: Price[T], reduction: Reduction) -> Price[T]:
|
||||
return cast(Price[T], (1.0 - reduction) * amount)
|
||||
|
||||
|
||||
a1 = cast(Price[EUR], 3.2)
|
||||
a2 = cast(Price[USD], 10.4)
|
||||
r1: Reduction = cast(Reduction, 0.2)
|
||||
|
||||
print(apply_reduction(a1, r1))
|
||||
print(apply_reduction(a2, r1))
|
||||
|
||||
# a3 = a1 + a2
|
||||
Reference in New Issue
Block a user