web analytics

Advanced Comparisons using Composable Predicate Modules

What is a Predicate?

A predicate is an operation on an Input object which produces a boolean (true/false) result. It can be helpful to think of a predicates as “Yes or No” questions; “Is this car red?” “Is this building taller than 4 stories?” “Does this restaurant serve shrimp?”. These questions can be applied to any car, building, or restaurant, and will produce a single yes or no answer about the color, height, or crustacean availability of each. More abstractly, a predicate determines whether an object meets (or fails to meet) certain criteria.

Starting Simple: Numeric and String Comparison Predicates

The most basic predicates are the Numeric and String Comparison Predicates, which compare their input to a fixed number or string; “Is my input greater than 4?” “Does my input end with ‘Road’?”

Simple Numeric and String Comparison Predicates being applied to inputs.

Building it Bigger: Object Lifters and Array Operations

The preceding modules allow you to operate on numbers and strings, but what if you need to process complex objects? The Lift Predicate to Object and Lift Predicate to JSON modules allow you to transform an existing predicate (which may operate on strings, numbers, or anything else) to produce a predicate which operates on objects, by applying said existing predicate to a single member of the object. You might go from “Is my input greater than 4?” to “Is my input’s Height property greater than 4?”, or “Does my input’s Address field end with ‘Road’?”

Predicates being applied to a JSON object

What about arrays? If you know the precise index of an element you wish to apply a predicate to, you can use bracket syntax in the above modules (i.e. ‘[0]’ for the expression/path input to apply to produce a predicate like “Is the 1st item in my input greater than 4?”). If you’re interested in the array as a whole, we have the Any/All Items Satisfy Predicate module, which can produce a predicate like “Is every(/any) item in my input greater than 4?”.

Predicates being applied to an array

Many to One: Predicate Combination

So far, we’ve seen how you make predicates that apply to simple types, and predicates that apply other predicates to members of complex types, but so far each predicate has been singular in focus; it applies to its input, or one property of its input. When you want to make a predicate that applies to multiple properties, you can create multiple predicates and combine them using the Predicate Logical Operation module, to produce a predicate like “Is my input’s Height property greater than 4 AND does its Address field ends in ‘Road’?”. You can combine arbitrary numbers of predicates in this way, using the AND (all predicates satisfied), OR (at least one predicate satisfied), and XOR (an odd number of predicates satisfied) operations.

Predicates being combined with an AND operation

You can also use the NOT operation to negate a single input predicate; i.e. produce a predicate that returns the opposite result for a given input.

Predicate being inverted with a NOT operation

Wrapping it Up: Predicate Application

So once you’ve built up a predicate, what can you do with it? As you’ve already seen in the example screenshots, you can provide a predicate and an object to the Evaluate Predicate module, and it will tell you whether or not the object satisfies the predicate.

You can also use a predicate to filter items from a list using the Filter Array By Predicate module, which will return an array containing only the input object that satisfied the predicate.

Filtering an array based on a predicate