-
-
Notifications
You must be signed in to change notification settings - Fork 0
unifiers matchCondition()
Eugene Lazutkin edited this page Apr 3, 2026
·
2 revisions
Matches values using a custom predicate function.
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); // falseArguments:
-
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.
Extends Unifier.
-
f— the predicate function.
-
unify(val, ls, rs, env)— calls the predicate function with the unification context.- Returns whatever the predicate returns (truthy/falsy).
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); // falseCore functions
Environments and variables
Unification
Traverse
Unifiers
Utilities