PeaBox Lab › PeaLang › Learn › Chapter 4 › Lesson 5

Part I — Foundations · Chapter 4 · Lesson 5 of 5 · 7 min read

Chapter 4 Checkpoint: Modular Pricing Rules Library

Checkpoint level: Foundation

Purpose: Integrate function declarations, parameters, defaults, returns, scope, closures, higher-order behavior, decomposition, naming, and defensive contracts in one reusable pricing library.

Checkpoint outcomes

By completing this checkpoint, you will demonstrate that you can:

Project scenario

A commerce application needs a pricing library that calculates line subtotals, tier discounts, promotional adjustments, tax, delivery, and a final quote. The library must keep policy logic reusable and free of external capabilities. A host application may later supply validated values, but this checkpoint uses source-defined scenarios.

4C.1 Pricing policy

4C.2 Required quote inputs

4C.3 Required result

A valid quote must return a string-keyed map containing:

An invalid quote must return a predictable invalid result with valid = false and a stable reason code. Do not calculate downstream monetary fields after validation fails.

4C.4 Required functions

4C.5 Contract requirements

For every function, document purpose, parameter meanings, defaults, units, accepted ranges, returned value, failure result, and side effects. For higher-order functions, also document the required callable arity and result meaning. For closures, document captured values and whether captured state can change.

4C.6 Dependency direction

The intended direction is:

Validation and calculation functions must not call presentation functions. Presentation must not recalculate policy. No layer may depend on files, HTTP, processes, secrets, or host-specific objects.

4C.7 Core invariants

4C.8 Reference structure

Attempt the project independently before using this outline. It demonstrates responsibility boundaries rather than every line of the final submission.

function is_valid_rate(rate) { return rate >= 0 and rate <= 1 } function line_subtotal(unit_price, quantity) { if unit_price < 0 or quantity <= 0 { return null } return unit_price * quantity } function tier_discount_rate(subtotal_amount, customer_tier) { if subtotal_amount < 0 { return null } if customer_tier == "gold" and subtotal_amount >= 500 { return 0.1 } if customer_tier == "silver" and subtotal_amount >= 500 { return 0.05 } if customer_tier == "standard" or customer_tier == "silver" or customer_tier == "gold" { return 0 } return null } function make_promotion(promotion_rate) { if promotion_rate < 0 or promotion_rate > 0.05 { return null } function apply(amount) { if amount < 0 { return null } return amount - (amount * promotion_rate) } return apply } function apply_adjustment(amount, adjustment) { return adjustment(amount) }

The final build_quote function should validate required inputs, compute each named stage, check every nullable helper result, verify invariants, and return a complete map. It should not print.

4C.9 Failure codes

Use stable reason codes such as:

Return the most specific applicable validation failure before attempting later stages.

4C.10 Required scenario matrix

4C.11 Closure and higher-order tests

4C.12 Function-level tests

Test each helper independently before integration. Cover ordinary inputs, exact boundaries, invalid inputs, and every documented default. For pure calculations, call the function twice with identical inputs and confirm identical results.

4C.13 Integration and invariant tests

For every valid quote, recompute each invariant from returned fields and compare it with the stored value. Verify that priority rules—tier discount, promotion cap, free delivery, and order of tax calculation—produce the documented outcome.

For every invalid quote, confirm that no downstream success fields are trusted and that the correct specific reason code is returned.

4C.14 Refactoring requirement

Begin with a baseline implementation or reference result table. Extract responsibilities one at a time. After each extraction, rerun the same scenario matrix and record whether behavior remained equivalent.

Introduce one temporary regression, such as calculating tax before discount or using a strict greater-than comparison at the 500 threshold. Show which test detects it, restore the approved rule, and rerun the full matrix.

4C.15 Command-line validation workflow

4C.16 Submission package

4C.17 Assessment rubric

Completion standard: Every required scenario passes, every invariant holds, closure instances remain independent, and the learner can explain the dependency direction and failure behavior without inspecting trial output.

4C.18 Reflection questions

4C.19 Optional extensions

Chapter 4 summary

You can now declare and call functions, design parameters and defaults, return predictable results, reason about local scope, create closures, pass functions as values, decompose workflows, name domain concepts, isolate effects, propagate failures, and preserve invariants. The modular pricing-rules checkpoint integrates those skills into a reusable library.

Transition to Chapter 5

Chapter 5 examines core data and collections in depth. The next lesson covers null, booleans, numbers, strings, lists, and string-keyed maps, including their operations, behavior, and role at portable boundaries.

Checkpoint status: Approved.

← Previous Next →