The Strange replace Filter Operator Syntax

In https://tiddlywiki.com/#replace%20Operator it states replace marker with N trailing items. Its syntax and usages is quite confusing to me. See https://tiddlywiki.com/#replace%20Operator%20(Examples)

[list[Days of the Week]] Last +[replace:2[Tuesday]]

Do you have better example and explanation for it? Why it is not like below

[<thisList>replace[old],[new]]

change old with new in the list. or

[<thisList>replace:2[new]]

replace second item with new

I have no idea why it is as it is, but I think this ASCII diagram might explain a bit. I apologize for anyone on a small screen.

               [enlist:raw[A B C D E D C B A]]  X Y Z +[replace:3[C]]
                `----------------------------' '-----'         /   \
                               /                 /            /     `+-- item to cut
              original list +-'                 /            /              
                                               /            / 
                 replacements to be inserted--'            /            
                                                          /  
         number of items to cut from the end and insert--+  


                    +--- (1) Initial list     +-- (4) mark C for replacement
                   /                         /
                  /                         /       +-- (5) move cut items to spot held by C
                 /                         /       /
                A          A        A      A       A        A
                B          B        B      B       B        B
                C          C        C    --C--       <-+    X
                D          D        D      D       D   |    Y
                E          E        E      E       E   |    Z
                D          D        D      D       D   |    D
                C          C        C      C*      C*  |    E
                B          B        B      B       B   |    D
                A          A        A      A       A   |    C * only first instance replaced
                           X                           |    B
                           Y       |X|    |X|     |X|  |    A
                           Z       |Y|    |Y|     |Y|--'     \
                          /        |Z|    |Z|     |Z|         `+-- (6) Final list
(2) append replacements--+            \
                                       `+-- (3) cut 3 from end of list
3 Likes

Thank you Scott!

I dont know why it is that complex and I have not seen in real world in TiddlyWiki.

I’m pretty sure it’s loosely modeled on maybe the worst built-in method of core JS: splice. It’s not an exact match, as that api is indexed based and allows you to replace an entire sub-array, but it feels similar.

I never use splice, and I’ve never found any need for replace. I’m sure someone has.

1 Like