我有一个功能:
ncount <- function(num = NULL) {
toRead <- readLines("abc.txt")
n <- as.character(num)
x <- grep("{"n"} number",toRead,value=TRUE)
}
Run Code Online (Sandbox Code Playgroud)
在 grep 时,我希望在函数中传递的 num 来动态创建要搜索的模式?在 R 中如何做到这一点?文本文件每行都有数字和文本
我正在 Erlang 中制作一个游戏服务器,我希望能够为每个连接的客户端拥有一个进程。该进程应该负责处理客户端发送到服务器的所有 UDP 消息,处理这些消息并在必要时进行回复。
我该如何实现这一目标?我知道使用 TCP 我可以放置任意数量的侦听器来运行该函数
gen_tcp:accept(ListenSocket)
Run Code Online (Sandbox Code Playgroud)
它会阻塞工作人员,直到有工作可用,等等。我也希望 udp 具有这种行为,但我一直在尝试制作一个简单的示例,该示例会生成一个新进程,该进程应该根据其 ip 和每个 udp 数据包来匹配端口,查看哪个玩家发送了它,将其占为己有并忽略其余的。
loop({Sock, Ip, Port}) ->
receive
{udp, Sock, Ip, Port, Msg} ->
do_stuff & loop, etc.
end
end.
Run Code Online (Sandbox Code Playgroud)
这与从循环到消息的 Ip 匹配吗?为了实现这一点,我需要任何其他 UDP 侦听器来与该特定数据包不匹配,以便工作程序在任何给定时间获取它应该获取的每条消息,对吗?如果另一个“通用”UDP 侦听器侦听,它可能会在工作人员之前拾取消息。
所以我问这种方法是否有效,或者是否会收到错误消息,指出它不是套接字的所有者或类似的内容。到目前为止,我还没有让任何工作人员实际接收任何消息,所以我不确定这是因为不匹配还是因为套接字只向当前控制进程发送消息,我想这会使我的计划变得不可能。
我正在尝试匹配从解构的枚举变体构造的元组,如下所示:
enum Enum {
Var {
field1: Option<String>,
field2: Option<String>
}
}
let var = Enum::Var {
field1: Some("aaa".to_string()),
field2: None
}
match var {
Enum::Var {field1, field2} => {
match (field1, field2) {
(Some("aaa".to_string()), None) => (),
_ => panic!()
}
}
_ => panic!()
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到错误消息
error: expected one of `)`, `,`, `...`, `..=`, `..`, or `|`, found `.`
--> src/parser/test_admonitions.rs:61:22
|
61 | (Some("aaa".to_string()), None) => {}
| ^
| |
| expected one of `)`, `,`, …Run Code Online (Sandbox Code Playgroud) 我想做以下替换:用符号 #替换$位于 2 之间的单词$,如果这些单词在 2 之间,则不执行任何操作$$。
例如,在本文中:
Currently, we are able to make cross correlations (understand "combine" to have better constraints on cosmological parameters) between weak lensing (WL) and photometric Galaxy clustering (GCph). When I say combine, as in the case where I have 2 sets of different measures ($\tau_1, \sigma_1$) and ($\tau_2, \sigma_2$), well, if I consider the Gaussian errors, it is shown quite easily (by Maximum Likelihood Estimation) that the estimator $\sigma_{\hat …Run Code Online (Sandbox Code Playgroud) 使用此代码 println 将仅针对指定的异常执行。我想知道是否可以否定该行以使其对未指定的所有其他异常执行。我知道可以使用 2 个案例,但我想知道是否可以使用一个案例来完成。
val myHandler: PartialFunction[Throwable, Unit] = {
case e @ (_: MappingException | _: ParseException | _: SomeOtherException) =>
println("Got it")
}
Run Code Online (Sandbox Code Playgroud) 假设我们有一个包含 3 个子案例类的特征。当我们将此特征与其中两个匹配而忘记检查第三个时,我们可能在编译时没有注意到它,然后我们会收到运行时错误,尤其是在相互递归函数中使用此模式匹配的情况下。
是否有任何语言构造(可能是某种特定的模式匹配)可以确保处理所有子类?如果是这样,更复杂的模式匹配呢?(比仅仅匹配特征的子类更复杂)是否有任何机制可以让我们确保这些模式匹配是完整的功能?
我发现自己写了很多这样的代码:
thing match {
case Case1 ...
case Case2 ...
case _ => throw new IllegalStateException(s"unexpected $thing")
}
Run Code Online (Sandbox Code Playgroud)
有时,当案例不匹配时,我想要一个运行时错误。案例是一种形式的断言。
有没有更好的方法来抑制穷举检查?
我不想使用,[@unchecked](https://www.scala-lang.org/api/2.12.1/scala/unchecked.html)因为这也会禁用可达性检查,这是我想要的。
正如标题所暗示的那样,我不确定方括号和圆括号在列表方面的区别。
Haskellinsert定义的两个版本,一个使用方括号,另一个使用括号:
insert' :: Int -> [Int] -> [Int]
insert' y [] = [y]
insert' y (x:xs) | y <= x = y:x:xs | otherwise = x : insert' y xs
----
insert' y [x:xs] | y <= x = y:x:xs | otherwise = x : insert' y xs
Run Code Online (Sandbox Code Playgroud)
的第二个定义insert'不起作用的原因是什么?
它给出的错误消息,对于任何想知道的人:
test.hs:3:12: error:
• Couldn't match expected type ‘Int’ with actual type ‘[Int]’
• In the pattern: x : xs
In the pattern: [x : xs] …Run Code Online (Sandbox Code Playgroud) 我有一个字符串选项,它也可能是空的。我编写了以下代码来处理不同的流程分支:
input match {
case Some(val) => {
val match {
case "sayHi" => "Hi"
case "sayHello" => "Hello"
case _ => extractFromAnotherInput
}
}
None => extractFromAnotherInput
}
private def extractFromAnotherInput = {
anotherInput match {
case a => ....
case b => ....
}
}
Run Code Online (Sandbox Code Playgroud)
这是在函数式语言中处理代码分支的好方法还是可以以更好的方式完成?
我有一个带有多个 case 类的密封 trait 实现,每个 case 类都有自己的参数,其中一些具有相同类型的参数(我想提取,在这种情况下Trigger:),其中一些没有。
sealed trait Event
object MyTrait {
final case class EventA(context: Context) extends Event
final case class EventB(trigger: Trigger) extends Event
final case class EventC(trigger: Trigger) extends Event
final case class EventD(trigger: Option[Trigger] = None) extends Event
...
final case class EventZ(factor: Factor) extends Event
}
Run Code Online (Sandbox Code Playgroud)
逻辑流程处理如下:
event match {
case ev @(_: Event.EventA | _: Event.EventB |
_: Event.EventC | _: Event.EventD) =>
val amount = for {
count1 <- countThis …Run Code Online (Sandbox Code Playgroud)