C#5.0异步/等待构造非常棒,但遗憾的是,微软只展示了.NET 4.5和VS 2012的候选版本,这些技术将在我们的项目中得到广泛采用需要一些时间.
在Stephen Toub的异步方法,C#迭代器和任务中,我找到了一个可以在.NET 4.0中很好地使用的替代方法.还有十几个其他实现甚至可以在.NET 2.0中使用该方法,尽管它们看起来很少过时且功能较少.
所以现在我的.NET 4.0代码看起来像(注释部分显示它是如何在.NET 4.5中完成的):
//private async Task ProcessMessageAsync()
private IEnumerable<Task> ProcessMessageAsync()
{
//var udpReceiveResult = await udpClient.ReceiveAsync();
var task = Task<UdpAsyncReceiveResult>
.Factory
.FromAsync(udpClient.BeginReceive, udpClient.EndReceive, null);
yield return task;
var udpReceiveResult = task.Result;
//... blah blah blah
if (message is BootstrapRequest)
{
var typedMessage = ((BootstrapRequest)(message));
// !!! .NET 4.0 has no overload for CancellationTokenSource that
// !!! takes timeout parameter :(
var cts
= new CancellationTokenSource(BootstrapResponseTimeout); // Error here
//... …Run Code Online (Sandbox Code Playgroud) c# timeout pattern-matching task-parallel-library async-await
在我的F#代码中观察到这种行为,我完全感到困惑,这里是从交互式会话中获取的:
Microsoft (R) F# 2.0 Interactive build 4.0.40219.1
Copyright (c) Microsoft Corporation. All Rights Reserved.
For help type #help;;
> type foo = Foo of (string * int);;
type foo = | Foo of (string * int)
> let f = Foo ("bar",42);;
val f : foo = Foo ("bar", 42)
> match f with Foo x -> x;;
val it : string * int = ("bar", 42)
> type bar = Bar of string * int;;
type bar = …Run Code Online (Sandbox Code Playgroud) 有没有办法在模式匹配中使用宏的参数?我想这样做:
def extr(X:AnyRef) = macro extrImpl
def extrImpl(c:Context)(X:c.Expr[AnyRef]):c.Expr[AnyRef] = {
import c.universe._
val tree = reify {
new {
def unapply(x:String):Option[String] = x match {
case X.splice => Some(x) //error
case _ => None
}
}
}.tree
c.Expr(c.typeCheck(tree))
}
Run Code Online (Sandbox Code Playgroud)
但不幸的是编译器说"需要稳定的标识符,但X.splice发现了".通常,人们可以通过首先分配val来解决这个问题,例如:
val XX = X.splice
Run Code Online (Sandbox Code Playgroud)
但当然,这也不适用于拼接.
我是Scala的新手,并一直在尝试使用其出色的组合解析器库.我一直在尝试编译这段代码:
import scala.util.parsing.combinator._
...
val r:Parsers#ParseResult[Node] = parser.parseAll(parser.suite,reader)
r match {
case Success(r, n) => println(r)
case Failure(msg, n) => println(msg)
case Error(msg, n) => println(msg)
}
...
Run Code Online (Sandbox Code Playgroud)
但我不断收到这些错误:
TowelParser.scala:97: error: not found: value Success
case Success(r, n) => println(r)
^
TowelParser.scala:98: error: not found: value Failure
case Failure(msg, n) => println(msg)
TowelParser.scala:99: error: object Error is not a case class constructor, nor does it have an unapply/unapplySeq method
case Error(msg, n) => println(msg)
Run Code Online (Sandbox Code Playgroud)
我尝试了很多不同的东西,比如:
case Parsers#Success(r, n) => println(r) …Run Code Online (Sandbox Code Playgroud) 是否可以使用Scala模式匹配重写以下代码?
val ls: List[String] = ??? // some list of strings
val res = if (ls.contains("foo")) FOO
else if (ls.contains("bar")) BAR
else SOMETHING_ELSE
Run Code Online (Sandbox Code Playgroud) 如何在以下声明中确定头部和尾部:
val head::tail = List(1,2,3,4);
//head: 1 tail: List(2,3,4)
Run Code Online (Sandbox Code Playgroud)
不应该有一些代码将第一个元素提取为head并将尾部作为新List返回.我一直在梳理Scala标准库代码,但我找不到/理解这是怎么做的.
我想有一个匹配任何单个UTF-8字符的函数子句.
我可以匹配这样的特定字符
def foo("a") do
"It's an a"
end
Run Code Online (Sandbox Code Playgroud)
但我无法确定是否可以对任何单个UTF8字符执行相同操作.
我目前的解决方案是将字符串拆分为字符列表并进行模式匹配,但我很好奇是否可以跳过该步骤.
对于我的AI项目,我需要将适用于其部分组件的所有规则应用于因式状态.这需要经常进行,所以我正在寻找一种尽可能快的方法.
我将用字符串描述我的问题,但真正的问题与无符号整数向量的工作方式相同.
我有一堆这样的条目(长度为N),我需要以某种方式存储:
__a_b
c_e__
___de
abcd_
fffff
__a__
Run Code Online (Sandbox Code Playgroud)
我的输入是一个单项ciede,我必须尽快找到所有与之匹配的存储条目.例如,在这种情况下,匹配将是c_e__和___de.应该支持删除和添加条目,但是我不关心它有多慢.我想尽快做到的是:
for ( const auto & entry : matchedEntries(input) )
Run Code Online (Sandbox Code Playgroud)
正如我所说,我的问题是每个字母实际上是无符号整数,并且向量具有未指定(但已知)的长度.我没有要求如何存储条目,或者要将哪种类型的元数据与它们相关联.匹配所有的天真算法是O(N),是否可以做得更好?我需要存储的合理条目数<= 100k.
我在想某种排序可能会有所帮助,或者看起来有些奇怪的树状结构,但我似乎无法找到解决这个问题的好方法.它看起来像处理器已经需要做的事情,所以有人可能会提供帮助.
是否有更可读和更强大(重构)的方式匹配像这样的案例类?
很长的案例类,有很多"字段".
case class Data(name: String, time: Long, ..., userId: Option[UUID] ..., orders: Int, ... ) //more fields fields more
Run Code Online (Sandbox Code Playgroud)
作品.但是当字段位置改变时容易出错.一个人最终算了_.
res match {
case data@Data(_,_,_,_,_,_,Some(id),_,_,_,6,_,_) => (id, data.orders)
case _ => ...
}
Run Code Online (Sandbox Code Playgroud)
也工作.对订单变更稳定.通过更多的检查来获得真正的麻烦.还必须重复阅读该值.
res match {
case data: Data if data.userId.isDefined && data.orders == 6 => (data.userId.get,data.orders)
case _ => ...
}
Run Code Online (Sandbox Code Playgroud)
有没有办法结合变体A和B来获得两种方法的好处?
例如以下功能:
fun fac (0 : int) : int = 1
| fac (n : int) : int = n * fac (n - 1)
Run Code Online (Sandbox Code Playgroud)
或者在功能中:
fun even 0 = true
| even x = odd(x-1)
and odd 0 = false
| odd x = even(x-1);
Run Code Online (Sandbox Code Playgroud)
我对ML的经验很少,我只是想了解基础知识.
pattern-matching ×10
scala ×5
algorithm ×1
async-await ×1
binary ×1
c# ×1
c++ ×1
elixir ×1
f# ×1
list ×1
macros ×1
ml ×1
parsing ×1
scala-2.10 ×1
scala-macros ×1
sml ×1
timeout ×1
types ×1
utf-8 ×1