请耐心等待,因为我对函数式编程和Haskell都很陌生.我试图在Haskell中编写一个函数,它接受一个Integers列表,打印所述列表的头部,然后返回列表的尾部.该函数需要是[Integer] - > [Integer]类型.为了给出一些上下文,我正在编写一个解释器,当在关联列表中查找其各自的命令时调用此函数(键是命令,值是函数).
这是我写的代码:
dot (x:xs) = do print x
return xs
Run Code Online (Sandbox Code Playgroud)
编译器给出以下错误消息:
forth.hs:12:1:
Couldn't match expected type `[a]' against inferred type `IO [a]'
Expected type: ([Char], [a] -> [a])
Inferred type: ([Char], [a] -> IO [a])
In the expression: (".", dot)
Run Code Online (Sandbox Code Playgroud)
我怀疑点函数中的打印调用是导致推断类型为IO [a]的原因.有什么方法可以忽略返回类型的打印,因为我需要返回的是列表的尾部被传递到点.
提前致谢.
我试图解决数独作为家庭作业的约束满足问题.我已经为特定行中的所有元素以及列构建了约束.我正在尝试为子区域中的元素构造约束,但我遇到了一些麻烦.
我当前算法背后的一般思想是将子区域中的所有变量(例如,9x9网格的3x3框)添加到列表中,然后置换该列表中的所有值以在每个变量之间构造NotEqualConstraints .下面的代码适用于NxN网格的第一个子区域,但我不知道如何更改它以迭代整个网格的其余部分.
int incSize = (int)Math.sqrt(svars.length);
ArrayList<Variable> subBox = new ArrayList<Variable>();
for (int ind = 0; ind < incSize; ind++) {
for (int ind2 = 0; ind2 < incSize; ind2++) {
subBox.add(svars[ind][ind2]);
}
}
for (int i = 0; i < subBox.size(); i++) {
for (int j = i + 1; j < subBox.size(); j++) {
NotEqualConstraint row = new NotEqualConstraint(subBox.get(i), subBox.get(j));
constraints.add(row);
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以指导我如何修改代码来打击每个子区域而不仅仅是左上角的方向吗?
编辑:我也愿意尝试任何有效的算法,没有必要为每个子区域的ArrayList添加所有值.如果您看到更好的方式,请分享见解
我最近从jMock 2.5.1升级到2.6.0,似乎它的一些依赖项已经改变,导致我以前通过的一些测试失败.
我的一个测试具有以下期望,用于几个测试的常见设置:
oneOf(service).event(with(any(Long.class)));
在我的测试套件中,event
使用两个null
和有效值调用Long
.这曾经在jMock 2.5.1中完全可以接受,但在升级之后,我得到以下异常:
java.lang.AssertionError: unexpected invocation: service.event(null)
expectations:
expected once, never invoked: service.event(an instance of java.lang.Long)
what happened before this:
locator.locateService()
service.getService()
at org.jmock.api.ExpectationError.unexpected(ExpectationError.java:23)
at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:85)
at org.jmock.Mockery.dispatch(Mockery.java:231)
at org.jmock.Mockery.access$100(Mockery.java:29)
at org.jmock.Mockery$MockObject.invoke(Mockery.java:271)
at org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
at org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38)
at org.jmock.lib.concurrent.Synchroniser.synchroniseInvocation(Synchroniser.java:82)
at org.jmock.lib.concurrent.Synchroniser.access$000(Synchroniser.java:23)
at org.jmock.lib.concurrent.Synchroniser$1.invoke(Synchroniser.java:74)
at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
at com.sun.proxy.$Proxy27.system(Unknown Source)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud)
我怀疑这可能是由于jMock 2.6.0使用的新版Hamcrest,但我不确定.是否有更合适的匹配器可用于为此方法指定null和非null值?
假设我有一个包含元素{1,2,3,4}的ArrayList,我想枚举ArrayList中两个元素的所有可能组合.即(1,2),(1,3),(1,4),(2,3),(2,4),(3,4).这样做最优雅的方式是什么?
我正在开发一个利用OSGi规范的项目,项目的结构是将代码分发到几十个单独的bundle文件夹中.有没有办法在Eclipse中的项目中快速运行所有测试,因为它们位于不同的项目文件夹中?我已尝试设置新的运行配置,但此选项限制您只指定一个源文件夹.
我知道我可以通过Maven轻松运行所有测试,但我更喜欢Eclipse的JUnit插件来运行测试.
假设我有一个带有id的按钮:
<input id='someButton' />
我想在这个按钮上附加一个事件监听器:
$('#form').on('click', '#someButton', function() {
alert("My listener called");
Run Code Online (Sandbox Code Playgroud)
});
但是,在我不知情的情况下,之前有人为这个相同的按钮编写了一个事件监听器:
$('#form').on('click', '#someButton', function() {
alert("Some other listener called");
});
Run Code Online (Sandbox Code Playgroud)
我遇到了一些有效地执行上述操作的代码,看起来第一个注册的侦听器就是使用的那个.我是否正确假设jQuery将始终调用在特定id(并且只有该侦听器)上注册的第一个事件侦听器?
java ×3
junit ×2
arraylist ×1
combinations ×1
constraints ×1
eclipse ×1
haskell ×1
io ×1
javascript ×1
jmock ×1
jquery ×1
monads ×1
osgi ×1
types ×1