文档中未描述这些方法.我主要使用:=我的.sbt文件,但有时候,由于我还不了解的原因,我指定的:=任务不起作用(意味着任务不会产生副作用而且不会返回任何内容),以及与...合作<<=.那么<<=和之间的区别是:=什么?
编辑:
我的下面的例子运行一个清理任务,当它完成时(doFinally),同时运行另外两个任务.
gae_prep_war := {
val after = Def.task {
(gae_copyJars).value; (compile in Compile).value;
}
(gae_clean, after) {
(clean, task) => clean doFinally task
}
}
Run Code Online (Sandbox Code Playgroud)
因为它现在不起作用,没有错误,但也没有效果或输出; 如果我改变:=到<<=它的工作原理.我的SBT版本是0.13.0并使用Scala 2.10.2.
EDIT2:
我发现:=只要附加.value到外部表达式就可以了:
gae_prep_war := {
val after = Def.task {
(gae_copyJars).value; (compile in Compile).value;
}
(gae_clean, after) {
(clean, task) => clean doFinally task
}
}.value
^
Run Code Online (Sandbox Code Playgroud)
但我还是不太明白为什么......
遵循两个类似的功能,a并以b不同的方式声明:
scala> val a = { x:Int => x+x }
a: Int => Int = <function1>
scala> def b(x:Int) = x+x
b: (x: Int)Int
Run Code Online (Sandbox Code Playgroud)
我可以在两者之间找到的唯一区别是,只有使用该def函数,我可以指定参数的名称,因为我传递它(例如b(x=1)).这些功能在其他方面有何不同?哪个应该用?
编辑:
我也无法@tailrec在val方法中使用注释.
如何从 REPL 运行所有 leiningen 项目的测试?莱宁根就是lein test这样做的。如何从 REPL 做到这一点?
Itertools::group_by是一个迭代器方法,每次键函数更改时都会生成一个新组.提供的示例演示了如何将它与for循环一起使用,但GroupBy在迭代器链中使用输出结构似乎非常麻烦,除非我误解了一些内容:
let data = vec![1, 3, -2, -2, 1, 0, 1, 2];
// example from docs
for (key, group) in &data.into_iter().group_by(|elt| *elt >= 0) {
assert_eq!(4, group.sum::<i32>().abs());
}
// usage in an iterator method chain
data.iter()
.group_by(|elt| **elt >= 0)
.into_iter()
.map(|bool, group| (bool, group.collect::<Vec<i32>>()))
.collect::<Vec<(bool, Vec<i32>)>>();
Run Code Online (Sandbox Code Playgroud)
第二个例子无法编译:
error[E0619]: the type of this value must be known in this context
--> src/main.rs:16:35
|
16 | .map(|bool, group| (bool, group.collect::<Vec<i32>>()))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0599]: no method named …Run Code Online (Sandbox Code Playgroud) 阻止坏,异步好,但在未来阻止仍然阻塞?这是我不断回归的事情; 考虑遵循伪代码:
def queryName(id:Id):Future[String]
def queryEveryonesNames:Future[Seq[String]] = {
val everyonesIds:Future[Seq[Id]] = getIds
val everyonesNames:Future[Seq[Future[String]]] = {
everyonesIds.map(seq.map(id=>queryName(id)))
}
// I'm trying to understand the impact of what I'll do below
everyonesNames.map(seq=>seq.map(fut=>blocking(fut, 1 s)))
}
queryEveryonesNames
Run Code Online (Sandbox Code Playgroud)
在最后一行我接通Future[Seq[Future[String]]](通知将来内将来)进入Future[Seq[String]]由阻塞在内部未来.
阻碍未来的未来感觉多余,至少在这里,但在未来拥有未来也感到多余.
你能提出一种更聪明的方式摆脱内心的未来吗?
你认为阻碍未来的未来是坏事吗?如果是这样的原因和在什么情况下?
Scala 的值类提供了一种无需分配运行时对象(Rust 中的结构)即可使用类型系统的方法。我正在寻找 Rust 中的等价物。
我想介绍的用例是传递一个表示 URI 的字符串,方法是用类似的东西来注释相关签名Uri而不是String,并且理想情况下,Uri在需要 a 时使用它String。所有这一切都以最小的开销。
一个明显的解决方案是使用具有一个字段的结构:
struct Uri { val: String }
Run Code Online (Sandbox Code Playgroud)
这有一个使用有点尴尬的缺点,在String预期的地方不可接受,而且我不确定它的开销。
有没有类似于 Rust 中 Scala 的值类的东西?是否有其他机制可以促进此用例?
如何实现扩展 Promise 的 Deferred 承诺?在需要典型 Promise 的情况下,为类型安全使用扩展 Promise 很重要。
实施后
export class Deferred<T> extends Promise<T> {
public _resolveSelf;
public _rejectSelf;
constructor() {
super(
(resolve, reject) =>
{
this._resolveSelf = resolve
this._rejectSelf = reject
}
)
}
public resolve(val:T) { this._resolveSelf(val) }
public reject(reason:any) { this._rejectSelf(reason) }
}
Run Code Online (Sandbox Code Playgroud)
抛出TypeError: _this is undefined。
在这个 Typescript playground 中,可以看到编译后的 javascript 很有趣。在第 15 行,在声明过程中_this已经分配了它的属性。
我想Future通过使用较少的lambda 来使我的用法更具建设性.目前我正在使用map和lambdas来访问期货的结果.例如:
val rateQuote = future {
connection.getCurrentValue(USD)
}
val purchase = rateQuote map { quote =>
if (isProfitable(quote)) connection.buy(amount, quote)
else throw new Exception("not profitable")
}
purchase onSuccess {
case _ => println("Purchased " + amount + " USD")
}
Run Code Online (Sandbox Code Playgroud)
map我不想为每个提供lambda(匿名函数),而是提供一个命名函数/方法.我该怎么办?例如:
val rateQuote = future {
connection.getCurrentValue(USD)
}
def namedFunction(arg: Arg) =
if (isProfitable(quote)) connection.buy(amount, quote)
else throw new Exception("not profitable")
val purchase = rateQuote map { quote => namedFunction }
Run Code Online (Sandbox Code Playgroud)
甚至更好
val purchase = rateQuote …Run Code Online (Sandbox Code Playgroud) 奇怪的错误:
scala> Parameter.with("published", false)
<console>:1: error: identifier expected but 'with' found.
Parameter.with("published", false)
^
Run Code Online (Sandbox Code Playgroud)
Scala,这个对象来自Java RestFB库,这是Parameter对象的javadoc.想法?
我这样说明了一下:
fn main() {
let mut opt1 = Some(1);
// compiler complains that opt2 doesn't have to be mutable
let mut opt2 = Some(1);
fn take_inner(mut opt: Option<u8>) {
opt.take();
};
opt1.take();
take_inner(opt2);
println!("opt1 {:?}", opt1); // prints "opt1 None"
println!("opt2 {:?}", opt2); // prints "opt2 Some(1)"
}
Run Code Online (Sandbox Code Playgroud)
为什么opt.take()函数内的调用与外部调用(相对于main函数的作用域)有不同的效果?
scala ×5
rust ×3
clojure ×2
future ×2
asynchronous ×1
blocking ×1
concurrency ×1
deferred ×1
lambda ×1
leiningen ×1
mutability ×1
promise ×1
sbt ×1
testing ×1
typescript ×1