Reader: Reading a Shared Environment
Write each step against the configuration, and supply it once, when the program runs.
| Models | A result worked out from an environment, such as settings, given when it runs |
| Replaces | Settings injected into a service, or passed down through every method that needs them |
| Effect Path | ReaderPath |
| Instances | Functor, Applicative, Selective, Monad |
| Choose it when | You pass the environment to run yourself; otherwise Context, which reads what the caller bound to a ScopedValue, or compare every type in Choosing a Type |
- Price an order from a
PricingConfigthat no method takes as a parameter - Say what each step of a
Readerchain receives, and when - Run one reader against two configurations, so a test needs no mock
- Change the configuration for one part of a computation with
ReaderPath.local - Use
Readerin generic code through itsMonadinstance
The code on this page is ReaderBook.java: the page includes it, so the build compiles and runs it. ReaderBookTest.java holds what the page says each method does.
Pricing from one configuration
What an order costs depends on settings: the VAT rate, the delivery charge, and the order value from which delivery is free. Pass those settings to every method that prices something and they spread through each signature on the way down. Inject them into a service's constructor instead, and the service prices everything with the settings it was built with. A Reader<R, A> is a function from an environment R to a result A. Function.andThen already gives you its map. What a Reader adds is flatMap, which hands the next step the previous result and the same environment. So the settings go in once, when you run the reader, and each run can be given different ones.
The code on this page uses these imports, with Order from the order records the Optics chapter introduces, and PricingConfig from this chapter's examples:
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.higherkindedj.example.book.foundations.PricingConfig;
import org.higherkindedj.example.book.optics.cast.Order;
import org.higherkindedj.hkt.effect.Path;
import org.higherkindedj.hkt.effect.ReaderPath;
import org.higherkindedj.hkt.reader.Reader;
PricingConfig is a record of the examples' own:
/**
* What pricing reads from its environment: the VAT rate, and the delivery charge for an order below
* the free-delivery threshold.
*/
public record PricingConfig(
BigDecimal vatRate, BigDecimal deliveryCharge, BigDecimal freeDeliveryFrom) {
public PricingConfig {
Objects.requireNonNull(vatRate, "vatRate");
Objects.requireNonNull(deliveryCharge, "deliveryCharge");
Objects.requireNonNull(freeDeliveryFrom, "freeDeliveryFrom");
}
}
Delivery is charged on an order under the threshold, and VAT is added to the whole:
// What the goods cost, before delivery and VAT
static BigDecimal net(Order order) {
return order.lines().stream()
.map(line -> line.price().multiply(BigDecimal.valueOf(line.quantity())))
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
// Delivery is charged below the threshold
static Reader<PricingConfig, BigDecimal> delivery(BigDecimal net) {
return Reader.of(
config ->
net.compareTo(config.freeDeliveryFrom()) >= 0
? BigDecimal.ZERO
: config.deliveryCharge());
}
// VAT on the whole
static Reader<PricingConfig, BigDecimal> withVat(BigDecimal amount) {
return Reader.of(
config ->
amount
.multiply(BigDecimal.ONE.add(config.vatRate()))
.setScale(2, RoundingMode.HALF_UP));
}
// No method here takes the configuration: each step is given it when the reader runs
static Reader<PricingConfig, BigDecimal> toPay(Order order) {
BigDecimal net = net(order);
return delivery(net).flatMap(charge -> withVat(net.add(charge)));
}
toPay describes the price without a configuration to hand. STANDARD charges VAT at 20%, and 4.95 for delivery on an order under 50.00. SALE is the same, with free delivery from 30.00. LAMP_ORDER is one lamp at 40.00. Run the reader with one configuration, then with the other:
Reader<PricingConfig, BigDecimal> lampToPay = toPay(LAMP_ORDER);
BigDecimal usually = lampToPay.run(STANDARD);
// 53.94
BigDecimal inTheSale = lampToPay.run(SALE);
// 48.00
The lamp costs 40.00, under the usual threshold of 50.00, so the usual price adds the 4.95 delivery charge before VAT: (40.00 + 4.95) × 1.20 = 53.94. The sale's threshold is 30.00, so the same reader charges no delivery: 40.00 × 1.20 = 48.00. A test prices against whatever configuration it builds, with no mock to set up.
Each step is given the configuration
flatMap runs the first reader, passes its result to the next step, and runs that step with the same environment. Nothing reads the configuration until run is called, and a reader keeps no result: each run calls its functions again, with the environment it is given.
flowchart TB
accTitle: How one run of toPay uses its configuration
accDescr: Running toPay with the standard configuration gives that configuration to both steps. The delivery step reads the threshold and the delivery charge, and gives 4.95. The VAT step receives that charge as its input, reads the VAT rate, and gives 53.94.
run["toPay(order).run(STANDARD)"]
config["the PricingConfig STANDARD"]
delivery["delivery(40.00)<br/>reads the threshold and the charge"]
vat["withVat(40.00 + 4.95)<br/>reads the VAT rate"]
result["53.94"]
run --> config
config --> delivery
config --> vat
delivery -- "4.95" --> vat
vat --> result
classDef domain fill:#a6d189,stroke:#40a02b,color:#232634
classDef step fill:#8caaee,stroke:#1e66f5,color:#232634
class run,delivery,vat step
class config,result domain
The configuration goes to every step, while each result goes only to the step after it. Reader.ask() gives the environment itself, for a step that needs the whole of it.
On a ReaderPath
Application code holds a reader on a ReaderPath, its Effect Path, and Path.reader(reader) gives you one. It chains with via, combines with zipWith, and adds local, which runs a reader against a configuration changed for that reader alone. A quote can then show what a member pays, with delivery free whatever the order costs:
// The configuration a member sees: the same, with delivery free on every order
static PricingConfig forMembers(PricingConfig config) {
return new PricingConfig(config.vatRate(), config.deliveryCharge(), BigDecimal.ZERO);
}
static ReaderPath<PricingConfig, String> quote(Order order) {
ReaderPath<PricingConfig, BigDecimal> usual = Path.reader(toPay(order));
ReaderPath<PricingConfig, BigDecimal> members = usual.local(ReaderBook::forMembers);
return usual.zipWith(members, (pay, memberPays) -> pay + ", or " + memberPays + " for members");
}
String quoted = quote(LAMP_ORDER).run(STANDARD);
quoted is "53.94, or 48.00 for members". The standard configuration is passed once: usual reads it as it is, and members reads it through forMembers. local also adapts a reader written for a part of a larger environment, such as a PricingConfig inside the application's settings. magnify does the same from a Getter or a FocusPath, as Local Environment Modification shows.
Creating instances
Reader.of(function) wraps a function of the environment, Reader.ask() gives the environment itself, and Reader.constant(value) gives a value whatever the environment holds. Reader.unit() gives Unit, and Reader.fromConsumer(action) runs an action with the environment and gives Unit. On a Path, Path.reader(reader), Path.ask(), Path.asks(function) and Path.readerPure(value) start a ReaderPath. A Reader from these factories always gives a value, as Values, never null sets out. constant(null) throws at the call, and a function given to of or map that returns null fails the reader when it runs. Reader is a functional interface, so a lambda written as one directly has no such check: build a reader with Reader.of.
Methods
| Method | What it does |
|---|---|
Reader.of(function) | Wraps a function of the environment |
Reader.ask() | Gives the environment itself |
Reader.constant(value) | Gives value, whatever the environment |
Reader.unit() | Gives Unit, whatever the environment |
Reader.fromConsumer(action) | Runs action with the environment, and gives Unit |
map(f) | Builds a reader whose result is changed by f |
flatMap(f) | Builds a reader that runs the next step on this one's result, with the same environment |
asUnit() | Keeps the computation and replaces the result with Unit |
run(environment) | Runs the reader with environment and gives its result |
readerPath.via(f), zipWith(other, f) | Chain or combine ReaderPath steps over one environment |
readerPath.local(f) | Runs the path against the environment f gives |
readerPath.magnify(getter) | Runs the path against the part of a larger environment the Getter or FocusPath reads |
readerPath.toReader() | Gives the Reader the path holds |
Working with Kind
Generic code, written once for any container, takes Reader through its Monad instance, from Instances.monad(reader()). The environment type is part of the witness, ReaderKind.Witness<R>, so one instance serves readers of one environment type. You widen a Reader to pass it in, run the result with READER.runReader, and READER.narrow(kind) gives the Reader back. These are the extra imports:
import static org.higherkindedj.hkt.instances.Witnesses.reader;
import static org.higherkindedj.hkt.reader.ReaderKindHelper.READER;
import org.higherkindedj.hkt.Kind;
import org.higherkindedj.hkt.Monad;
import org.higherkindedj.hkt.instances.Instances;
import org.higherkindedj.hkt.reader.ReaderKind;
Monad<ReaderKind.Witness<PricingConfig>> pricing = Instances.monad(reader());
Kind<ReaderKind.Witness<PricingConfig>, BigDecimal> lamp = READER.widen(toPay(LAMP_ORDER));
Kind<ReaderKind.Witness<PricingConfig>, String> label =
pricing.map(total -> "pay " + total, lamp);
String ran = READER.runReader(label, STANDARD);
ran is "pay 53.94". Widening allocates nothing: Reader implements ReaderKind itself, so widen checks for null and returns the same object. Reader has a Selective instance too, ReaderSelective.instance(), an object of its own, and Reader at a glance, at the head of the page, lists it. Code that reads an environment through any monad, not only Reader, takes a MonadReader, as Reading settings with MonadReader shows.
When to use Reader
| You need | Use |
|---|---|
| Steps that read one environment, supplied once when the program runs | Reader, or ReaderPath |
| A value the request binds, which threads forked for that request should see | Context |
| An environment, together with failure or asynchronous work | ConfigContext, or ReaderT |
| Steps that each change a value the next step reads | State |
| One or two methods that need the setting | A parameter: a reader earns its place when many steps share the environment |
Practise threading configuration with Reader, in Exercise 3 of Tutorial 07: Real World (6 exercises), and magnify in Exercise 5 of Tutorial 05: Optic-Polymorphic Zoom and Magnify (6 exercises).
Previous: Advanced Features Next: Context