I have a filter that converts a string by replacing each char that is a digit with a string consisting of dots as long as that number:
<$let str="8x7x6x5x4x3x2x1">
{{{ [<str>
search-replace[8],[........]
search-replace[7],[.......]
search-replace[6],[......]
search-replace[5],[.....]
search-replace[4],[....]
search-replace[3],[...]
search-replace[2],[..]
search-replace[1],[.]
] }}}
</$let>
I’ve split it to multiple lines to show that intuitively there’s potential for refactoring here - this is just a lot of copypasted code. But I have no idea how to approach it: I barely start to understand the idea of list as a loop, while here I see a nested, two level loop.
A second question is about performance. Does my ugly filter above do one additional pass for each search-replace
operator? I could probably implement this algorithm in one pass using a Python oneliner.
Edit: not sure it’s one pass, but it’s indeed an oneliner:
>>> ''.join([''.join(['.'] * int(x)) if x in '87654321' else x for x in '8x7x6x5x4x3x2x1'])
'........x.......x......x.....x....x...x..x.'