Leaving a question open
Decide what is a choice, write out its possibilities, and give each choice its identity.
In Quick start, the tone became a choice because nobody can know it in advance. Sam's card has more open questions now: a border, and a sign-off for every grandchild who signs. Each one needs its possibilities written out, and an identity, so that an answer finds its way back to the right question.
You will learn
- How to tell a fact from a choice, and how
statesettles a choice - The ways to write
choices, with or without weights - How a possibility carries data, and how it is known by its tag
- How
{ of, name }gives each choice its own identity, and what a choice shows before anyone answers
A fact or a choice
Grandma is 90, so her card is printed large. Sam knows that, so the print is a fact. The same card component could make a card for someone younger, and then the print is an open question:
/** @jsxImportSource @reintent/core */
import { choices, host, state, useChoose } from '@reintent/core';
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const PRINTS = ['regular', 'large'] as const;
export type Print = (typeof PRINTS)[number];
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return `${p.age} laps around the sun, ${p.to}!`;
}
/** A guess: nobody knows yet which tone she would like. */
export function useTone() {
return useChoose(choices<Tone>(...TONES), { of: 'card', name: 'tone', defaultValue: 'warm' });
}
/** At 80 and over, the print is large: settled, so there is nothing to choose. */
export function usePrint(p: Person) {
return useChoose(p.age >= 80 ? state<Print>('large') : choices<Print>(...PRINTS), {
of: 'card',
name: 'print',
defaultValue: 'regular',
});
}
export function Card(p: Person) {
const tone = useTone();
const print = usePrint(p);
return host('div', {
style: {
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
fontSize: print === 'large' ? 20 : 14,
},
children: [
host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone) }),
host('p', { children: 'Wishing you a wonderful day.' }),
host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` }),
],
});
}
export default function App() {
return <Card {...GRANDMA} />;
}
Starting…
Starting…
Nothing logged.
usePrint reads the print as a choice, and the code decides whether there is anything to choose. At 80 and over, the possibilities are state('large'): one value, with weight 1. A state is resolved from the start, so nobody is asked, and Choices greys it out. Below 80, both prints are possible. Change age: 90 to 40 in GRANDMA, and the print becomes an open question.
Facts stay in the code
Grandma's name, her age and Sam's name are props. The title follows from the tone and the facts, so it is computed. Only a guess becomes a choice: something that depends on the person, the occasion, or a taste nobody has stated yet.
Writing out the possibilities
choices makes the list of possibilities, a Choices<T>. It takes them in three forms:
/** @jsxImportSource @reintent/core */
import { choices, host, useChoose } from '@reintent/core';
export type Person = { to: string; age: number; from: string };
export const TONES = ['warm', 'funny', 'formal'] as const;
export type Tone = (typeof TONES)[number];
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };
export const LOOK: Record<Tone, { background: string; accent: string }> = {
warm: { background: '#fdebd3', accent: '#b5562b' },
funny: { background: '#fff2a8', accent: '#7a4fd6' },
formal: { background: '#f1eee8', accent: '#2b2b2b' },
};
export function title(p: Person, tone: Tone) {
if (tone === 'formal') return `Many happy returns, ${p.to}.`;
if (tone === 'warm') return `Happy ${p.age}th birthday, ${p.to}!`;
return `${p.age} laps around the sun, ${p.to}!`;
}
/** The possibilities from an array: equally likely, so the code says nothing about them. */
export function useTone() {
return useChoose(choices<Tone>(TONES), { of: 'card', name: 'tone', defaultValue: 'warm' });
}
export function Card(p: Person) {
const tone = useTone();
return host('div', {
style: {
maxWidth: 380,
padding: '1.5em',
borderRadius: 12,
background: LOOK[tone].background,
color: '#3a2a1c',
},
children: [
host('h1', { style: { color: LOOK[tone].accent }, children: title(p, tone) }),
host('p', { children: 'Wishing you a wonderful day.' }),
host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` }),
],
});
}
export default function App() {
return <Card {...GRANDMA} />;
}
Starting…
Starting…
Nothing logged.
choices('warm', 'formal'): the possibilities one by one.choices(TONES): the same, from an array.useToneuses this one.choices([{ value, weight }, …]): each possibility with its weight, the code's prior.
Each entry is a { value, weight }, and What happened shows the lists. The first two forms weigh every possibility the same, and equal weights say nothing, so the choice waits for an answer from above the code. Who answers a choice covers the prior and the layers above it. System One answers with weights too, keyed by tag: a Weights such as { warm: 0.8, funny: 0.15, formal: 0.05 }.
Weights sum to one, and each possibility appears once
choices throws when the weights do not sum to 1, when a weight is negative, or when two possibilities share a tag.
Possibilities that carry data
A border can be flowers of some colour, stars of some count, or plain. Each kind needs its own data:
/** @jsxImportSource @reintent/core */
import { action, choices, host, useChoose } from '@reintent/core';
export type Person = { to: string; age: number; from: string };
export const GRANDMA: Person = { to: 'Grandma', age: 90, from: 'Sam' };
/** A border is flowers of a colour, stars of a count, or plain: each kind with its own data. */
export type Border =
{ kind: 'flowers'; color: string } | { kind: 'stars'; count: number } | { kind: 'plain' };
export const BORDERS = choices<Border>([
{ value: { kind: 'flowers', color: '#e889a8' }, weight: 0.6 },
{ value: { kind: 'stars', count: 9 }, weight: 0.3 },
{ value: { kind: 'plain' }, weight: 0.1 },
]);
/** What a border button does: a decision names a possibility by its tag. */
export function useBorder() {
return useChoose(BORDERS, { of: 'card', name: 'border' });
}
/** Each kind of border, drawn from its own data. */
function Edge(p: { border: Border }) {
const b = p.border;
if (b.kind === 'flowers')
return host('p', { style: { color: b.color, margin: 0 }, children: '✿ ✿ ✿ ✿ ✿ ✿' });
if (b.kind === 'stars') return host('p', { style: { margin: 0 }, children: '★'.repeat(b.count) });
return null;
}
export function Card(p: Person) {
const border = useBorder();
return host('div', {
style: { maxWidth: 380, padding: '1.5em', borderRadius: 12, background: '#fdebd3', color: '#3a2a1c' },
children: [
<Edge border={border} />,
host('h1', { style: { color: '#b5562b' }, children: `Happy ${p.age}th birthday, ${p.to}!` }),
host('p', { children: 'Wishing you a wonderful day.' }),
host('p', { style: { textAlign: 'right', fontStyle: 'italic' }, children: `— ${p.from}` }),
<Edge border={border} />,
],
});
}
export function Buttons() {
const border = useBorder();
return host('p', {
children: BORDERS.map(({ value }) =>
host('button', {
disabled: value.kind === border.kind,
onClick: action.decide({ of: 'card', name: 'border' }, value.kind),
children: value.kind,
}),
),
});
}
export default function App() {
return host('div', { children: [<Card {...GRANDMA} />, <Buttons />] });
}
Starting…
Starting…
Nothing logged.
Border is a union of objects, each with a kind. A possibility is known by its tag: a string is its own tag, an object's tag is its kind, and anything else is tagged by its JSON. tagOf(x) gives the tag. Edge looks at border.kind and draws each kind from its data.
The buttons decide by tag: PickBorder takes a Tagged<Border>, which is 'flowers' | 'stars' | 'plain' or a whole Border. Press stars, and the card gets the stars listed in BORDERS, all nine of them. A decision names a possibility, and the value comes from the list.
Data the list does not hold waits for a reprogramming
A whole value counts as what the person said. { kind: 'stars', count: 9 } is the listed stars, and the decision applies. { kind: 'stars', count: 3 } asks for stars the list does not have: the decision waits (waiting), the card keeps what the layers below give, and the choice is unresolved with the reason 'unmet', so the list can be reprogrammed (When no possibility fits). When a person picks from the list, decide by tag.
One identity per question
Every grandchild signs the card, and each picks their own sign-off: love, hugs or cheers.
/** @jsxImportSource @reintent/core */
import { action, choices, createContext, host, useChoose, type Id } from '@reintent/core';
export const SIGNOFFS = ['Love', 'Hugs', 'Cheers'] as const;
export type Signoff = (typeof SIGNOFFS)[number];
export const FAMILY = ['Sam', 'Mia', 'Leo'];
/** What a button does: the intent gives it to every evaluation (index.intent.tsx). */
export const Decide = createContext<(id: Id, value: unknown) => void>('Decide', { key: () => undefined });
/** Each signer's own sign-off: one choice per name, found by its identity. */
export function signoffOf(name: string): Id {
return { of: name.toLowerCase(), name: 'signoff' };
}
export function useSignoff(name: string) {
return useChoose(choices<Signoff>(...SIGNOFFS), { ...signoffOf(name), defaultValue: 'Love' });
}
function Signer(p: { name: string }) {
const signoff = useSignoff(p.name);
return host('p', {
style: { display: 'flex', justifyContent: 'space-between', margin: '4px 0' },
children: [
host('span', { style: { fontStyle: 'italic' }, children: `${signoff}, ${p.name}` }),
host('span', {
children: SIGNOFFS.map((s) =>
host('button', {
disabled: s === signoff,
onClick: action.decide(signoffOf(p.name), s),
children: s,
}),
),
}),
],
});
}
export function Card() {
return host('div', {
style: { maxWidth: 420, padding: '1.5em', borderRadius: 12, background: '#fdebd3', color: '#3a2a1c' },
children: [
host('h1', { style: { color: '#b5562b' }, children: 'Happy 90th birthday, Grandma!' }),
host('p', { children: 'Wishing you a wonderful day.' }),
FAMILY.map((name) => <Signer name={name} />),
],
});
}
export default function App() {
return <Card />;
}
Starting…
Starting…
Nothing logged.
useChoose finds a choice by its identity, an Id { of, name }: of is what it is about, and name is which question. useSignoff(name) passes of: 'mia' for Mia, so each signer reads a choice of their own, and Choices shows three. Press Hugs next to Mia, and Sam and Leo keep theirs. keyOf(id) writes an identity as a string, such as 'mia#signoff'. Every read reports its choice by this key, and What happened lists the three.
defaultValue is what the card shows while nobody has answered: 'Love' here. Leave it out, and the card shows the likeliest possibility, or the first one listed when they are equally likely.
One identity, one choice
Two reads with the same identity are the same choice, wherever they are. That is how Card and Buttons share the tone in Quick start. Give each item in a list its own of, or every signer signs the same way.
Recap
- A fact stays in the code. A guess becomes a choice, and
state(x)settles a choice on one value. choices(a, b),choices([a, b])andchoices([{ value, weight }])make aChoices<T>; equal weights say nothing.- A possibility is known by its tag (
tagOf): a string, or thekindof an object. A decision names the tag, and the value comes from the list; data the list does not hold waits, and the choice is unresolved with'unmet'. { of, name }is a choice's identity, andkeyOfwrites it as a string.defaultValueis shown while nobody has answered; without it, the likeliest possibility is.
Last updated on
Edit on GitHub