ApplyToList ( function ; listOfValues ; separator )
Rate this function: Average rating: 3.7 (53 votes) Discuss this Custom Function
Debi Fuchs, Aptworks Consulting
http://www.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.
Sample Input:
ApplyToList(
"[n]^2";
"3¶2¶7";
¶
) 

Sample Output:

Description:
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: 13AUG2008 by Debi Fuchs of Aptworks Consulting
Note: these functions are not guaranteed
or supported by BrianDunning.com. Please contact the individual
developer with any questions or problems.
This is my Custom Function and I want to
edit it
Discuss:Make a comment about this Custom Function (please try to keep it brief & to the point). Anyone can post:

Newest Custom Functions:
1. 
SortList ( theList ) 

(Sat, Jun 23, 8:45am) 
2. 
HexUUIDToNum_cf (_uuid) 

(Sat, Jun 16, 1:55pm) 
3. 
httpResponseCode ( responseHeaders ) 

(Tue, Jun 12, 10:40pm) 
4. 
FindDuplicateCharacters ( string ) 

(Fri, Jun 08, 5:01pm) 
5. 
FieldRepetitionLast ( field ; maxRepetition ) 

(Wed, Jun 06, 6:05pm) 
6. 
CountModifiedRecords ( NameTimestampField ; StartTimestamp ; EndTimestamp ) 

(Mon, May 28, 8:23am) 
7. 
JSONArrayLength ( JSONArrayStr ) 

(Fri, May 25, 7:46am) 
8. 
interpolation (y1; y2; y3; x1; x2; x3; x1y1; x2y1; x1y2; x2y2; rnd) 

(Sat, May 19, 3:55am) 


Like CustomList, I find in brief tests that this function does not work on FileMaker GO.
Bruce Robertson, Redmond
August 21, 2010 2:52pm