在下面的示例中,l1和l2是整数列表,我想知道为什么会产生错误?
l1.zip(l2).map(_ => { if(_._1 > _._2) 1 else 2 } )
Run Code Online (Sandbox Code Playgroud)
它抱怨它错过了扩展功能的参数类型.为什么这里不允许使用下划线?
我很擅长使用Akka演员.最近,我读到了分布式演员或远程演员(无论你怎么称呼它).到目前为止,我一直很舒服地使用我的Actor中的状态并在我的接收方法中改变它.快乐的时光!现在我想把我的演员分配到几个JVM上,我已经看到我演员中的状态可能是一个问题.
我确实读过关于成为和不成功的内容,我只想知道Akka如何在内部处理这个问题?
有状态:
class TestActor extends Actor {
var state = List.empty[String])
def receive =
case Add(elem) => state + elem
case Contains(elem) => sender() ! state.contains(elem)
}
}
Run Code Online (Sandbox Code Playgroud)
删除状态:
class TestActor extends Actor {
def receive = start(List.empty[String])
def start(lst: List[String]): Receive = {
case Add(elem) =>
context become start(lst+ elem)
case Contains(elem) =>
sender() ! lst.contains(elem)
}
}
Run Code Online (Sandbox Code Playgroud)
国家在哪里进入第二个版本的例子?
我有一个有可变状态的演员.在我的receive方法中,我模式匹配消息并调用一些将返回Future的服务.这个Future将修改我的Actor实例中的状态.这个状态不是线程安全的吗?由于Future将在不同的线程中执行,我的Actor中的状态是否保证是线程安全的?
我有一个特定类型的列表,我想根据条件减少它。我有一种类型,其中 Interval 是具有开始和结束的日期时间间隔:
case class MyType(a: Interval, value: Double)
Run Code Online (Sandbox Code Playgroud)
我有一个 List[MyType] 条目,我想将其缩减为基于包含相同日期时间和值的 MyType 的 List[MyType] 。我不想再重复一遍这份清单,虽然我已经这么做了。
假设我有:
val a = MyType(interval1, 2)
val b = MyType(interval2, 2)
val c = MyType(interval3, 1)
val d = MyType(interval4, 6)
val e = MyType(interval5, 2)
val original = List(a, b, c, d, e)
Run Code Online (Sandbox Code Playgroud)
我现在必须根据以下条件减少原始列表:
1. interval should be continuous, then take the start of the first entry and the end of the second entry
2. the double value should be the same
Run Code Online (Sandbox Code Playgroud)
因此假设interval1、interval2是连续的,结果应该是这样的: …
我有一个形状如下的类型:
val myType: Future[Either[MyError, TypeA]] = // some value
Run Code Online (Sandbox Code Playgroud)
我知道我可以在此模式匹配并转到Right或Left类型,但问题是我必须嵌套我的模式匹配逻辑.我正在寻找更优雅的处理方式吗?有什么建议?
假设我有以下一组代码在未来做了一些事情:
1 to 10 foreach {
case x => Future { x + x }
}
Run Code Online (Sandbox Code Playgroud)
假设我给这段代码提供了默认的ExecutionContext,我知道后台会发生什么,但我想知道的是如何实现Future的处理呢?我的意思是应该有一些线程或一组线程可能等待Future完成?这些线程被阻止了吗?在他们真正等待未来完成的意义上被阻止了?
现在在以下场景中:
val x: Future[MyType] = finishInSomeFuture()
Run Code Online (Sandbox Code Playgroud)
假设x有一个超时,我可以像这样调用:
Future {
blocking {
x.get(3, TimeOut.SECONDS)
}
}
Run Code Online (Sandbox Code Playgroud)
我真的在阻止吗?是否有更好的异步超时方法?
编辑:以下Timeout比我上面定义的阻塞上下文有多么不同或有多好?
object TimeoutFuture {
def apply[A](timeout: FiniteDuration)(block: => A): Future[A] = {
val prom = promise[A]
// timeout logic
Akka.system.scheduler.scheduleOnce(timeout) {
prom tryFailure new java.util.concurrent.TimeoutException
}
// business logic
Future {
prom success block
}
prom.future
}
}
Run Code Online (Sandbox Code Playgroud) 假设我有一个Monoid特征,如下所示:
trait Monoid[A] {
def combine(a1: A, a2: A): A
def identity: A
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我想为此编写一个optionMonoid,我可以像这样写:
val optionMonoid1 = new Monoid[Option[A]] {
def combine(a1: Option[A], a2: Option[A2]) a1 orElse a2
def identity = None
}
Run Code Online (Sandbox Code Playgroud)
这是因为我对Option中的内部类型一无所知.但是,如果我想以这样的方式使用combine运算符,我想真正组合Option中的内部类型呢?
我正在尝试使用Iron框架的Hello World应用程序.这就是我的主要内容:
extern crate iron;
extern crate router;
use iron::prelude::*;
use iron::status;
use router::Router;
fn main() {
let mut router = Router::new();
router.get("/", hello_world);
router.post("/data", randomfriend);
fn hello_world(_: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, "Hello World!")))
}
fn data(_: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Ok, "Got some data")))
}
Iron::new(router).http("localhost:3000").unwrap();
println!("On 3000");
}
Run Code Online (Sandbox Code Playgroud)
这是我的Cargo.toml:
[package]
name = "webserver-iron"
version = "0.1.0"
[[bin]]
name = "webapp_demo_server"
[dependencies]
iron = "*"
router = "*"
Run Code Online (Sandbox Code Playgroud)
当我运行时cargo run,我收到以下错误:
error: struct field shorthands …Run Code Online (Sandbox Code Playgroud) 我一直在阅读Rust和Go并且我看到这些语言如何处理悬空指针及其引起的问题的细微差别.例如,这是Rust中的一个版本:
fn main() {
let reference_to_nothing = dangle();
}
fn dangle() -> &String {
let s = String::from("hello");
&s
}
Run Code Online (Sandbox Code Playgroud)
上面会错误地说,在函数中dangle,s超出范围,我不能返回它的引用!但在Go中,这似乎有点允许吗?
Go中如何处理这样的事情?在Go中创建悬空指针是否容易?如果是这样,我有什么措施来控制它们?
我有这样的特征定义:
trait MyTrait {
def dbService[M[_]]: DBService[M[_]]
def appConfig: AppConfig
def supervisorActor: ActorRef
}
Run Code Online (Sandbox Code Playgroud)
我有这个特征的实现,如下所示:
object MyTrait {
def apply(system: ActorSystem, actorMaterializer: Materializer): MyTrait = new MyTrait {
override val appConfig: AppConfig = AppConfig.load()
// Get the ERROR here saying Value dbService overrides nothing
override val dbService: DBService[Task] = DBServiceT.asTask(appConfig.dbConfig)(scala.concurrent.ExecutionContext.Implicits.global)
override val supervisorActor: ActorRef =
system.actorOf(
SupervisorActor.props(appConfig)(monix.execution.Scheduler.Implicits.global),
s"${appConfig.appName}-supervisor"
)
}
}
Run Code Online (Sandbox Code Playgroud)
我的DBService特性如下所示:
trait DBService[M[_]] {
def allPowerPlants(fetchOnlyActive: Boolean): M[Seq[PowerPlantRow]]
def powerPlantsPaginated(filter: PowerPlantFilter): M[Seq[PowerPlantRow]]
def allPowerPlantsPaginated(fetchOnlyActive: Boolean, pageNumber: Int): M[Seq[PowerPlantRow]]
def …Run Code Online (Sandbox Code Playgroud)