Brian Dunning's FileMaker Custom Functions

ApplyToList ( function ; listOfValues ; separator )

Speedily (using limited recursion and avoiding GetValue) apply an "inline" function across a list of up to one million items, handling them either as numbers/expression or as text.

  Average rating: 3.7 (52 votes) Log in to vote

Debi Fuchs   Debi Fuchs - Show more from this author
Aptworks Consulting
http://www.aptworks.com

Share on Facebook Share on Twitter

  Sample input:
ApplyToList(
"[n]^2";
"3¶2¶7";

)
  Sample output:
"9¶4¶49"

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

CUSTOM FUNCTION: ApplyToList ( function ; listOfValues ; separator )
© 2008 Debi Fuchs of Aptworks Consulting, debi@aptworks.com

Speedily (using limited recursion and avoiding GetValue) apply an "inline" function across a list of up to one million items, handling them either as numbers/expression or as text.

EXAMPLES:
ApplyToList( "[n]^2"; "3¶2¶7"; ¶ )
// --> "9¶4¶49"

ApplyToList( "Length([t])"; "Robin¶Pat¶Chris"; ¶)
// --> "5¶3¶5"

Let(
number_me = "[i]&\". \"&[t]";
ApplyToList( number_me; "Robin¶Pat¶Chris"; ¶)
)
// --> "1. Robin¶2. Pat¶3. Chris"

Let(
add_30 = "GetAsDate([n])+30";
ApplyToList( add_30; "Date(1;1;2000)¶Date(1;15;2000)"; ¶)
)
// --> "1/31/2000¶2/14/2000"


CREDITS: Thanks to Agnès Barouh for the idea of the "[n]" style function call syntax, to Ray Cologon for ideas about local variables within custom functions, and to Darren Terry for inspring this function.

USAGE: ApplyToList returns the results, delimited by "separator", of repeatedly evaluating the string "function" across the list "listOfValues", each time assigning to placeholders "[t]" or "[n]" one item from the list. Use "[t]" to treat items as text; Use "[n]" to evaluate them before application. For convenience in creating numbered lists, use placeholder "[i]" to represent the position of the item within the list, starting with an index of 1. IMPORTANT: Use "[n]" over "[t]" where possible, as this is faster, and do NOT use BOTH "[n]" and "[t]" placeholders (or BOTH "[t]" and "evaluate([t])" in one function), as this slows the calculation engine to a crawl.

EXAMPLES:

ApplyToList( "[t] & 1"; "a¶b¶¶c"; ¶)
// --> ("a" & 1) & ¶ & ("b" & 1) & ¶ & ("" & 1) & ¶ & ("c" & 1)
// --> "a1¶b1¶1¶d1"

ApplyToList( "Length([t])"; "Robin¶Pat"; ¶)
// --> (Length("Robin")) & ¶ & (Length("Pat"))
// --> "5¶3"

ApplyToList( "Length([n])"; "Robin¶Pat"; ¶)
// --> (Length(Robin)) & ¶ & (Length(Pat))
// --> "[ERROR]"

ApplyToList("[n] + [n]"; "01+2¶3+2"; ¶)
// --> (01+2 + 01+2) & ¶ & (3+2 + 3+2)
// --> "6¶10"

ApplyToList("[t] + [t]"; "01+2¶3+2"; ¶)
// --> ("01+2" + "01+2") & ¶ & ("3+2" + "3+2")
// --> (12 + 12) & ¶ & (32 + 32)
// --> "24¶64"

THIS IS VERY VERY VERY (!!!!!) SLOW... (see above)
ApplyToList("[t] + [n]"; "01+2¶3+2"; ¶)
// --> ("01+2" + 01+2) & ¶ & ("3+2" + 3+2)
// --> (12 + 01+2) & ¶ & (32 + 3+2)
// --> "15¶37"

THIS IS VERY VERY VERY (!!!!!) SLOW... (see above)
ApplyToList("[t] + evaluate([t])"; "01+2¶3+2"; ¶)
// --> ("01+2" + evaluate("01+2") & ¶ & ("3+2" + evalute("3+2"))
// --> ("01+2" + 3) & ¶ & ("3+2" + 5)
// --> (12 + 3) & ¶ & (32 + 5)
// --> "15¶37"


IMPLEMENTATION: ApplyToList creates long strings of Let statements and then evaluates them. It does not recurse at all unless it is called on a list larger than 1000 items, and then it does so in binary fashion (calling itself twice not once). Thus, the CF stack grows logarithmically, reaching (for example) a depth of only 10 CF stack frames and 1000 CF calls when called on a list of one million items. This leaves room to use other custom functions (including ApplyToList) within the "function" argument of ApplyToList. ApplyToList is maximized for speed. Suggestions for further optimization welcome!

NOTE: See "ApplyToRange" to apply a function to a range of numbers. See "Apply" to apply a function to a single value.

LAST MODIFIED: 13-AUG-2008 by Debi Fuchs of Aptworks Consulting

 

Comments

Bruce Robertson   Bruce Robertson
Aug 21, 2010
Like CustomList, I find in brief tests that this function does not work on FileMaker GO.
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: