Core Concepts

Modular expressions

Split one large expression into reusable sub-expressions, then compose them into a final result that stays reactive.

See how modular expressions are implemented in the advanced modular expressions page.

What it means

In rs-x, expressions can reference other expressions as first-class values. Instead of one monolithic formula string, you define focused building blocks and compose them into a higher-level expression.

Practical value

This makes expression logic easier to read, debug, and test. It also improves runtime efficiency because shared sub-expressions are reused instead of recalculated in multiple places.

Key points

From monolithic formula to reusable modules

A large formula often duplicates parts of the same calculation, which hurts readability and performance. In the credit-risk example, the risk score logic appears multiple times in one expression string.

With modular expressions, that same logic is split into smaller units. Each unit has one responsibility, and a final expression combines them. This removes duplication and makes each piece independently understandable.

Reactive behavior across expression boundaries

When a sub-expression changes, dependent expressions update automatically. You do not need custom plumbing code to keep derived values in sync.

Because each expression is observable, the dependency graph remains explicit and predictable, even when modules are nested.

Performance and maintainability benefits

Expression parser services cache parse/evaluation structures, and modular design avoids repeated heavy computation.

As rules grow, modular expressions let you evolve one part at a time while keeping the full formula behavior stable.

Example

import { InjectionContainer } from '@rs-x/core';
import { rsx, RsXExpressionParserModule } from '@rs-x/expression-parser';

await InjectionContainer.load(RsXExpressionParserModule);

const model = {
  customer: {
    age: 31,
    income: 52000,
    employmentYears: 4,
  },
  credit: {
    score: 640,
    outstandingDebt: 12000,
  },
  riskParameters: {
    market: { baseInterestRate: 0.04 },
    risk: { volatilityIndex: 0.22, recessionProbability: 0.18 },
  },
};

// Sub-expressions