Skip to content

unifiers matchCondition()

Eugene Lazutkin edited this page Apr 3, 2026 · 2 revisions

Matches values using a custom predicate function.

Introduction

This unifier provides maximum flexibility by allowing you to define custom matching logic with a predicate function. The function receives the value being unified and the unification context.

import matchCondition from 'deep6/unifiers/matchCondition.js';
import unify from 'deep6/unify.js';

// Match positive numbers
const isPositive = matchCondition((val) => typeof val === 'number' && val > 0);
unify(42, isPositive); // true
unify(-5, isPositive); // false

API

matchCondition(f)

Arguments:

  • f — a required predicate function with signature (val, ls, rs, env) => boolean.
    • val — the value being unified.
    • ls — the left argument stack.
    • rs — the right argument stack.
    • env — the unification Env.

Returns a MatchCondition unifier instance.

Class: MatchCondition

Extends Unifier.

Properties

  • f — the predicate function.

Methods

  • unify(val, ls, rs, env) — calls the predicate function with the unification context.
    • Returns whatever the predicate returns (truthy/falsy).

Examples

import matchCondition from 'deep6/unifiers/matchCondition.js';
import {match} from 'deep6';

// Match even numbers
const isEven = matchCondition((val) =>
  typeof val === 'number' && val % 2 === 0
);
match(4, isEven); // true
match(3, isEven); // false

// Match non-empty arrays
const isNonEmptyArray = matchCondition((val) =>
  Array.isArray(val) && val.length > 0
);
match([1, 2], isNonEmptyArray); // true
match([], isNonEmptyArray); // false

// Match objects with specific property
const hasId = matchCondition((val) =>
  val && typeof val === 'object' && 'id' in val
);
match({id: 123, name: 'test'}, hasId); // true
match({name: 'test'}, hasId); // false

Clone this wiki locally