| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- #nop vim: set filetype=tt:;
- /*
- 本文件属于 PaoTin++ 的一部分。
- PaoTin++ © 2020~2023 的所有版权均由担子炮(dzp <danzipao@gmail.com>) 享有并保留一切法律权利
- 你可以在遵照 GPLv3 协议的基础之上使用、修改及重新分发本程序。
- */
- #nop 本文件是 xtintin 的一部分,实现了一些 FP 风格函数;
- ///=== {
- ///// FP 风格函数:
- //
- // #@ fp.Map <表格> <内容表达式>
- // 遍历表格,根据内容表达式重新生成新内容。
- // };
- #func {fp.Map} {
- #local table {%1};
- #local body {%2};
- #local key {};
- #local value {};
- #replace {body} {KEY} {\$key};
- #replace {body} {VALUE} {\$value};
- #local newTable {};
- #local key {};
- #foreach {*table[]} {key} {
- #local value {$table[$key]};
- #line sub {var;functions;escapes} #format value {%s} {$body};
- #cat newTable {{$key}{$value}};
- };
- #return {$newTable};
- };
- ///=== {
- // #@ fp.MapIf <表格> <条件表达式> <内容表达式>
- // 遍历表格,对符合条件的表格项,根据内容表达式重新生成新内容,不符合条件的保留原值。
- // };
- #func {fp.MapIf} {
- #local table {%1};
- #local cond {%2};
- #local body {%3};
- #local key {};
- #local value {};
- #replace {cond} {KEY} {\$key};
- #replace {cond} {VALUE} {\$value};
- #replace {body} {KEY} {\$key};
- #replace {body} {VALUE} {\$value};
- #local newTable {};
- #local key {};
- #foreach {*table[]} {key} {
- #local value {$table[$key]};
- #local c {};
- #line sub {var;functions;escapes} #format c {%s} {$cond};
- #if { $c } {
- #line sub {var;functions;escapes} #format value {%s} {$body};
- };
- #cat newTable {{$key}{$value}};
- };
- #return {$newTable};
- };
- ///=== {
- // #@ fp.Filter <表格> <条件表达式>
- // 遍历表格,根据过滤条件生成新表格。如果条件为真则收集,否则跳过。
- // };
- #func {fp.Filter} {
- #local table {%1};
- #local cond {%2};
- #local key {};
- #local value {};
- #replace {cond} {KEY} {\$key};
- #replace {cond} {VALUE} {\$value};
- #local newTable {};
- #local key {};
- #foreach {*table[]} {key} {
- #local value {$table[$key]};
- #local c {};
- #line sub {var;functions;escapes} #format c {%s} {$cond};
- #if { $c } {
- #cat newTable {{$key}{$value}};
- };
- };
- #return {$newTable};
- };
- ///=== {
- // #@ fp.FilterMap <表格> <条件表达式> <内容表达式>
- // 遍历表格,根据过滤条件生成新表格。如果条件为真则收集,否则跳过。
- // 收集的内容可以通过内容表达式重新生成。
- //
- ///// 上述函数中,条件表达式和内容表达式都可以通过 KEY 和 VALUE 关键字来指代表格项的 key 和 value。
- // };
- #func {fp.FilterMap} {
- #local table {%1};
- #local cond {%2};
- #local body {%3};
- #local key {};
- #local value {};
- #replace {cond} {KEY} {\$key};
- #replace {cond} {VALUE} {\$value};
- #replace {body} {KEY} {\$key};
- #replace {body} {VALUE} {\$value};
- #local newTable {};
- #local key {};
- #foreach {*table[]} {key} {
- #local value {$table[$key]};
- #local c {};
- #line sub {var;functions;escapes} #format c {%s} {$cond};
- #if { $c } {
- #line sub {var;functions;escapes} #format value {%s} {$body};
- #cat newTable {{$key}{$value}};
- };
- };
- #return {$newTable};
- };
- ///=== {
- // #@ fp.Transform <字符串列表> <内容表达式>
- // 遍历字符串列表,根据内容表达式重新生成新内容
- // };
- #func {fp.Transform} {
- #local strList {%1};
- #local body {%2};
- #local value {};
- #replace {body} {VALUE} {\$value};
- #local newStrList {};
- #foreach {$strList} {value} {
- #line sub {var;functions;escapes} #format value {%s} {$body};
- #cat newStrList {$value;};
- };
- #replace newStrList {;$} {};
- #return {$newStrList};
- };
- ///=== {
- // #@ fp.TransformIf <字符串列表> <条件表达式> <内容表达式>
- // 遍历字符串列表,对符合条件的项,根据内容表达式重新生成新内容,不符合条件的保留原值。
- // };
- #func {fp.TransformIf} {
- #local strList {%1};
- #local cond {%2};
- #local body {%3};
- #replace {cond} {VALUE} {\$value};
- #replace {body} {VALUE} {\$value};
- #local value {};
- #local newStrList {};
- #foreach {$strList} {value} {
- #local c {};
- #line sub {var;functions;escapes} #format c {%s} {$cond};
- #if { $c } {
- #line sub {var;functions;escapes} #format value {%s} {$body};
- };
- #cat newStrList {$value;};
- };
- #replace newStrList {;$} {};
- #return {$newStrList};
- };
- ///=== {
- // #@ fp.Select <字符串列表> <条件表达式>
- // 遍历字符串列表,根据过滤条件生成新字符串列表。如果条件为真则收集,否则跳过。
- // };
- #func {fp.Select} {
- #local strList {%1};
- #local cond {%2};
- #local value {};
- #replace {cond} {VALUE} {\$value};
- #local newStrList {};
- #local value {};
- #foreach {$strList} {value} {
- #local c {};
- #line sub {var;functions;escapes} #format c {%s} {$cond};
- #if { $c } {
- #cat newStrList {$value;};
- };
- };
- #replace newStrList {;$} {};
- #return {$newStrList};
- };
- ///=== {
- // #@ fp.SelectTransform <字符串列表> <条件表达式> <内容表达式>
- // 遍历字符串列表,根据过滤条件生成新字符串列表。如果条件为真则收集,否则跳过。
- // 收集的内容可以通过内容表达式重新生成。
- //
- ///// 上述函数中,条件表达式和内容表达式都可以通过 VALUE 关键字来指代字符串列表中的项。
- ///// 不难看出,上述四个函数与前面四个很像,只是针对字符串列表而不是表格。
- // };
- #func {fp.SelectTransform} {
- #local strList {%1};
- #local cond {%2};
- #local body {%3};
- #replace {cond} {VALUE} {\$value};
- #replace {body} {VALUE} {\$value};
- #local value {};
- #local newStrList {};
- #foreach {$strList} {value} {
- #local c {};
- #line sub {var;functions;escapes} #format c {%s} {$cond};
- #if { $c } {
- #line sub {var;functions;escapes} #format value {%s} {$body};
- #cat newStrList {$value;};
- };
- };
- #replace newStrList {;$} {};
- #return {$newStrList};
- };
|