我正在为我们创建的 API 编写演示代码,但我一直遇到同样的问题,我一遍又一遍地重复自己的问题,令人作呕。我痛苦地意识到 Java 计划添加闭包,但我现在无法访问它们。这是我想在它自己的小角落里重复的地方:
public BarObj Foo(Double..._input){
try{
//things that vary per function
//but everything else...
} catch(NullException _null){
m_Logger.error("Null error exception caught in Blah::Foo");
return null;
} catch(Exception ex){
m_Logger.error( ex.getMessage() );
return null;
}
}
关于我想解决这个问题的唯一方法是将 Method 传递给一个带有 try-catch 逻辑的函数,并将其全部包装在另一个函数中,如下所示:
public BarObj MyFunc(Double..._input){
return compose("MyLogic",_input);
}
private BarObj MyLogic(Double..._input)
throws Exception{
//stuff
}
但它看起来很丑并且带有很多样板。在 Java 中有更简单的方法来组合函数吗?
最佳答案
在 Java 中这是非常困难的,因为没有对函数的一流支持(不像 clojure 或 scala,可能还有其他)。
但是,您可以封装对象中的操作:
interface Function<R, T> {
R call(T... input);
}
然后将 Foo 重构为:
static <R, T> R runFunction(Function<R, T> function, T ... input){
try{
return function.call(input);
} catch(NullPointerException _null){
m_Logger.error("Null error exception caught in Blah::Foo");
return null;
} catch(Exception ex){
m_Logger.error( ex.getMessage() );
return null;
}
}
测试用例:
class SumDoubles implements Function<Double, Double> {
@Override
public Double call(Double... input) {
Double sum = 0.0;
for (Double d : input) {
sum += d;
}
return sum;
}
}
@Test
public void sum() {
Double sum = runFunction(new SumDoubles(), 1.0, 2.0, 3.0);
assertThat(sum, is(6.0));
}
关于java - 用 Java 编写函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2207877/
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin
我想在Ruby中创建一个用于开发目的的极其简单的Web服务器(不,不想使用现成的解决方案)。代码如下:#!/usr/bin/rubyrequire'socket'server=TCPServer.new('127.0.0.1',8080)whileconnection=server.acceptheaders=[]length=0whileline=connection.getsheaders想法是从命令行运行这个脚本,提供另一个脚本,它将在其标准输入上获取请求,并在其标准输出上返回完整的响应。到目前为止一切顺利,但事实证明这真的很脆弱,因为它在第二个请求上中断并出现错误:/usr/b
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只