![]() |
#1 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Request for Comment: possible template function list_map()
Over the last while I have been asked a few times how to transform a list of values according to some rules, for example the latest posts in this thread. I have been wondering if this problem is general enough to merit direct support. Thus this RFC.
I propose a new function list_map() that takes a list of items then applies a series of rules to transform or not each item. It returns the transformed list. It does not modify the original list. The syntax: Code:
list_map(list_of_values, separator, rule [, rule]*) Code:
boolean to enable the rule, search expression for item, replacement expression Code:
'Banks' in $authors, '^Science Fiction' in $, 'SF' Putting it all together we have Code:
program: r = list_map($tags, ', ', 'Banks' in $authors, '^Science Fiction' in $, 'SF') Code:
program: r = list_map($tags, ', ', 'Banks' in $authors, '^Science Fiction' in $, 'SF', 1, 'Fiction' == $, '') Final example: I rewrote the example template in this post of the above mentioned thread to use the proposed list_map() function: Code:
program: # show = 'Criminal Minds, Law and Order'; show = 'Pretty Little Liars, Law and Order, Supergirl'; # show = 'Criminal Minds, Law and Order, Supergirl'; # show = $#show; ao3tags_pair = 'Alex / Emily, Olivia / Alex'; # ao3tags_pair = $#ao3tags_pair; show1 = '^Criminal Minds$' inlist show && '^Law and Order$' inlist show; show2 = '^Pretty Little Liars$' inlist show && '^Law and Order$' inlist show; show3 = '^Supergirl$' inlist show && '^Law and Order$' inlist show; output = list_map(ao3tags_pair, ',', show1, $ == "Alex / Emily", "Emily Prentiss / Alex Cabot", show2, $ == "Alex / Emily", "Emily Fields / Alex Cabot", show3, $ == "Olivia / Alex", "Alex Danvers / Olivia Benson"); output = list_sort(list_remove_duplicates(output, ','), 0, ',') Code:
Alex Danvers / Olivia Benson, Emily Fields / Alex Cabot Thoughts? Last edited by chaley; 01-03-2023 at 03:28 PM. Reason: Fix the separator in the last example |
![]() |
![]() |
![]() |
#2 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 10,974
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
If I understand right, here's one instance I may find it useful:
FanFicFare adds an additional 'crossover' tag to #fanficcat when there's multiple entries. For certain sets of entries, I don't want this. Since this isn't easy to work around in the FFF settings, I use a template to take the column, sort the entries, compare the string, and then replace if it matches: Code:
program: f = list_sort($#fanficcat,0,','); if contains(f, '^Crossover, Mass Effect Trilogy, Mass Effect: Andromeda$', '1', '') then 'Mass Effect Trilogy, Mass Effect: Andromeda' elif contains(f, '^Crossover, Half-Life, Portal$', '1', '') then 'Half-Life, Portal' else $#fanficcat fi Last edited by ownedbycats; 01-03-2023 at 04:32 PM. Reason: tidying up the template indents |
![]() |
![]() |
Advert | |
|
![]() |
#3 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Question: Assume existence of a function list_is_subset(s1, s2, sep) that returns true (a non-empty value) if all the items in s2 are in s1. In this case, would you expect this example of the function list_subset() function to return True? Code:
program: fanficcat = 'AAA, Crossover, DDD, Mass Effect Trilogy, Mass Effect: Andromeda'; list_is_subset(fanficcat, 'Crossover, Mass Effect Trilogy, Mass Effect: Andromeda', ',') |
|
![]() |
![]() |
![]() |
#4 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 10,974
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
But for list_is_subset, I'd expect it to return a 'true' for the example you provided.
But for my specific example, I was doing this for an exact match; e.g. if a book had Crossover, Half-Life, Mass Effect Trilogy, Mass Effect Andromeda then keep it as is. This template was done with Action Chains, so the second list excluding 'crossover' was actually overwriting the original. |
![]() |
![]() |
![]() |
#5 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
|
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 10,974
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
In all likelihood, probably because I didn't know what I was doing at the time. I'll take a look at that.
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
ePub version in template function? | tamaracks | Library Management | 8 | 06-17-2023 07:54 PM |
BuiltinAdd template language function | DyckBook | Editor | 4 | 10-19-2021 04:21 PM |
Using built-in template functions in a custom template function | ilovejedd | Library Management | 4 | 01-28-2018 12:20 PM |
Problem with contains function in save template | MicaOlaAdams | Calibre | 5 | 10-21-2016 10:25 AM |
how to use re() function in Template Program Mode? | msciwoj | Library Management | 3 | 07-07-2016 03:55 PM |