compose and compose/deep for parse rule in red?

compose and compose/deep for parse rule in red?

如何在使用 compose 时保持(打印文本)消失?

s: {hello test1 parse test2}
t1: "test1"
t2: "test2"

rule: compose [ thru (t1) mark: copy text to (t2) (print text)]
parse s rule

compose/deep 的相同问题,如果答案不同:

s: {hello test1 parse test2. hello second test for test1 parse test2.}
t1: "test1"
t2: "test2"

rule: compose/deep [ any [thru (t1) mark: copy text to (t2) (print text)]]
parse s rule

您可以将块转换为括号,或引用括号:

>> compose [something here (to paren! [print text])]
== [something here (print text)]
>> compose [something here (quote (print text))]
== [something here (print text)]

compose/deep也是一样。

您不局限于盒子里的东西,因此您可以制作自己的类似 COMPOSE 的结构来执行其他模式。

例如,这里有一个 composeII,它只替代有嵌套括号的情况。所以它会单独留下 (x) 但评估 ((x)).

composeII: function [block /only /deep] [
    collect [
        foreach item block [
            case [
                all [
                    paren? :item
                    1 = length? item
                    paren? first item
                ][
                    either only [
                        keep/only do first item
                    ][
                        keep do first item
                    ]
                ]

                all [
                    deep
                    block? :item
                ][
                    either only [
                        keep/only composeII/deep/only item
                    ][
                        keep/only composeII/deep item
                    ]
                ]

                true [
                    keep/only :item
                ]
            ]
        ]
    ]
]

所以对于你的情况:

>> t1: "test1"
>> t2: "test2"

>> composeII/deep [
    any [thru ((t1)) mark: copy text to ((t2)) (print text)]
]

== [
    any [thru "test1" mark: copy text to "test2" (print text)]
]

由于块保护其内容不受 compose 的评估,您还可以使用

>> rule: compose [ thru (t1) mark: copy text to (t2) (first [(print text)] )]
== [thru "test1" mark: copy text to "test2" (print text)]