Smart Function Parameters
We want to be able to set both a minimum win percentage, as well as a minimum number of votes for each of our votes. For example, we might want to make every vote have at least 10 yes and no vote, combined.
In addition, in order for a vote to pass, we could set a minimum threshhold of 50% or 60%.
We'll be creating both of these smart functions in the next lesson, but first we need to learn how to use parameters in our smart functions.
Params in Smart Functions
When we write the function minWinPercentage
, we don't want to hard-code the win percentage. We want to be able to re-use this same smart function for different predicates, and be able to require different minimum win percentages for different predicates.
In order to achieve these, we need to specify _fn/params
when we create our smart function.
_fn/params
expects an array of strings. The strings that are specified in params can be used in _fn/code
.
For example, this function will add the numbers. a
and b
.
[
{
"_id": "_fn",
"name": "addTwoNumbers",
"params": ["a", "b"],
"code": "(+ a b)"
}
]
When we call, addTwoNumbers
, it expects two parameters, otherwise it has nothing to add. For example, we could call it, (addNumbers 105 10)
. (This smart function is trivial, because the built-in +
is similar to addNumbers
.)
We can look at another smart function, addAndDivide
.
[
{
"_id": "_fn",
"name": "addAndDivide",
"params": ["a", "b", "c"],
"code": "(/ (+ a b) c)"
}
]
How Would You Call addAndDivide?
Call `addAndDivide`, so that it would return 30.