Clojure Rules Next Steps
Having read Clojure Rules 101, you'll recall that a Clojure formulas contain operands and operators (not to mention a good deal of parentheses). This article will explain how each get a little more complex when writing Clojure Rules in LMS. In essence, it comes down to this:
- Operands don't have to be a static number. If you put a variable in a rule, the system will pull values from a specific loan or customer when it evaluates their account.
- Operators aren't limited to basic math. There's also comparison, logic, and utility functions.
The rest of this article will explore how these special operands and operators work and how they help with automation.
Let's use the example (+ 1 x). In this rule, x is a variable, a placeholder for another value. As you plug in different values for X, the formula will give different answers. In LMS, there are dozens of values related to loans and customers, and we can use a variable to pull these values from the system. For example, let's write a Clojure formula that will give us the sum of the next payment the amount that's already past due.
(+ status-next-payment-amount status-amount-due)
These variables are long and descriptive enough that you can tell at a glance what each means, but from a math perspective, this formula is as simple as (+ x y). When this formula runs on a loan, the system will pull the actual numbers and solve the equation, giving us a dollar amount as our answer.
These variables pull numbers (in this case, a dollar amount). Others will pull dates or even text. The variable loan-status, for example, will return something like "Open - Repaying", depending on how your loan statuses are configured.
We also maintain an article with a full list of the Available Variables.
So far, we've only seen examples with basic math functions as the operators. But there are also operators that perform different kinds of functions. Some return a number, like our math functions, but others will return a true-or-false value. These are the operators we use in Rules Applied: When a rule evaluates to true, the system will apply an affect on the loan.
The next few sections will go over the different kinds of operators and show what kind of results they give.
In this article and the last one, you've already seen some math operators. They include add +, subtract -, multiply *, and divide /. In every case, math operators give a number as their answer.
Comparisons refer to basic valuations of whether one value is bigger, smaller, or equal to the next. The basic comparisons include greater than >, less than <, greater than or equal to >=, and less than or equal to <=. There's also equality = and inequality not=. All of these operators can work on multiple operands, and each will will return a true-or-false answer.
5 > 4 > 3
(> 5 4 3)
5 is greater than 4, which in turn is greater than 3.
5 ≥ 3 ≥ 4
(>= 5 3 4)
5 is greater than 3, but 3 is less than 4. The formula is false.
1 < 2 < 2
(< 1 2 2)
1 is less than two, but two and two are equal. The numbers aren't in a strictly ascending order, so it's false.
1 ≤ 2 ≤ 2
(<= 1 2 2)
Unlike the last example, the less than or equal to <= operator will accept a series of numbers that are equal. As long as a number isn't greater than the one after it, the formula is true.
(3 + 4) = (14 / 2) = 7
(= (+ 3 4) (/ 14 2) 7)
3 plus 4 is 7, 14 divided by 2 is 7, and 7 is of course equal to 7. All the operands are equal, so the formula is true.
(3 + 4) ≠ (14 / 2) ≠ 7
(not= (+ 3 4) (/ 14 2) 7)
If an equality = formula is true, its inequality not= counterpart is false.
Logical operators can make judgements about their operands, which are always formulas. They include and, or, and not. The and operator evaluates to true if all of it's operands are true formulas. The or operator, on the other hand, will evaluate as true if at least one of the operands is true. Compare these two formulas:
The equations 2 = 2 and 3 = 4 are both true.
(and (= 2 2) (= 3 4))
This formula asks whether both the operands are true. Two does equal two, but three does not equal four, so the whole formula evaluates as false.
At least one of the equations 2 = 2 or 3 = 4 is true.
(or (= 2 2) (= 3 4))
This formula asks whether at least one of the operands is true. Three doesn't equal four, but two does equal two, so the whole formula evaluates as true.
The not operator works a little differently, only evaluating a single operand. A not formula will be true if the operand is false.
The equation 3 = 4 is not true.
(not (= 3 4)
Three doesn't equal four, so not formula is true.
A practical use for not formulas is writing a rule that excludes a specific loan status or portfolio:
(not (= loan-status "Closed - Paid Off"))
In that formula, loans with that status will evaluate as false, and the affect won't apply to them.
Utility functions perform special operations. They always start with u/ and will return different types of answers depending on the function.
For example, many utility functions do math with dates. You might want a formula that adds 7 days to a due date, showing when a grace period ends. But dates aren't normal numbers, so you can't just write (+ 7 9/17/2021). Instead, we use a utility function: (u/date-+ status-next-payment-date 7). The answer would be a date one week from the next payment date.
These articles explain where you can use Clojure in LMS: