我有一个应用程序(除其他外)支持纯文本搜索和使用 Lua 模式的搜索。为了方便起见,该应用程序支持不区分大小写的搜索。这是一个图像片段:

将给定 Lua 模式转换为不区分大小写的 Lua 模式的代码不太漂亮。它主要关心字符前面是否有奇数或偶数转义符 (%) 以及它是否位于方括号内。图像中显示的图案变为%a[bB][bB]%%[cC][%abB%%cC]
我还没有机会学习 LPeg,我想这可能是我的动力。
我的问题是,这是否是 LPeg 可以轻松处理的事情?
考虑以下代码 - 它可以编译并工作:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::{Something, Nothing};
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wrapped = Rc::new(RefCell::new(Foo::Nothing));
//....
match *wrapped.borrow() {
Something(x) => println!("{}", x),
Nothing => println!("Nothing"),
};
}
Run Code Online (Sandbox Code Playgroud)
现在我想匹配两个包装的值而不是一个:
use std::rc::Rc;
use std::cell::RefCell;
use crate::Foo::{Something, Nothing};
enum Foo {
Nothing,
Something(i32),
}
fn main() {
let wrapped1 = Rc::new(RefCell::new(Foo::Nothing));
let wrapped2 = Rc::new(RefCell::new(Foo::Nothing));
//....
match (*wrapped1.borrow(), *wrapped2.borrow()) {
(Something(x), Something(y)) => println!("{}, {}", x, y),
_ => println!("Nothing"),
};
}
Run Code Online (Sandbox Code Playgroud)
现在这将给出编译错误:
use …Run Code Online (Sandbox Code Playgroud) 我有一个Vec值,并且想要filter那些匹配特定模式的值。
什么是惯用的办法只有检查,如果一个表达式匹配的模式,而不必做一些与比赛?
enum Kind {
A,
B(char),
}
let vector: Vec<Option<Kind>> = ...;
vector.filter(|item| /* check if item matches pattern Some(Kind::B(_)) */)
Run Code Online (Sandbox Code Playgroud)
我知道我可以使用match关键字:
vector.filter(|item| match item {
Some(Kind::B(_)) => true,
_ => false,
})
Run Code Online (Sandbox Code Playgroud)
或if let关键字:
vector.filter(|item| {
if let Some(Kind::B(_)) = item {
true
} else {
false
}
})
Run Code Online (Sandbox Code Playgroud)
但是在这两个示例中,代码看起来仍然很庞大,因为我需要手动提供true和false常量。我觉得应该有一种更优雅的方式来做到这一点,类似于以下内容:
vector.filter(|item| matches(item, Some(Kind::B(_))))
Run Code Online (Sandbox Code Playgroud) 我想知道触发默认情况时模式匹配中对象的类型。
这是我尝试过的:
byeBuffer = array(0) match {
case _: Int =>
ByteBuffer.allocate(4 * array.length)
case _: Long =>
ByteBuffer.allocate(8 * array.length)
case _: Float =>
ByteBuffer.allocate(4 * array.length)
case _: Double =>
ByteBuffer.allocate(8 * array.length)
case _: Boolean =>
ByteBuffer.allocate(1 * array.length)
case _ => throw new UnsupportedOperationException("Type not supported: " + _.getClass())
}
Run Code Online (Sandbox Code Playgroud)
但它说“无法解析符号getClass”。
在Scala 规范中有 14 个“命名”模式的定义(不包括“模式绑定器”),我无法确定哪个是以下模式的正确类别:
List("A", "B", "C") match {
case head :: Nil => ???
case "A" :: _ :: Nil => ???
case head :: tail => ???
}
Run Code Online (Sandbox Code Playgroud)
这纯粹是学术性的,因为我希望能够参考规范中的命名模式。因此,问题是关于如何解释规范以及在针对列表引用这些类型的模式时如何准确。
例如 1,它是“ 11. 中缀操作模式”作为::(又名Cons)正在使用中缀吗?或“ 7. Constructor Pattern ”作为::构造函数?或者如果我们通过在列表的特定位置(L3)使用文字使它复杂化,是否会调用“ 4. Literal Patterns ”?
也许是一个更简单的案例?
List(1, 4, 2, 5) match {
case Nil => ???
case head :: tail => ???
}
Run Code Online (Sandbox Code Playgroud)
我们会根据使用的模式命名它吗?
使用 GADT,我定义了一个深度索引树数据类型(2-3 tree)。深度是静态地确保树木平衡的。
-- Natural numbers
data Nat = Z | S Nat
-- Depth-indexed 2-3 tree
data DT :: Nat -> Type -> Type where
-- Pattern of node names: N{#subtrees}_{#containedValues}
N0_0 :: DT Z a
N2_1 :: DT n a -> a -> DT n a
-> DT (S n) a
N3_2 :: DT n a -> a -> DT n a -> a -> DT n a
-> DT (S n) a
deriving instance Eq …Run Code Online (Sandbox Code Playgroud) im in the process of writing a transposing recursive function and i have stopped at a problem. So i want to have a check using match by calling isTable function to verify that the input M is a valid table, however it errors and im not sure how to fix it
let isTable list =
match List.map List.length list |> List.distinct |> List.length with
| 1 -> true
| _ -> false
let rec transpose M =
match M with …Run Code Online (Sandbox Code Playgroud) 如何对string option refOCaml中具有类型的变量进行模式匹配。我需要提取这个变量的字符串部分,但我无法让它工作。
我是 Haskell 的新手,我正在尝试使用模式匹配做一些事情。我什至不知道这种方式是否可行。我有一个数据 Fml :
data Fml a = And (Fml a) (Fml a)
| NAnd (Fml a) (Fml a)
| Or (Fml a) (Fml a)
| NOr (Fml a) (Fml a)
| XOr (Fml a) (Fml a)
| XNOr (Fml a) (Fml a)
| Imply (Fml a) (Fml a)
| Equiv (Fml a) (Fml a)
| Not (Fml a)
| Final (Var.Var a)
deriving (Show)
Run Code Online (Sandbox Code Playgroud)
Var 只是一个值(例如:1)我正在尝试制作一个简单的函数来将所有 Var 合并到一个数组中。我已经这样做了:
getVar :: (Eq a) => Fml a -> [Fml a] …Run Code Online (Sandbox Code Playgroud) sealed trait User
case class ExternalUser(name: String, email: String) extends User
case class InternalUser(tag: String, email: String) extends User
val user: User = ...
user match {
case u: ExternalUser =>
println(s"${u.name}")
case InternalUser(tag, email) =>
println(s"${tag}")
}
Run Code Online (Sandbox Code Playgroud)
我什么时候应该使用一个来反对另一个,为什么。