Thursday, March 6, 2008

TEC rules - using the findall() predicate as if-then-else-always

You can use the findall() predicate to execute another predicate (or group of predicates) and always return true. The correct use of this can help simplify your code. Thanks to I.V. for this tip.

Read on for more.

Often in a TEC rule, you need to do something similar to the following (pseudocode):

if (A()) then
B(),
C()
endif
D(),
E()

Essentially, you only want to do B() and C() if A() is true, but you ALWAYS want to do D() and E(), whether A(), B() or C() returns true. There are several ways to code this without the use of findall(), but I think the code is much cleaner when using findall(), as shown here:

findall(_,(A(),B(),C()),_),
D(),
E()

The findall() predicate executes the specified predicate (or group of predicates) and builds a list, which is returned as the last parameter provided. A consequence of this is that findall() ALWAYS returns true, even if A(), B() or C() fails.

No comments: