すべてのコレクションにメソッドを追加してproduct()
、使用できるように使用したいと思いますsum()
。行くことですでにリストの製品を手に入れることがx.inject { a, b -> a * b }
できますが、行きたいと思っていますx.product()
。
これまで私は試してきました
Collection.metaClass.product = {-> delegate.inject { a, b -> a * b } }
x = [1,2,3,4]
println(x.product())
しかし、これは
Caught: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.inject() is applicable for argument types: (Util$_run_closure1_closure2) values: [Util$_run_closure1_closure2@161bb7fe]
Possible solutions: inject(java.lang.Object, groovy.lang.Closure), inject(java.lang.Object, groovy.lang.Closure), inspect(), toSet(), collect(), collect()
groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.inject() is applicable for argument types: (Util$_run_closure1_closure2) values: [Util$_run_closure1_closure2@161bb7fe]
Possible solutions: inject(java.lang.Object, groovy.lang.Closure), inject(java.lang.Object, groovy.lang.Closure), inspect(), toSet(), collect(), collect()
at Util$_run_closure1.doCall(Util.groovy:1)
at Util.run(Util.groovy:4)