是否可以部分匹配a tuple而无需指定大小?例如,如果我有一个元组
val v = ( "Dr", "John","H", "Watson")
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这样的事情:
v match {
case ( "Dr", : _*) => "What's up, Doc?"
case ( "Mr", name, :_*) => s"Welcome, Mr. ${name}"
case _ => "Have we met?"
}
Run Code Online (Sandbox Code Playgroud)
这不编译,:_*通常意味着未确定数量的参数,但显然不能在这种情况下使用.我的想法是能够将这个匹配器用于任何大于2的元组.我知道我可以先将它转换v为List(例如),只是想知道是否有可能用它做tuple.
编辑:我在网上找到的信息最多是本次讨论,其历史可以追溯到2.8阶,所以我会用"不,你不能"的答案.
尝试鱼贝,所以我翻译我的bash功能.问题是,在一种情况下,我正在使用bash正则表达式来检查字符串是否与正则表达式匹配.我无法弄清楚如何把它翻译成鱼.
这是我的例子.
if [[ "$arg" =~ ^[0-9]+$ ]]
...
Run Code Online (Sandbox Code Playgroud)
那么,在*nix中是否有一种方法可以检查字符串是否与正则表达式匹配,所以我可以将它放入条件中?
这是我目前有效的,但我不满意:
# kill jobs by job number, or range of job numbers
# example: k 1 2 5
# example: k 1..5
# example: k 1..5 7 10..15
# example: k 1-5 7 10-15
function k
for arg in $argv
if ruby -e "exit ('$arg' =~ /^[0-9]+\$/ ? 0 : 1)"
kill -9 %$arg
else
set _start (echo …Run Code Online (Sandbox Code Playgroud) 如何在bash中相互同步读取/处理2个文件?
我有2个文本文件,其中包含相同数量的行/项.一个文件是
a
b
c
Run Code Online (Sandbox Code Playgroud)
另一个文件是
1
2
3
Run Code Online (Sandbox Code Playgroud)
如何同步循环这些文件,以便a与1b-> 2,c-> 3 相关联?
我以为我可以将文件作为数组读入,然后使用索引处理它们,但似乎我的语法/逻辑不正确.
这样做f1=$(cat file1)品牌f1 = a b c.我认为f1=($(cat file1))这会使它成为一个数组,但它会使f1=a我无法处理数组.
如果有人想知道我搞砸的代码是什么:
hostnames=($(cat $host_file))
# trying to read in as an array, which apparently is incorrect
roles=($(cat $role_file))
for i in {0..3}
do
echo ${hostnames[$i]}
# wanted to iterate through each element in the file/array
# but there is only one object instead of N objects
echo ${roles[$i]} …Run Code Online (Sandbox Code Playgroud) 如果我通过创建此索引将users.email上的常规唯一索引更改为不区分大小写的索引:
CREATE UNIQUE INDEX user_email_ci_idx ON users ((lower(email)));
Run Code Online (Sandbox Code Playgroud)
有没有理由不放弃先前的区分大小写的索引?
我猜想postgres将切换到新索引,结果集的性能将是相同的.
我可以通过获取向量的切片和对元组中项目的引用来解构元组向量:
let items = vec![("Peter".to_string(), 180)];
if let [(ref name, ref age)] = items.as_slice() {
println!("{} scored {}", name, age);
};
Run Code Online (Sandbox Code Playgroud)
如何直接解构向量,将项目移出元组。像这样的东西:
let items = vec![("Peter".to_string(), 180)];
if let [(name, age)] = items {
println!("{} scored {}", name, age);
};
Run Code Online (Sandbox Code Playgroud)
编译上述结果导致错误:
let items = vec![("Peter".to_string(), 180)];
if let [(ref name, ref age)] = items.as_slice() {
println!("{} scored {}", name, age);
};
Run Code Online (Sandbox Code Playgroud) 我有以下数据框:
----------------------------
Index| col1 | col2 |
----------------------------
0 | 1 | a-b-c
1 | 2 | d-e-f
2 | 3 | g
----------------------------
Run Code Online (Sandbox Code Playgroud)
我希望能够进行如下查询:
myvar= 'a'
df.query('@myvar in col2')
Run Code Online (Sandbox Code Playgroud)
但它总是适用于精确匹配。模式匹配有什么解决方案吗?
谢谢,
鲁特
我正在尝试得到这样的东西(不起作用):
match input {
"next" => current_question_number += 1,
"prev" => current_question_number -= 1,
"goto {x}" => current_question_number = x,
// ...
_ => status = "Unknown Command".to_owned()
}
Run Code Online (Sandbox Code Playgroud)
我尝试了两个不同版本的Regex:
go_match = regex::Regex::new(r"goto (\d+)?").unwrap();
// ...
match input {
...
x if go_match.is_match(x) => current_question_number = go_match.captures(x).unwrap().get(1).unwrap().as_str().parse().unwrap(),
_ => status = "Unknown Command".to_owned()
}
Run Code Online (Sandbox Code Playgroud)
和
let cmd_match = regex::Regex::new(r"([a-zA-Z]+) (\d+)?").unwrap();
// ...
if let Some(captures) = cmd_match.captures(input.as_ref()) {
let cmd = captures.get(1).unwrap().as_str().to_lowercase();
if let Some(param) = captures.get(2) { …Run Code Online (Sandbox Code Playgroud) 在 C# 7.1 中,以下是有效代码:
object o = new object();
switch (o)
{
case CustomerRequestBase c:
//do something
break;
}
Run Code Online (Sandbox Code Playgroud)
但是,我想在以下场景中使用模式 switch 语句:
public T Process<T>(object message, IMessageFormatter messageFormatter)
where T : class, IStandardMessageModel, new()
{
switch (T)
{
case CustomerRequestBase c:
//do something
break;
}
}
Run Code Online (Sandbox Code Playgroud)
IDE 给我错误“'T' 是一种类型,在给定的上下文中无效”有没有一种优雅的方法来切换泛型参数的类型?我知道在我的第一个例子中你正在打开对象,第二个我想打开类型 T。这样做的最佳方法是什么?
当类型参数来自封闭方法而不是封闭类时,为什么模式匹配的工作方式不同?例如,
trait Base[T]
case class Derived(v: Int) extends Base[Int]
class Test[A] {
def method(arg: Base[A]) = {
arg match {
case Derived(_) => 42
}
}
}
Run Code Online (Sandbox Code Playgroud)
给出错误
constructor cannot be instantiated to expected type;
found : A$A87.this.Derived
required: A$A87.this.Base[A]
case Derived(_) => 42
^
Run Code Online (Sandbox Code Playgroud)
虽然它成功编译时A是方法类型参数
class Test {
def method[A](arg: Base[A]) = {
arg match {
case Derived(_) => 42
}
}
}
Run Code Online (Sandbox Code Playgroud)