ApplyToList ( function ; listOfValues ; separator )
Rate this function: Average rating: 3.2 (14 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: 13-AUG-2008 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:There are no comments yet. Be the first to post a comment about this Custom Function! Please try to keep it brief & to the point. Anyone can post:
|
Newest Custom Functions:
| 1. |
Bin2Hex ( binary ) |
| |
(Sat, Mar 13, 10:08pm) |
| 2. |
SlideView (text) |
| |
(Sat, Mar 13, 8:40pm) |
| 3. |
HashFNV1a64 ( text ; empty ) |
| |
(Sat, Mar 13, 6:23pm) |
| 4. |
HashFNV1a32 ( text ; empty ) |
| |
(Sat, Mar 13, 5:55pm) |
| 5. |
XORbin( bin0 ; bin1 ; "" ) |
| |
(Sat, Mar 13, 4:35pm) |
| 6. |
cfMatch ( value_to_test; match_Value; match_operator ) |
| |
(Thu, Mar 11, 10:45pm) |
| 7. |
DeleteMiddleText ( Value ; StringStart ; StringEnd ) |
| |
(Wed, Mar 10, 8:27am) |
| 8. |
Codify( theText; code_option ) |
| |
(Sun, Mar 07, 8:17pm) |
 |
|