/// import { ifElse, recipe, UI } from "commontools"; interface State { matrix: number[][]; row: number; col: number; items: string[]; arr: number[]; a: number; b: number; indices: number[]; nested: { arrays: string[][]; index: number; }; users: Array<{ name: string; scores: number[] }>; selectedUser: number; selectedScore: number; } export default recipe("ElementAccessComplex", (state) => { return { [UI]: (

Nested Element Access

{/* Double indexing into matrix */}

Matrix value: {state.matrix[state.row]![state.col]}

{/* Triple nested access */}

Deep nested: {state.nested.arrays[state.nested.index]![state.row]}

Multiple References to Same Array

{/* Same array accessed multiple times with different indices */}

First and last: {state.items[0]} and{" "} {state.items[state.items.length - 1]}

{/* Array used in computation and access */}

Sum of ends: {state.arr[0]! + state.arr[state.arr.length - 1]!}

Computed Indices

{/* Index from multiple state values */}

Computed index: {state.arr[state.a + state.b]}

{/* Index from computation involving array */}

Modulo index: {state.items[state.row % state.items.length]}

{/* Complex index expression */}

Complex: {state.arr[Math.min(state.a * 2, state.arr.length - 1)]}

Chained Element Access

{/* Element access returning array, then accessing that */}

User score:{" "} {state.users[state.selectedUser]!.scores[state.selectedScore]!}

{/* Using one array element as index for another */}

Indirect: {state.items[state.indices[0]!]}

{/* Array element used as index for same array */}

Self reference: {state.arr[state.arr[0]!]}

Mixed Property and Element Access

{/* Property access followed by element access with computed index */}

Mixed: {state.nested.arrays[state.nested.index]!.length}

{/* Element access followed by property access */}

User name length: {state.users[state.selectedUser]!.name.length}

Element Access in Conditions

{/* Element access in ternary */}

Conditional:{" "} {state.arr[state.a]! > 10 ? state.items[state.b]! : state.items[0]!}

{/* Element access in boolean expression */}

Has value: {ifElse( state.matrix[state.row]![state.col]! > 0, "positive", "non-positive", )}

Element Access with Operators

{/* Element access with arithmetic */}

Product: {state.arr[state.a]! * state.arr[state.b]!}

{/* Element access with string concatenation */}

Concat: {state.items[0]! + " - " + state.items[state.indices[0]!]!}

{/* Multiple element accesses in single expression */}

Sum: {state.arr[0]! + state.arr[1]! + state.arr[2]!}

), }; });