我正在制作我自己的Java版Stream库以获得乐趣.这是我的班级签名:
class Stream<T> {
Supplier<T> head;
Supplier<Stream<T>> tail;
...
}
Run Code Online (Sandbox Code Playgroud)
另外,我编写了一个基本的无限流迭代器,它将基于给定的函数生成无限列表:
public static <T> Stream<T> iterate(T first, Function<T, T> f) {
return new Stream<T>(
() -> first,
() -> {
T nextElem = f.apply(first);
if (nextElem == null) {
return generate(() -> null);
} else {
return iterate(nextElem, f);
}
}
);
}
Run Code Online (Sandbox Code Playgroud)
该函数generate是iterate的一个特例,它永远重复给定的元素.在上面的函数中,我正在生成一个无限序列null来指示流的结束(我不认为我将在流中存储空值).
然后我写了一个reduce函数,其中reduce函数在第二个参数上是惰性的:
public <U> U reduce(U acc, Function<T, Function<Supplier<U>, U>> f) {
System.out.println("REDUCE CALL");
T elem = head.get();
if (elem != null) { …Run Code Online (Sandbox Code Playgroud) 我有以下程序导致分段错误.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
printf("TEST");
for (int k=0; k<(strlen(argv[1])); k++)
{
if (!isalpha(argv[1])) {
printf("Enter only alphabets!");
return 1;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我已经发现正是这条线导致了这个问题
if (!isalpha(argv[1])) {
Run Code Online (Sandbox Code Playgroud)
并替换argv[1]为argv[1][k]解决问题.
但是,我觉得很奇怪程序会导致分段错误而不打印TEST.我还希望isalpha函数错误地检查char*指针的低字节是否argv[1],但似乎不是这种情况.我有代码来检查参数的数量,但为简洁起见,此处未显示.
这里发生了什么事?
我正在学习如何在c ++中实现mergesort并遇到以下问题.
这是我的合并函数,它将两个已排序的数组合并为一个已排序的数组.
void merge(int *list, int *final, int start, int mid, int stop) {
int h = start;
int i = start;
int j = mid + 1;
while ((h <= mid) && (j <= stop)) {
if (list[h] <= list[j]) {
final[i] = list[h];
h++;
} else {
final[i] = list[j];
j++;
}
i++;
}
/* CODE A */
if (h > mid) {
for (int k = j; k <= stop; k++) {
final[i] = list[k];
i++; …Run Code Online (Sandbox Code Playgroud) 这是我的问题.给定一个整数数组和另一个整数k,找到数组中每个元素的差异总和k.
例如,如果阵列是2, 4, 6, 8, 10与k是3
Sum of difference
= abs(2 - 3) + abs(4-3) + abs(6 - 3) + abs(8 - 3) + abs(10 - 3)
= 1 + 1 + 3 + 5 + 7
= 17
Run Code Online (Sandbox Code Playgroud)
该数组始终保持相同,最多可包含100000个元素,并且将测试100个不同的k值.k可以是也可以不是数组的元素.这必须在1s或大约100M的操作中完成.我该如何实现这一目标?
我是Phoenix/Elixir的初学者,我正在尝试编写API以允许用户在我的应用程序中注册.
除非我尝试设置响应的HTTP状态代码,否则API端点将按预期工作.当我包括A,B和C行(在下面的代码中指出)时,我得到了一条FunctionClauseError消息no function clause matching in :cowboy_req.status/1.
完整的错误消息如下:
[error] #PID<0.344.0> running App.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /api/user/
** (exit) an exception was raised:
** (FunctionClauseError) no function clause matching in :cowboy_req.status/1
(cowboy) src/cowboy_req.erl:1272: :cowboy_req.status(451)
(cowboy) src/cowboy_req.erl:1202: :cowboy_req.response/6
(cowboy) src/cowboy_req.erl:933: :cowboy_req.reply_no_compress/8
(cowboy) src/cowboy_req.erl:888: :cowboy_req.reply/4
(plug) lib/plug/adapters/cowboy/conn.ex:34: Plug.Adapters.Cowboy.Conn.send_resp/4
(plug) lib/plug/conn.ex:356: Plug.Conn.send_resp/1
(app) web/controllers/user_controller.ex:1: App.UserController.action/2
(app) web/controllers/user_controller.ex:1: App.UserController.phoenix_controller_app/2
(app) lib/app/endpoint.ex:1: App.Endpoint.instrument/4
(app) lib/phoenix/router.ex:261: App.Router.dispatch/2
(app) web/router.ex:1: App.Router.do_call/2
(app) lib/app/endpoint.ex:1: App.Endpoint.phoenix_app/1
(app) lib/plug/debugger.ex:122: App.Endpoint."call (overridable 3)"/2
(app) lib/app/endpoint.ex:1: …Run Code Online (Sandbox Code Playgroud) 我正在设计一个带有背景图像的网页。中间有一个白框(使用 div),白框内是一些文本。我想要做的是使文本颜色与正文的背景颜色相同。如果我将文本的 color 属性设置为 transparent ,它就会消失,因为有一个白色的 div。我该怎么做呢?
这是 JSFiddle 的链接:http : //jsfiddle.net/FpJpV/
html是
<div>
<h1>Title</h1>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS是
body {
background-color: red;
font-family: sans-serif;
}
div {
width: 200px;
background-color: white;
margin: auto;
text-align: center;
padding: 1em;
}
Run Code Online (Sandbox Code Playgroud)
注意:在 JSFiddle 中,为了简单起见,我使用了红色背景,但在真实网页中,主体的背景是图像。
我目前正在学习R,我遇到了制表数据的问题.
我在数据框中有整数分数model,范围从1到10(含).当我使用表函数时,即
table(model$score)
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
1 2 3 4 5 6 7 8 9 10
5 6 8 7 2 3 6 4 5 0
Run Code Online (Sandbox Code Playgroud)
但是,我想按以下格式制表数据:
1-2 3-4 5-6 7-8 9-10
11 15 5 10 5
Run Code Online (Sandbox Code Playgroud)
是否可以通过表函数实现此功能,还是必须寻求其他功能/包的帮助?那我该怎么办?这个prop.table功能有类似的方法吗?
谢谢您的帮助.