如何在 Scala 中模拟以下行为?即在满足蓄能器的某些特定条件时继续折叠。
def foldLeftWhile[B](z: B, p: B => Boolean)(op: (B, A) => B): B
Run Code Online (Sandbox Code Playgroud)
例如
scala> val seq = Seq(1, 2, 3, 4)
seq: Seq[Int] = List(1, 2, 3, 4)
scala> seq.foldLeftWhile(0, _ < 3) { (acc, e) => acc + e }
res0: Int = 1
scala> seq.foldLeftWhile(0, _ < 7) { (acc, e) => acc + e }
res1: Int = 6
Run Code Online (Sandbox Code Playgroud)
更新:
根据@Dima 的回答,我意识到我的意图有点副作用。所以我让它与 同步takeWhile,即如果谓词不匹配,则不会有任何进展。并添加更多示例以使其更清晰。(注意:这不适用于Iterators)
I have some docker containers provisioned by docker-compose with restart: always flag. But I accidentally delete docker-compose.yml file.
How do I delete those containers so that they will not restart automatically?
给定val as: Seq[Int] = ...
很多时候我需要对两个连续的元素应用操作,例如
顺便说一句我不喜欢
for (i <- 1 until as.size) {
// do something with as(i) and as(i - 1)
}
Run Code Online (Sandbox Code Playgroud)
或者通过另一个
as.tail.foldLeft((0, as.head)) { (acc, e) =>
// do something with acc._2 and e
// and try to not forget returning (_, e)
}
Run Code Online (Sandbox Code Playgroud)
如何针对这种情况编写更好的代码?
我在 GCP 中有一个启用了 RBAC 的 Kubernetes 集群
有一个命名空间的分蘖和倍数服务
现在,我可以为特定服务帐户分配读者角色,因为它的全名
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
annotations:
rbac.authorization.kubernetes.io/autoupdate: "true"
name: tiller-reader
namespace: tiller
rules:
- apiGroups: [""]
resources: ["pods"]
verbs:
- "get"
- "watch"
- "list"
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: tiller-reader-role-binding
namespace: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: tiller-reader
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: User
name: system:serviceaccount:my-namespace-1:my-namespace-1-service-account
Run Code Online (Sandbox Code Playgroud)
该服务的命名空间和帐户动态创建。如何自动授予所有服务帐户访问Tiller 命名空间的权限,例如:获取 Pod?
为什么以下基本安全配置不适用于 inMemoryAuthentication() 子句?
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf().disable()
.httpBasic()
.and()
.authorizeRequests()
.anyRequest().authenticated();
super.configure(http);
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("username").password("password");
super.configure(auth);
}
}
Run Code Online (Sandbox Code Playgroud)
应用初始化之后,仍然只有userSpring自己生成的default ,没有像username.
我有以下配置
@Configuration
@EnableWebFlux
public class WebfluxConfig {
@Bean
RouterFunction<?> routerFunction(UserResource userResource) {
return route(GET("/user"), r -> ok()
.body(userResource.findAll(), UserDto.class));
}
}
Run Code Online (Sandbox Code Playgroud)
当我启动应用程序时,它会进行预期的映射
2018-04-12 12:12:06.682 INFO 54617 --- [主要] oswrfssRouterFunctionMapping:映射/用户-> cnaicWebfluxConfig$$Lambda$764/909878836@14ba7f15
然而,当我尝试连接到它时,我得到了 404。并且路由器谓词没有被调用一次。
curl -v localhost:8080/user
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /user HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 404
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 12 Apr 2018 09:15:52 GMT …Run Code Online (Sandbox Code Playgroud)