我是Scala的新手,刚刚开始学习,所以这是一个基本的初学者问题.
我尝试实现Sierat of Eratosthenes算法.这是我到目前为止所得到的:
def sieve_core(cross: Int, lst: Seq[Int]): List[Int] = {
val crossed = lst.filter(_ % cross != 0)
crossed match {
case a :: rest => cross :: sieve_core(a, crossed)
case _ => cross :: Nil
}
}
def sieve(max: Int): List[Int] = {
sieve_core(2, (2 to max))
}
println(sieve(100))
结果是:
List(2)
据我所知,case _ => cross :: Nil在第一次迭代中匹配sieve_core,这意味着它crossed不是List的实例.
我将lst参数类型更改为List[Int],现在代码将无法编译并显示错误:
(fragment of Problem3.scala):24: error: type mismatch; found …
我有两个选择,我想以这样的方式组合它们,只返回两个选择中唯一的行.Oracle 10g中是否有内置的方法来实现这一目标?
我知道我可以这样做:
(select1 UNION select2) MINUS (select1 INTERSECT select2)
但我想避免它.双方select1并select2有20条线,所以这种方式将是非常晦涩和难以维持.
我正在用Mockito嘲笑一个对象,这个对象的相同方法被多次调用,我想每次都返回相同的值.
这就是我所拥有的:
LogEntry entry = null; // this is a field
// This method is called once only.
when(mockLogger.createNewLogEntry()).thenAnswer(new Answer<LogEntry>() {
@Override
public LogEntry answer(InvocationOnMock invocationOnMock) throws Throwable {
entry = new LogEntry();
return entry;
}
});
// This method can be called multiple times,
// If called after createNewLogEntry() - should return initialized entry.
// If called before createNewLogEntry() - should return null.
when(mockLogger.getLogEntry()).thenAnswer(new Answer<LogEntry>() {
@Override
public LogEntry answer(InvocationOnMock invocationOnMock) throws Throwable {
return entry;
}
});
Run Code Online (Sandbox Code Playgroud)
问题是,似乎我的getLogEntry方法只被调用一次.对于所有后续调用, …
我正在编写类似TotalCommander的应用程序.我有一个单独的文件列表组件,以及它的模型.模型支持侦听器并CurrentDirChanged以下列方式发出诸如此类事件的通知:
private void fireCurrentDirectoryChanged(final IFile dir) {
if (SwingUtilities.isEventDispatchThread())
for (FileTableEventsListener listener : tableListeners)
listener.currentDirectoryChanged(dir);
else {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
for (FileTableEventsListener listener : tableListeners)
listener.currentDirectoryChanged(dir);
}
});
}
}
我为此写了一个简单的测试:
@Test
public void testEvents() throws IOException {
IFile testDir = mockDirectoryStructure();
final FileSystemEventsListener listener =
context.mock(FileSystemEventsListener.class);
context.checking(new Expectations() {{
oneOf(listener).currentDirectoryChanged(with(any(IFile.class)));
}});
FileTableModel model = new FileTableModel(testDir);
model.switchToInnerDirectory(1);
}
这不起作用,因为没有EventDispatchThread.有没有办法在无头构建中进行单元测试?
单元测试java swing jmock
我是斯卡拉新手,刚开始学习这门语言.
我从Project Euler页面解决了问题8.
代码看起来像这样(我删除了读取输入文件所需的所有代码):
def max(n1: Int, n2: Int): Int = Math.max(n1, n2)
def max_product(digits: List[Int], num: Int): Int = {
def max_core(lst: List[Int], curr_max: Int): Int = lst match {
case a if lst.length >= num =>
max_core(a.tail, max(lst.slice(0, num).reduceLeft(_*_), curr_max))
case _ => curr_max
}
max_core(digits, 0)
}
println(max_product(1::2::3::4::2::3::Nil, 2))
它工作正常,结果是正确的.但是,我对这个解决方案并不完全满意.我不喜欢max_core子功能,并且感觉它可以改进.我对FP的理解是,你应该迭代一个列表,切片似乎不是这里的方式.
问题是:如何?
我在Freemarker中创建了函数:
<#function formatDate anyDate>
<#assign dateFormat = read_from_configuration() />
<#if anyDate??>
<#return anyDate?date(dateFormat) />
<#else >
<#return '' />
</#if>
</#function>
我称之为:${formatDate(object.someDate)}.
一切正常,直到someDate为空.在那种情况下,我得到例外:
Error executing macro: formatDate required parameter: anyDate is not specified.
我怎样才能做到这一点?如果参数值为null,我希望函数能够工作.
在我的项目中,我们使用Lucene 2.4.1进行全文搜索.这是一个J2EE项目,IndexSearcher创建一次.在后台,索引每隔几分钟刷新一次(内容更改时).用户可以通过页面上的搜索机制搜索索引.
问题是,Lucene返回的结果似乎是以某种方式缓存的.
这是我注意到的情况:
我分析了我们的配置,并没有在任何地方找到任何缓存.我已经调试了搜索,并且代码中没有缓存,searcher.search返回6个结果.
Lucene是否以某种方式在内部缓存结果?我应该检查哪些属性等?
这是我的freemarker模板的一部分:
${order.needByDate?if_exists?date}
我希望它的工作方式如下:
needByDate为null,则不写入任何内容以上仅适用于第二种情况.实现这一目标的正确方法是什么?
我在遗留应用程序上工作,使用Spring AOP(即ProxyFactoryBean).
我需要在某个类的方法周围添加一个方面.但是这个类不是 bean.AspecjJ切入点表达式如下:
execution(* xyz.package.Class.method())
我创建了一个MethodInterceptor和AspectJExpressionPointcut,但我不知道如何让这两个一起工作.
编辑:
我没有这个类的源代码,它是第三方库.这个类的实例不是由我创建的,既不在源代码中,也不在spring配置中作为bean创建.它由库内部使用.
任何帮助赞赏.
我的用例如下:我想找到所有与此类似的东西/name.action,但最后一部分不是.action例如:
我有这个:
/\w+.\w*
匹配用点分隔的两个单词,但我不知道如何添加'和不匹配.action'.