将流转换为滑动窗口的推荐方法是什么?
例如,在Ruby中你可以使用each_cons:
irb(main):020:0> [1,2,3,4].each_cons(2) { |x| puts x.inspect }
[1, 2]
[2, 3]
[3, 4]
=> nil
irb(main):021:0> [1,2,3,4].each_cons(3) { |x| puts x.inspect }
[1, 2, 3]
[2, 3, 4]
=> nil
Run Code Online (Sandbox Code Playgroud)
在Guava中,我发现只有Iterators#partition,它是相关的但没有滑动窗口:
final Iterator<List<Integer>> partition =
Iterators.partition(IntStream.range(1, 5).iterator(), 3);
partition.forEachRemaining(System.out::println);
-->
[1, 2, 3]
[4]
Run Code Online (Sandbox Code Playgroud) 我无法理解为什么Java HashMap与MapKotlin中的Java 不兼容:
val map : java.util.Map<Int, Int> = java.util.HashMap<Int, Int>()
// ERROR: Type mismatch
Run Code Online (Sandbox Code Playgroud)
这是一个错误还是故意强制在Kotlin中出错?
这是Java to Kotlin编译器的第二个例子.看看这个Java示例文件:
public class Test {
public static void main(String[] args) {
java.util.Map<Integer, Integer> map = new java.util.HashMap<>();
insertValue(map);
}
private static void insertValue(java.util.Map<Integer, Integer> map) {
map.putIfAbsent(0, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
运行"将Java转换为Kotlin"会生成以下文件:
object Test {
@JvmStatic fun main(args: Array<String>) {
val map = java.util.HashMap<Int, Int>()
insertValue(map)
}
private fun insertValue(map: Map<Int, Int>) {
map.putIfAbsent(0, 1) // ERROR! Unresolved reference
} …Run Code Online (Sandbox Code Playgroud) 我正在使用Cassandra,在启动过程中,Netty会打印一个带有堆栈跟踪的警告:
在类路径中找到Netty的本地epoll传输,但epoll不可用.改用NIO."
应用程序正常工作,但有没有办法解决警告?
这是完整的堆栈跟踪:
16:29:46 WARN com.datastax.driver.core.NettyUtil - Found Netty's native epoll transport in the classpath, but epoll is not available. Using NIO instead.
java.lang.UnsatisfiedLinkError: no netty-transport-native-epoll in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:168)
at io.netty.channel.epoll.Native.<clinit>(Native.java:49)
at io.netty.channel.epoll.Epoll.<clinit>(Epoll.java:30)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.datastax.driver.core.NettyUtil.<clinit>(NettyUtil.java:68)
at com.datastax.driver.core.NettyOptions.eventLoopGroup(NettyOptions.java:101)
at com.datastax.driver.core.Connection$Factory.<init>(Connection.java:709)
at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1386)
at com.datastax.driver.core.Cluster.init(Cluster.java:162)
at com.datastax.driver.core.Cluster.connectAsync(Cluster.java:341)
at com.datastax.driver.core.Cluster.connect(Cluster.java:286)
at org.springframework.cassandra.config.CassandraCqlSessionFactoryBean.connect(CassandraCqlSessionFactoryBean.java:100)
at org.springframework.cassandra.config.CassandraCqlSessionFactoryBean.afterPropertiesSet(CassandraCqlSessionFactoryBean.java:94)
at org.springframework.data.cassandra.config.CassandraSessionFactoryBean.afterPropertiesSet(CassandraSessionFactoryBean.java:60)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) …Run Code Online (Sandbox Code Playgroud) 在 Java JVM 中,kill -3强制进程打印所有正在运行的线程的当前堆栈跟踪。我发现快速定位瓶颈非常有效。
V8 中是否有等价物?我可以让 V8 打印当前的堆栈跟踪吗?
澄清:我认为,由于节点的异步性质,它不如典型的非异步程序有用。尽管如此,如果有一种简单的方法可以访问一些堆栈跟踪,那么查看它并不需要太多时间。
根据我的经验,在您需要切换到更高级的工具之前,可以通过这种方式快速定位一些明显的瓶颈。
你会替换吗?
const int one = 1;
const int two = 2;
Run Code Online (Sandbox Code Playgroud)
有了这个?
constexpr int one = 1;
constexpr int two = 2;
Run Code Online (Sandbox Code Playgroud)
我的理解是正确的,两个块在语义上是相同的,并且它目前只是一个品味的问题?
另一方面,正如constexpr所暗示的那样const,你可以争辩说,总是更喜欢更具限制性的形式更为一致,即使是在无关紧要的微不足道的情况下也是如此?
(我理解当右侧的表达式允许更复杂时,情况会完全改变.因此,为了澄清,问题只关注表达式是固定整数的最简单的情况.)
std::unique_ptr很好,但我发现在DDD或gdb中调试时不太舒服.
我正在使用作为gcc一部分的gdb漂亮打印机(例如/usr/share/gcc-4.8.2/python/libstdcxx/v6/printers.py).这是可读性的一大胜利,例如:
$ print pTest
std::unique_ptr<MyType> containing 0x2cef0a0
Run Code Online (Sandbox Code Playgroud)
但是,取消引用指针不起作用:
$ print *pTest
Could not find operator*.
Run Code Online (Sandbox Code Playgroud)
当我需要访问该值时,我必须手动复制指针并将其强制转换为正确的类型,例如:
print *((MyType*) 0x2cef0a0)
Run Code Online (Sandbox Code Playgroud)
如果进程仍在运行,那么这个版本可以工作(仍然很难但更好):
print *pTest.get() // will not work if analyzing a core dump
Run Code Online (Sandbox Code Playgroud)
Display *pTest在DDD中直接的方法也不起作用.它只会导致以下错误:
<error: Could not find operator*.>
Run Code Online (Sandbox Code Playgroud)
有没有办法在DDD中使用unique_ptr调试C++ 11代码(不像我使用繁琐的解决方法那样打破工作流程)?
我不怕使用gdb命令,但DDD集成将是一个加号.例如,通过双击它们来跟踪数据结构中的指针通常比键入更快.
我已经尝试放弃漂亮的打印机,但它也不是最佳选择.我能想到的最好的是以下内容:
print pTest._M_t->_M_head_impl
Run Code Online (Sandbox Code Playgroud) 我花了一些时间在NodeJS测试套件中调试一个奇怪的无限循环问题.它只在罕见的条件下发生,但我可以在附加到chrome调试器时重现它.
我认为它与V8处理异常中的堆栈跟踪以及vows库对AssertionError对象所做的扩展(vows添加了一个toString方法)有关.我也错了,所以我想问一下我对V8实现的理解是否正确.
这是重现错误的最小示例:
$ git clone https://github.com/flatiron/vows.git
$ cd vows && npm install && npm install should
$ cat > example.js
var should = require('should');
var error = require('./lib/assert/error.js');
try {
'x'.should.be.json;
} catch (e) {
console.log(e.toString());
}
// without debug, it should fail as expected
$ node example.js
expected 'x' to have property 'headers' // should.js:61
// now with debug
$ node-inspector &
$ node --debug-brk example.js
// 1) …Run Code Online (Sandbox Code Playgroud) 我最近了解了GADTs他们和他们的符号:
例如
data Maybe a where
Nothing :: Maybe a
Just :: a -> Maybe a
data Either a b where
Left :: a -> Either a b
Right :: b -> Either a b
data Bool where
False :: Bool
True :: Bool
Run Code Online (Sandbox Code Playgroud)
现在,我注意到一个similiarity像功能bool,并且either,这基本上是一样的GADT定义:
Type -> (the letter of step 2)例如
maybe :: b -> (a -> b) -> Maybe a -> b
either :: (a -> …Run Code Online (Sandbox Code Playgroud) 我在Kotlin中表达Java的try-with-resources构造时遇到了一些麻烦.在我的理解中,作为实例的每个表达式都AutoClosable应该提供use扩展函数.
这是一个完整的例子:
import java.io.BufferedReader;
import java.io.FileReader;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
public class Test {
static String foo(String path) throws Throwable {
try (BufferedReader r =
new BufferedReader(new FileReader(path))) {
return "";
}
}
static String bar(TupleQuery query) throws Throwable {
try (TupleQueryResult r = query.evaluate()) {
return "";
}
}
}
Run Code Online (Sandbox Code Playgroud)
Java-to-Kotlin转换器创建此输出:
import java.io.BufferedReader
import java.io.FileReader
import org.openrdf.query.TupleQuery
import org.openrdf.query.TupleQueryResult
object Test {
@Throws(Throwable::class)
internal fun foo(path: String): String {
BufferedReader(FileReader(path)).use { r -> …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个辅助函数来以类型安全的方式比较两种类型:
typesafeEquals("abc", new Integer(42)); // should not compile
Run Code Online (Sandbox Code Playgroud)
我的第一次直接尝试失败了:
<T> boolean typesafeEquals(T x, T y) { // does not work!
return x.equals(y);
}
Run Code Online (Sandbox Code Playgroud)
那么,问题是T可以推断出是一个Object.现在,我想知道typesafeEquals在Java类型系统中是否无法实现.
我知道FindBugs找到的工具可以警告不兼容类型的比较.无论如何,要么看到没有外部工具的解决方案,要么解释为什么它是不可能的,这将是有趣的.
更新:我认为答案是不可能的.但是,我没有证据支持这种说法.只是似乎很难找到适用于所有情况的解决方案.
一些答案很接近,但我相信Java的类型系统不支持在所有普遍性中解决问题.
java ×5
c++ ×2
c++11 ×2
kotlin ×2
node.js ×2
v8 ×2
cassandra ×1
const ×1
constexpr ×1
ddd-debugger ×1
gadt ×1
gdb ×1
generics ×1
haskell ×1
inheritance ×1
java-8 ×1
java-stream ×1
javascript ×1
netty ×1
profiling ×1
stack-trace ×1
type-erasure ×1
type-safety ×1
type-systems ×1
unique-ptr ×1
vows ×1