A typed product catalogue, filtered with a regular expression, with a discount applied to the selected products. A concrete example of Klyn’s compact syntax and object model.
import klyn.collections
class Product:
public readonly property name as String
public readonly property price as Double
public Product(name as String, price as Double):
this.name = name
this.price = price
public discounted(rate as Double = 0.10) as Double:
return this.price * (1.0 - rate)
catalog as List<Product> = [
Product("Keyboard", 89.0),
Product("SSD 1 TB", 129.0),
Product("SSD 2 TB", 199.0)
]
selection = [p for p in catalog if p.name =~ /^SSD.*/]
for product in selection:
print(f"{product.name}: {product.discounted():.2f} EUR")
SSD 1 TB: 116.10 EUR SSD 2 TB: 179.10 EUR
klyn Catalog.kn