The modified environment
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 Option.none();
}
return Option.some(found);
});
const scoreReport = (id: string): Reader<Bulk, string> =>
Cat.cat(ask<Option.Option<User>>())
.feed(
map(
Option.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 },
],
};
expect(run(scoreReport("1321"))(bulk)).toStrictEqual("John's score is 12130!");
expect(run(scoreReport("4209"))(bulk)).toStrictEqual("Alice's score is 320123!");
Executes the computation in an environment modified by
f.