我想与包含许多(.*)捕获组的以编程方式构建的正则表达式进行匹配。我有这个正则表达式作为一个字符串,说
my $rx = "(.*)a(.*)b(.*)"
Run Code Online (Sandbox Code Playgroud)
我想将该字符串插入为正则表达式并与之匹配。该文档告诉我<$rx>应该做的伎俩(即插补该字符串作为一个正则表达式),但事实并非如此。比较匹配的输出(在perl6REPL 中):
> 'xaybz' ~~ rx/<$rx>/
?xaybz?
Run Code Online (Sandbox Code Playgroud)
与预期/所需输出相比,将捕获组分开:
> 'xaybz' ~~ rx/(.*)a(.*)b(.*)/
?xaybz?
0 => ?x?
1 => ?y?
2 => ?z?
Run Code Online (Sandbox Code Playgroud)
注释
我可以这样做的一种不吸引人的方法是评估我的正则表达式匹配(也在 REPL 中):
> use MONKEY; EVAL "'xaybz' ~~ rx/$rx/";
?xaybz?
0 => ?x?
1 => ?y?
2 => ?z?
Run Code Online (Sandbox Code Playgroud)
因此,虽然这确实给了我一个解决方案,但我确信我缺少一个字符串插值技巧,它可以避免依赖EVAL..
我最近摆弄问题14的Euler project:该范围内数1..1_000_000的最长的在Collatz序列?
我知道必须记忆以获得合理时间的问题,并且以下Python代码使用该技术相对快速地返回答案(记忆到字典):
#!/usr/bin/env python
L = 1_000_000
cllens={1:1}
cltz = lambda n: 3*n + 1 if n%2 else n//2
def cllen(n):
if n not in cllens: cllens[n] = cllen(cltz(n)) + 1
return cllens[n]
maxn=1
for i in range(1,L+1):
ln=cllen(i)
if (ln > cllens[maxn]): maxn=i
print(maxn)
Run Code Online (Sandbox Code Playgroud)
(从这里改编;我更喜欢这个不使用的版本max,因为我可能想摆弄它以返回最长的 10 个序列等)。
我试图将其翻译为尽可能Raku保持语义上的接近:
#!/usr/bin/env perl6
use v6;
my $L=1_000_000;
my %cllens = (1 => 1); …Run Code Online (Sandbox Code Playgroud) 我试图定义一个子程序Raku,其理由是,比方说,一个阵列的诠释S(强加的约束,即拒绝那些参数不是 ArrayS的IntS)。
问题:实现这一目标的“最佳”方式是什么(最惯用的,或最直接的,或者您认为“最佳”在这里应该是什么意思)?
在RakuREPL 中运行的示例如下。
我所希望的会奏效
> sub f(Int @a) {1}
&f
> f([1,2,3])
Type check failed in binding to parameter '@a'; expected Positional[Int] but got Array ([1, 2, 3])
in sub f at <unknown file> line 1
in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
另一个非工作示例
> sub f(@a where *.all ~~ Int) {1}
&f
> f([1,2,3])
Constraint type check failed in binding to parameter '@a'; …Run Code Online (Sandbox Code Playgroud) 这主要是为了了解Raku有多棒。
题
是否有内置方法可以获取列表并无限期地循环遍历它,例如生成惰性列表
a, b, c, a, b, c, ...
Run Code Online (Sandbox Code Playgroud)
出(a, b, c)?列表文档中的任何内容似乎都没有明显的作用。
可能的解决方案
我至少能想到一对。
更实际的方法是映射 @array[<variable> mod length-of-@array]惰性范围0..Inf。在perl6REPL 中:
> my @ar=<a b c>
[a b c]
> (0..Inf).map({ @ar[$_ % @ar.elems] }).[0..100]
(a b c a b c a b c a b c a b c a b c a b c a b c a b c a b c a b …Run Code Online (Sandbox Code Playgroud) 只是为了它,我试图用raku 中的运算符(在该页面上搜索)匹配正则表达式的连接点。m// Explicit topic match
在perl6REPL 中:
> any('a','b') ~~ m/./
False
Run Code Online (Sandbox Code Playgroud)
之后,无论我怎么称呼,m//我都会收到一个不可变的匹配投诉:
> 'x' ~~ m/./
Cannot modify an immutable Match (?a?)
in block <unit> at <unknown file> line 1
Run Code Online (Sandbox Code Playgroud)
题
这里的幕后发生了什么?
讨论
问题似乎源于将$/ 特殊变量设置为结点
any(?a?, ?b?)
Run Code Online (Sandbox Code Playgroud)
在路口比赛之后,似乎是?a?在路口引起了投诉。
只要我做任何改变$/其他事情的事情,功能就会恢复:
> $/=Any
(Any)
> 'x' ~~ m/./
?x?
Run Code Online (Sandbox Code Playgroud)
或者
> 'x' ~~ /./
?x?
> 'x' ~~ m/./
?x?
Run Code Online (Sandbox Code Playgroud)
(所以先匹配//,以便改变$/,然后 …
我希望能够将我的脚本传递到.rakumod文件的路径,例如<blah>/Mod.rakumod,并且能够以散列形式访问符号表,就像我use改为使用模块一样:
模块:
$ cat Mod.rakumod
unit module Mod;
sub bag is export { ... }
Run Code Online (Sandbox Code Playgroud)
use lib <dir-containing-Mod>
use Mod;
say Mod::EXPORT::.keys
Run Code Online (Sandbox Code Playgroud)
正如预期的那样工作,返回(ALL DEFAULT)。
另一方面:
use lib <dir-containing-Mod>
require Mod;
say Mod::EXPORT::.keys
Run Code Online (Sandbox Code Playgroud)
失败
Could not find symbol '&EXPORT' in 'Mod'
in block <unit> at <blah>
Run Code Online (Sandbox Code Playgroud)
尽管事实上,即使require,say Mod::.keys确实看到EXPORT:
use lib <dir-containing-Mod>
require Mod;
say Mod::.keys
---
(EXPORT Mod)
Run Code Online (Sandbox Code Playgroud)
我需要使用require来使这个动态,因为我不知道我想要哪个模块。
我其实可以 …
我正在尝试使用 Python 的Selenium Webdriver以及Firefox位于<PROFILE-DIR>.
我尝试过的
#!/usr/bin/env python
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver import Firefox, DesiredCapabilities
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
options = Options()
options.profile = '<PROFILE_DIR>'
webdriver = Firefox(options=options)
Run Code Online (Sandbox Code Playgroud)
这会将现有配置文件复制到临时位置。我可以看到它有效,因为我启动的新会话可以访问配置文件的旧cookie等。但这不是我想要的:我想就地使用配置文件。
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['args'] = '--profile <PROFILE-DIR>'
webdriver = Firefox(desired_capabilities=capabilities)
Run Code Online (Sandbox Code Playgroud)
什么也没做:关闭会话后查看geckodriver.log仍然显示类似的内容Running command: "/usr/bin/firefox" "--marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofileOFKY46",即仍在使用临时配置文件(它甚至不是 …
raku ×6
python ×2
regex ×2
collatz ×1
constraints ×1
firefox ×1
junction ×1
list ×1
match ×1
memoization ×1
module ×1
rakudo ×1
regex-group ×1
types ×1