View Source Funx.Predicate (funx v0.1.0)
Provides utility functions for working with predicates—functions that return true
or false
.
This module enables combining predicates in a declarative way using logical operations:
p_and/2
: Returnstrue
if both predicates aretrue
.p_or/2
: Returnstrue
if at least one predicate istrue
.p_not/1
: Negates a predicate.p_all/1
: Returnstrue
if all predicates in a list aretrue
.p_any/1
: Returnstrue
if any predicate in a list istrue
.p_none/1
: Returnstrue
if none of the predicates in a list aretrue
.
These functions simplify complex conditional logic.
Examples
Combining predicates with p_and/2
:
iex> is_adult = fn person -> person.age >= 18 end
iex> has_ticket = fn person -> person.tickets > 0 end
iex> can_enter = Funx.Predicate.p_and(is_adult, has_ticket)
iex> can_enter.(%{age: 20, tickets: 1})
true
iex> can_enter.(%{age: 16, tickets: 1})
false
Using p_or/2
for alternative conditions:
iex> is_vip = fn person -> person.vip end
iex> is_sponsor = fn person -> person.sponsor end
iex> can_access_vip_area = Funx.Predicate.p_or(is_vip, is_sponsor)
iex> can_access_vip_area.(%{vip: true, sponsor: false})
true
iex> can_access_vip_area.(%{vip: false, sponsor: false})
false
Negating predicates with p_not/1
:
iex> is_minor = fn person -> person.age < 18 end
iex> is_adult = Funx.Predicate.p_not(is_minor)
iex> is_adult.(%{age: 20})
true
iex> is_adult.(%{age: 16})
false
Using p_all/1
and p_any/1
for predicate lists:
iex> conditions = [is_adult, has_ticket]
iex> must_meet_all = Funx.Predicate.p_all(conditions)
iex> must_meet_any = Funx.Predicate.p_any(conditions)
iex> must_meet_all.(%{age: 20, tickets: 1})
true
iex> must_meet_all.(%{age: 20, tickets: 0})
false
iex> must_meet_any.(%{age: 20, tickets: 0})
true
iex> must_meet_any.(%{age: 16, tickets: 0})
false
Using p_none/1
to reject multiple conditions:
iex> cannot_enter = Funx.Predicate.p_none([is_adult, is_vip])
iex> cannot_enter.(%{age: 20, ticket: :vip})
false
iex> cannot_enter.(%{age: 16, ticket: :basic})
true
Summary
Functions
Combines a list of predicates (p_list
) using logical AND.
Returns true
only if all predicates return true
. An empty list returns true
.
Combines two predicates (pred1
and pred2
) using logical AND.
Returns a predicate that evaluates to true
only if both pred1
and pred2
return true
.
Combines a list of predicates (p_list
) using logical OR.
Returns true
if at least one predicate returns true
. An empty list returns false
.
Combines a list of predicates (p_list
) using logical NOR (negated OR).
Returns true
only if none of the predicates return true
. An empty list returns true
.
Negates a predicate (pred
).
Returns a predicate that evaluates to true
when pred
returns false
, and vice versa.
Combines two predicates (pred1
and pred2
) using logical OR.
Returns a predicate that evaluates to true
if either pred1
or pred2
return true
.
Types
Functions
Combines a list of predicates (p_list
) using logical AND.
Returns true
only if all predicates return true
. An empty list returns true
.
Examples
iex> can_enter = Funx.Predicate.p_all([is_adult, has_ticket])
iex> can_enter.(%{age: 20, tickets: 1})
true
iex> can_enter.(%{age: 16, tickets: 1})
false
Combines two predicates (pred1
and pred2
) using logical AND.
Returns a predicate that evaluates to true
only if both pred1
and pred2
return true
.
Examples
iex> is_adult = fn person -> person.age >= 18 end
iex> has_ticket = fn person -> person.tickets > 0 end
iex> can_enter = Funx.Predicate.p_and(is_adult, has_ticket)
iex> can_enter.(%{age: 20, tickets: 1})
true
iex> can_enter.(%{age: 16, tickets: 1})
false
Combines a list of predicates (p_list
) using logical OR.
Returns true
if at least one predicate returns true
. An empty list returns false
.
Examples
iex> can_access_vip_area = Funx.Predicate.p_any([is_vip, is_sponsor])
iex> can_access_vip_area.(%{vip: true, sponsor: false})
true
iex> can_access_vip_area.(%{vip: false, sponsor: false})
false
Combines a list of predicates (p_list
) using logical NOR (negated OR).
Returns true
only if none of the predicates return true
. An empty list returns true
.
Examples
iex> cannot_enter = Funx.Predicate.p_none([is_adult, is_vip])
iex> cannot_enter.(%{age: 20, ticket: :vip})
false
iex> cannot_enter.(%{age: 16, ticket: :basic})
true
Negates a predicate (pred
).
Returns a predicate that evaluates to true
when pred
returns false
, and vice versa.
Examples
iex> is_minor = fn person -> person.age < 18 end
iex> is_adult = Funx.Predicate.p_not(is_minor)
iex> is_adult.(%{age: 20})
true
iex> is_adult.(%{age: 16})
false
Combines two predicates (pred1
and pred2
) using logical OR.
Returns a predicate that evaluates to true
if either pred1
or pred2
return true
.
Examples
iex> is_vip = fn person -> person.vip end
iex> is_sponsor = fn person -> person.sponsor end
iex> can_access_vip_area = Funx.Predicate.p_or(is_vip, is_sponsor)
iex> can_access_vip_area.(%{vip: true, sponsor: false})
true
iex> can_access_vip_area.(%{vip: false, sponsor: false})
false