The function to modify the record of environment.
The modified environment
import { ask, local, map, Reader, run } from "./reader.ts";
import { cat } from "./cat.ts";
import { assertEquals } from "../deps.ts";
import { Option, some, mapOr, none } from "./option.ts";
interface User {
name: string;
id: string;
score: number;
}
interface Bulk {
users: readonly User[];
}
const extractFromBulk = (id: string) =>
local((bulk: Bulk): Option<User> => {
const found = bulk.users.find((elem) => elem.id === id);
if (!found) {
return none();
}
return some(found);
});
const scoreReport = (id: string): Reader<Bulk, string> =>
cat(ask<Option<User>>())
.feed(
map(
mapOr("user not found")(({ name, score }) =>
`${name}'s score is ${score}!`
),
),
)
.feed(extractFromBulk(id)).value;
const bulk: Bulk = {
users: [
{
name: "John",
id: "1321",
score: 12130,
},
{ name: "Alice", id: "4209", score: 320123 },
],
};
assertEquals(run(scoreReport("1321"))(bulk), "John's score is 12130!");
assertEquals(run(scoreReport("4209"))(bulk), "Alice's score is 320123!");
Generated using TypeDoc
Executes the computation in an environment modified by
f
.