当我初始化List时,我能够这样做:
List<Object[]> foo = new ArrayList<>();
foo.add(new Object[]{816, "foo", 2.6});
Run Code Online (Sandbox Code Playgroud)
但是,当我想使用Arrays.asList以下方法简化它时:
List<Object[]> bar = Arrays.asList(new Object[]{"bar", 286});
Run Code Online (Sandbox Code Playgroud)
它无法编译错误:
incompatible types: inference variable T has incompatible bounds
equality constraints: java.lang.Object[]
lower bounds: java.lang.Object
Run Code Online (Sandbox Code Playgroud)
为什么它不能做类型推断权以及如何解决这个问题?
我使用setState来更新部分html,但我发现新的更新html没有呈现.这是代码,js小提琴:
HTML:
<div>hello<br />world<br /></div>
<div id='content'></div>
Run Code Online (Sandbox Code Playgroud)
JS:
var Main = React.createClass({
getInitialState: function() {
return {output: 'hello<br />world<br />'};
},
render: function() {
return (
<div>
{this.state.output}
</div>
);
}
});
React.renderComponent(<Main/>, document.getElementById('content'));
Run Code Online (Sandbox Code Playgroud)
我省略了从服务器更新html的部分,但结果是一样的.如何让字符串处于状态?
问题:有没有一种自动进行结构打包的方法?
背景:
结构打包对于减少某些基础数据的内存成本非常有用。基本上,这是通过对内部数据重新排序来实现最小内存成本的技巧。我的问题是有没有一种自动方法可以做到这一点?例如,我这里有一个struct Foo。(假设32位)
struct Foo {
char flag;
char* p;
short number;
};
Run Code Online (Sandbox Code Playgroud)
经过自动检查(无论是否是脚本,无论是否是本机),我应该获得 Foo 的内存优化版本,即:
struct Foo {
char* p;
short number;
char flag;
};
Run Code Online (Sandbox Code Playgroud)
这只是一个玩具示例。考虑下面更困难的情况,手动重新排序将是一项相当艰巨的工作。
结构具有依赖结构:
struct Foo {
char* p;
short number;
MoreFoo more_foo // How to deal with this?
char flag;
};
Run Code Online (Sandbox Code Playgroud)struct 位于遗留代码中,并且您不熟悉代码库。
我不考虑使用“packed”属性,因为它会导致一些性能问题。
在谷歌C++风格指南中,它说:
定义函数时,参数顺序为:输入,然后输出.
谷歌基本上建议功能参数排序如下:
void foo(const Foo& input1, const Foo& input2, Foo* output);
Run Code Online (Sandbox Code Playgroud)
但是,我的同事建议将产出放在第一位.因为这样,foo可以接受默认值,大多数时候输出不会使用默认值.例如:
void foo(Foo* output, const Foo& input1, const Foo& input2 = default);
Run Code Online (Sandbox Code Playgroud)
我觉得他说的很有道理.或者,从可读性,性能等方面来看,我们在这里缺少什么?为什么风格指南建议输出应该是最后一个?
我使用以下代码在 GenericRecord 和字节列表之间进行序列化和反序列化:
public static byte[] encode(List<GenericRecord> records, Schema schema) throws IOException {
SpecificDatumWriter<GenericRecord>
datumWriter =
new SpecificDatumWriter<>(schema);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.reset();
BinaryEncoder binaryEncoder = new EncoderFactory().binaryEncoder(byteArrayOutputStream, null);
for(GenericRecord segment: records) {
datumWriter.write(segment, binaryEncoder);
}
binaryEncoder.flush();
byte[] bytes = byteArrayOutputStream.toByteArray();
return bytes;
}
public static List<GenericRecord> decode(byte[] recordBytes, Schema schema) throws IOException {
DatumReader<GenericRecord>
datumReader =
new SpecificDatumReader<>(schema);
ByteArrayInputStream stream = new ByteArrayInputStream(recordBytes);
stream.reset();
BinaryDecoder binaryDecoder = new DecoderFactory().binaryDecoder(stream, null);
List<GenericRecord> records = new ArrayList<>();
while (true) { …Run Code Online (Sandbox Code Playgroud) 我按照sbt-pack链接导入插件.我做了:
添加一行addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.7.5")到plugins.sbt我的项目的根目录下.
在项目根目录下packAutoSettings的build.sbt文件顶部添加一行
然后我跑sbt启动控制台,它说:
error: not found: value packAutoSettings
另外,当我运行sbt插件时,我没有看到sbt-pack列表中显示的新插件.
我还尝试创建一个名为pack.sbt的文件,并添加如下内容:
pack.settings
addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.7.5")
但它不起作用.
public static void main(String[] args) throws Exception {
new Thread(new Runnable() {
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Hello");
}
}
}).run();
System.out.println("Bye");
}
Run Code Online (Sandbox Code Playgroud)
在主要的thead中,我创建了一个新的线程,它将每秒打印"你好".为什么最后的"再见"从未打印过?换句话说,为什么子线程阻塞主线程?
我定义了一个这样的函数,其中有一个模板模板类
template<typename Key, typename Value, template <typename, typename> class Map>
struct ForEachOf {
void operator()(const Map<Key, Value>& map, std::function<void (Key, Value)> func) {
for(const auto& pair : map) {
func(pair.first, pair.second);
}
}
};
std::map<int, string> m { {1, "foo"}, {3, "bar"}};
ForEachOf<int, string, std::map> forEachOf;
forEachOf(m, [](int key, string value) {
cout << key << value;
});
Run Code Online (Sandbox Code Playgroud)
但是,上面的代码无法编译.错误就像:
error: template template argument has different template parameters
than its corresponding template template parameter
ForEachOf<int, string, std::map> forEachOf;
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__tree:1119:5: note: too …Run Code Online (Sandbox Code Playgroud) c++ templates metaprogramming template-meta-programming c++11
我有如下文本文件格式:
[foo]
foo1=1
foo2=2
[goo]
goo1=1
goo2=2
[bar]
bar1=1
bar2=2
Run Code Online (Sandbox Code Playgroud)
目标是在给定标题名称的情况下提取整个列表.例如,给定标题名称foo,使用正则表达式\[foo\](?:.|\s)+?\[我可以提取foo及其后续列表.给定goo和正则表达式\[goo\](?:.|\s)+?\[我可以extrct goo和它的列表.如regex101中所示
perl实现:
sub getListOfSettingFromKey {
my $key = shift;
my $text = shift;
my $pattern = "\\[$key\\](?:.|\\s)+?\\[";
my $regex = qr/$pattern/;
if ($text =~ $regex) {
return $&;
}
return "";
}
Run Code Online (Sandbox Code Playgroud)
但是,这种方式适用于除最后一部分之外的所有部分,bar在本例中,因为最后没有匹配[.那么如何匹配最后一部分?