gpt4 book ai didi

curry 时的 Groovy 错误

转载 作者:行者123 更新时间:2023-12-03 17:52:53 25 4
gpt4 key购买 nike

给出错误的代码位于 ideone here ,对不起,我无法举一个更简单的例子。

因为有点长,我来解释一下。
Unfoldr类和 unfoldr功能基本上是什么 unfoldr 在 Haskell 中,创建列表。但在这种情况下,我创建了一个迭代器。
map函数又是,很像 map 在 haskell 。它通过了一个迭代器,调用 unfoldr将迭代器作为“状态”并创建一个新的迭代器,它给出与旧迭代器相同的结果,但应用了给定的函数。
splitlist将列表分成列表 n long,即 splitlist(2)([1,2,3,4]) 会产生一个迭代器,它首先给出 [1,2],然后是 [3,4]。
currify接受一个函数并允许部分应用它。例如,

f = currify { a, b -> a + b }

表示 f(2,3) = 5,f(2)(3) = 5,并且 f(2) 是一个加 2 的闭包。我从 this question 得到这个并回答.

Currifying 一个参数闭包实际上应该对它没有任何作用,并且就像它上面的识别函数一样。

最后,我覆盖了 >>迭代器上的运算符,所以我基本上可以像管道一样使用它。

我的问题是为什么行 F:失败,但行 A-E都成功了吗?

代码(很长)在这里,就像我说的,也在 ideone .
@groovy.transform.TypeChecked

class Unfoldr<A,B> implements java.util.Iterator<A>
{
public Unfoldr(Closure<Object[]> f, B init)
{
this.f = f;
this.state = f(init);
}

public synchronized A next() throws java.util.NoSuchElementException
{
if (hasNext())
{
A curr = state[0];
state = f(state[1]);
return curr;
}
else
{
throw new java.util.NoSuchElementException();
}
}

public synchronized boolean hasNext()
{
return state != null;
}

public void remove() { throw UnsupportedOperationException; }

private Closure<Object[]> f;

private Object[] state;
}

def currify(fn) {
{ Object... args ->
if (args.size() == fn.maximumNumberOfParameters) {
fn(*args)
} else {
currify(fn.curry(*args))
}
}
};

def unfoldr = currify { f, init -> new Unfoldr(f, init) };

def map = currify { f, l -> unfoldr({ l2 -> if (l2.hasNext()) { def e = l2.next(); return [f(e), l2]} else { return null; } } , l.iterator())}

def splitlist = currify {
n, l ->
unfoldr(
{
l2 ->
try
{
def a = new Object[n];
for (i in 0..(n-1))
{
a[i] = l2.next();
}
return [a, l2];
}
catch (java.util.NoSuchElementException e)
{
return null;
}
},
l
)
};

Iterator.metaClass.rightShift = { PrintStream os -> delegate.each({ x -> os.println(x) }) }
Iterator.metaClass.rightShift = { Closure f -> f(delegate) }

id = { x -> x }
f = currify { x -> x }

println "A: "
[[1,2],[3,4]].iterator() >> System.out
println "B: "
[1,2,3,4].iterator() >> splitlist(2) >> System.out
println "C: "
[[1,2],[3,4]].iterator() >> map(id) >> System.out
println "D: "
[1,2,3,4].iterator() >> splitlist(2) >> map(id) >> System.out
println "E: "
[[1,2],[3,4]].iterator() >> map(f) >> System.out
println "F: "
[1,2,3,4].iterator() >> splitlist(2) >> map(f) >> System.out

输出:
A: 
[1, 2]
[3, 4]
B:
[1, 2]
[3, 4]
C:
[1, 2]
[3, 4]
D:
[1, 2]
[3, 4]
E:
[1, 2]
[3, 4]
F:
Caught: java.lang.IllegalArgumentException: Can't curry 2 arguments for a closure with 1 parameters.
java.lang.IllegalArgumentException: Can't curry 2 arguments for a closure with 1 parameters.
at prog$_currify_closure8.doCall(prog.groovy:42)
at prog$_run_closure2_closure9.doCall(prog.groovy:49)
at Unfoldr.<init>(prog.groovy:8)
at prog$_run_closure1.doCall(prog.groovy:47)
at prog$_currify_closure8.doCall(prog.groovy:40)
at prog$_run_closure2.doCall(prog.groovy:49)
at prog$_currify_closure8.doCall(prog.groovy:40)
at prog$_run_closure5.doCall(prog.groovy:75)
at prog.run(prog.groovy:91)

最佳答案

看起来 F 行失败的原因

def e = l2.next() 

返回 Object[] 而不是 ArrayList

关于curry 时的 Groovy 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16848874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com