或者从另一个方向找到第一个非数字字符.
相同的函数是否适用于字符串和char*?
我正在玩模式匹配,我发现,将方法的匹配参数与空映射进行模式化并不容易.我认为它会是这样的:
defmodule PatternMatch do
def modify(%{}) do
%{}
end
def modify(map) do
# expensive operation
%{ modified: "map" }
end
end
Run Code Online (Sandbox Code Playgroud)
但似乎第一个函数子句匹配任意映射:
iex> PatternMatch.modify(%{a: "map"})
==> %{}
Run Code Online (Sandbox Code Playgroud)
还有另一种检查空地图的方法吗?
我发现自己陷入了一件非常微不足道的事情: - ]
我有一个枚举:
object Eny extends Enumeration {
type Eny = Value
val FOO, BAR, WOOZLE, DOOZLE = Value
}
Run Code Online (Sandbox Code Playgroud)
在代码中,我必须将它有条件地转换为数字(varianr-number对应关系因上下文而异).我写:
val en = BAR
val num = en match {
case FOO => 4
case BAR => 5
case WOOZLE => 6
case DOOZLE => 7
}
Run Code Online (Sandbox Code Playgroud)
这给了我每个分支的"无法访问代码"编译器错误,但无论第一个是什么(在这种情况下"情况FOO => 4").我究竟做错了什么?
有没有办法在vi(m)中生成数字序列?
例如,从文件中的随机行(在vim中打开),比如Row-i - 到随机行,比如Row-j,其中Row-i <Row-j,有没有办法从Row生成数字序列-i到Row-j从数字1开始到数字j-i + 1,步长增量为1?
假设我在文件中有以下行.
this is line #1
this is line #2
this is line #3
this is line #4
this is line #5
this is line #6
this is line #7
this is line #8
this is line #9
this is line #10
Run Code Online (Sandbox Code Playgroud)
我想要从第4行到第8行的数字序列作为前缀,从数字1到数字5开始.操作后,生成的文件应如下所示:
this is line #1
this is line #2
this is line #3
1 this is line #4
2 this is line #5
3 this is line #6
4 this is line …Run Code Online (Sandbox Code Playgroud) 在Emacs中匹配括号的命令是什么(相当于%Vim 中的命令)?
Python 3.10 中新的结构模式匹配功能是一个非常受欢迎的功能。有没有办法使用这个语句来匹配不等式?原型示例:
match a:
case < 42:
print('Less')
case == 42:
print('The answer')
case > 42:
print('Greater')
Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用匹配大小写而不是一百万个 IF 语句,但我尝试的任何操作都会返回错误:
match http_code:
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我还尝试过测试我发现的示例,这些示例也会返回此错误,包括以下错误:
http_code = "418"
match http_code:
case "200":
print("OK")
case "404":
print("Not Found")
case "418":
print("I'm a teapot")
case _:
print("Code not found")
Run Code Online (Sandbox Code Playgroud)
我知道匹配案例对于 python 来说是相当新的,但我使用的是 3.10,所以我不确定为什么它们总是返回这个错误。
给定一组字符串,例如:
EFgreen
EFgrey
EntireS1
EntireS2
J27RedP1
J27GreenP1
J27RedP2
J27GreenP2
JournalP1Black
JournalP1Blue
JournalP1Green
JournalP1Red
JournalP2Black
JournalP2Blue
JournalP2Green
Run Code Online (Sandbox Code Playgroud)
我希望能够检测到这些是三组文件:
有没有任何已知的方法来解决这个问题 - 我可以阅读任何已发表的论文吗?
我正在考虑的方法是每个字符串查看所有其他字符串并查找常见字符和不同字符的位置,尝试查找最常见的字符串集,但我担心这不是非常有效并且可能会给出误报.
请注意,这与"如何检测文件名中的常见字符串组"不同,因为它假定字符串后面始终有一系列数字.
我发现自己经常写这样的东西:
a match {
case `b` => // do stuff
case _ => // do nothing
}
Run Code Online (Sandbox Code Playgroud)
是否有更短的方法来检查某个值是否与模式匹配?我的意思是,在这种情况下我可以写if (a == b) // do stuff,但如果模式更复杂怎么办?就像匹配列表或任意复杂的任何模式时一样.我希望能够写出这样的东西:
if (a matches b) // do stuff
Run Code Online (Sandbox Code Playgroud)
我对Scala比较陌生,所以请原谅,如果我错过了大的东西:)
pattern-matching ×10
scala ×3
python ×2
algorithm ×1
c++ ×1
editor ×1
elixir ×1
emacs ×1
gnu ×1
linux ×1
python-3.10 ×1
scala-2.10 ×1
sequential ×1
string ×1
vi ×1
vim ×1