The Feed Doctor on Combining Functions
Sorry that it’s been so long since my last post, but I do have a day job, you know. Here’s this week’s question, from “S.” in Piscataway, NJ.
Dear Feed Doctor,
I notice in a lot of your posts you have functions inside of functions. Can I do that with any function?
Well, S., the answer is: Yes. Mostly.
Let’s say I’ve written a rule that looks something like:
JOIN(" ",REGEXREPLACE($somefield,"something","else"),$anotherfield)
I’ve got a REGEXREPLACE “inside” a JOIN. In programming, we sometimes call this “nesting” or “chaining” function calls. All it means is that one of the inputs of the JOIN is the output of the REGEXREPLACE. The cool part is, functions don’t care where their inputs come from: text, fields, function outputs—it’s all the same to them. So that’s why the answer to S’s question is “Yes.”
But there’s a but. Functions don’t care where their inputs come from, but they do care what the input is. Specifically, they care about the data type of the input. In our system, there are the following data types:
- Booleans (i.e., True/False)
- Numbers
- Strings (i.e., text values inside quotation marks, like “this”)
Each function’s inputs can be restricted to take only certain types, though most functions will accept any type in their inputs. Also, each function’s output value has a type, and that’s what you have to keep in mind when you nest function calls.
Let’s look at an example. Our old friend LOOKUP has two inputs: the name of a list and a “key” to lookup. The name of the list must be some text value. So, this wouldn’t work:
LOOKUP(1,$somefield)
because the 1 is a number, and the first input to LOOKUP has to be a string. This wouldn’t work either:
LOOKUP(ISZERO($price),$somefield)
because ISZERO has a boolean output value.
In general, though, most of our functions aren’t too picky: as I said, they will often accept any type in their inputs, so the output of almost any function is an acceptable input to almost any function. One final note: inventory fields can masquerade as any type, so an inventory field will always work as a function input.
