Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm looking at various aspects of Java SE 8. I've encountered a number of situations where compilable code leads to runtime exceptions or apparent inconsistencies. Here is one, where a construct operates as expected in one context, but appears to fail in another. Is it a bug or am I missing something?
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import java.util.stream.DoubleStream;
import static java.lang.System.*;
public class Stream06 {
public static void main(String[] args) {
out.println(IntStream.rangeClosed(2,5)
.reduce((i,j)->i+j).getAsInt()); // OK
out.println(IntStream.rangeClosed(2,5)
.reduce(0,(i,j)->i+j)); // OK
out.println(IntStream.rangeClosed(2,5)
.reduce((i,j)->i*j).getAsInt()); // OK!
out.println(IntStream.rangeClosed(2,5)
.reduce(0,(i,j)->i*j)); // zero!!!
out.println();
out.println(LongStream.rangeClosed(2,5)
.reduce((l,m)->l+m).getAsLong()); // OK
out.println(LongStream.rangeClosed(2,5)
.reduce(0,(l,m)->l+m)); // OK
out.println(LongStream.rangeClosed(2,5)
.reduce((l,m)->l*m).getAsLong()); // OK!
out.println(LongStream.rangeClosed(2,5)
.reduce(0,(l,m)->l*m)); // zero!!!
out.println();
out.println(DoubleStream.of(2.5, 1.3, 6.8)
.reduce((d,e)->d+e).getAsDouble()); // OK
out.println(DoubleStream.of(2.5, 1.3, 6.8)
.reduce(0,(d,e)->d+e)); // OK
out.println(DoubleStream.of(2.5, 1.3, 6.8)
.reduce((d,e)->d*e).getAsDouble()); // OK (usual rounding issue)
out.println(DoubleStream.of(2.5, 1.3, 6.8)
.reduce(0,(d,e)->d*e)); // zero!!!
}
}
reduce i*j starting from zero leads to zero... well, that's quite expected :-)
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I don't know why whenever I run this code in CMD ,I got this error:
Syntax error, unexpected =>
Here's my code:
hash_brown = (
"topping_1" => "Sour Cream",
"topping_2" => "Butter",
"topping_3" => "Salt",
"topping_4" => "Ketchup"
)
puts hash_brown["topping_2"]
first_hash = Hash.new
first_hash["first_name"] = "Jacob"
first_hash["nick_name"] = "Day"
first_hash["last_name"] = "Williams"
puts first_hash["first_hash"]
$end
Please let me know what is wrong with it cause I have checked it thousands of times & didn't found anything!
You should use curly braces ({}) to define Hash objects:
hash_brown = {
"topping_1" => "Sour Cream",
"topping_2" => "Butter",
"topping_3" => "Salt",
"topping_4" => "Ketchup"
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I faced with strange problem with sorting Files.
Given env
Dev: Mac OS X 10.11.3 OracleJDK 1.8.0_45
PreProduction env: FreeBSD 10 OpenJDK 1.8.0_72
Code
public static String getLatestTag() {
File tagsDir = new File("./.git/refs/tags");
...
File[] tags = tagsDir.listFiles();
List<File> tagsList = Arrays.asList(tags);
Collections.sort(tagsList, (f1, f2) -> {
if(f1.lastModified() > f1.lastModified()) {
return 1;
} else if(f1.lastModified() == f2.lastModified()) {
return 0;
} else {
return -1;
}
});
logTagsList(tagsList);
String latestTag = tagsList.get(0).getName();
Logger.info("Application version is: %s", latestTag.replaceAll("[^\\d.]", ""));
return latestTag;
}
private static void logTagsList(List<File> tags) {
if(Logger.isDebugEnabled()) {
Logger.debug("Tags found");
for(File tag : tags) {
Logger.debug("Tag: %s, Date modified: %s", tag.getName(), tag.lastModified());
}
}
}
Gives an output
At Mac:
17:49:50,601 DEBUG ~ Tags found
17:49:50,602 DEBUG ~ Tag: v0.97, Date modified: 1457277455000
17:49:50,602 DEBUG ~ Tag: v0.95, Date modified: 1455809758000
17:49:50,602 INFO ~ Application version is: 0.97
At FreeBSD:
18:52:49,902 DEBUG ~ Tags found
18:52:49,903 DEBUG ~ Tag: v0.95, Date modified: 1456038720000
18:52:49,903 DEBUG ~ Tag: v0.97, Date modified: 1457277515000
18:52:49,904 INFO ~ Application version is: 0.95
In both cases user who is running an application has read access to .git directory.
Steps to reproduce:
1) git init
2) bootstrap java application (or play framework 1.4 application for complete reproduce)
3) add given code
4) make 2 commits to git
5) label those commits
6) run application
7) examine logs
You got an error in your comparison: if(f1.lastModified() > f1.lastModified()) - you are comparing f1 with f1 here. And if both files are not modified at the same time, you are always returning -1, no matter what you compare. And this leads to unpredictable behaviour.
I used JDOM1 before to parse xmls with xpath, and tired with the non-generic style, so I decide to try JDOM2, OK, everything works perfect for me ( the generic, XPathFactory, XPathExpression). then I try a xpath statement with contains function :
XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
"./p[contains(.,'time:')]/text()", Filters.textOnly());
String time = timeXpath.evaluateFirst(div).getTextTrim();
then I got exeptions:
java.lang.IllegalStateException: Unable to evaluate expression. See cause
at org.jdom2.xpath.jaxen.JaxenCompiled.evaluateRawFirst(JaxenCompiled.java:200)
at org.jdom2.xpath.util.AbstractXPathCompiled.evaluateFirst(AbstractXPathCompiled.java:327)
at peace.org.tm.spider.star.DamaiStarSpider.syncStarTracks(DamaiStarSpider.java:123)
at peace.org.tm.spider.star.DamaiStarSpider.main(DamaiStarSpider.java:156)
Caused by: org.jaxen.UnresolvableException: Function :contains
at org.jaxen.SimpleFunctionContext.getFunction(SimpleFunctionContext.java:142)
at org.jaxen.ContextSupport.getFunction(ContextSupport.java:189)
at org.jaxen.Context.getFunction(Context.java:153)
at org.jaxen.expr.DefaultFunctionCallExpr.evaluate(DefaultFunctionCallExpr.java:183)
at org.jaxen.expr.DefaultPredicate.evaluate(DefaultPredicate.java:106)
at org.jaxen.expr.PredicateSet.evaluatePredicates(PredicateSet.java:188)
at org.jaxen.expr.DefaultLocationPath.evaluate(DefaultLocationPath.java:218)
then I tried:
XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
"./p[fn:contains(.,'time:')]/text()", Filters.textOnly());
String time = timeXpath.evaluateFirst(div).getTextTrim();
xpath compile failed:
java.lang.IllegalArgumentException: Unable to compile './p[fn:contains(.,'time:')]/text()'. See Cause.
at org.jdom2.xpath.jaxen.JaxenCompiled.<init>(JaxenCompiled.java:152)
at org.jdom2.xpath.jaxen.JaxenXPathFactory.compile(JaxenXPathFactory.java:82)
at org.jdom2.xpath.XPathFactory.compile(XPathFactory.java:282)
at peace.org.tm.spider.star.DamaiStarSpider.syncStarTracks(DamaiStarSpider.java:91)
at peace.org.tm.spider.star.DamaiStarSpider.main(DamaiStarSpider.java:156)
Caused by: org.jaxen.XPathSyntaxException: Unexpected '('
at org.jaxen.BaseXPath.<init>(BaseXPath.java:136)
at org.jaxen.BaseXPath.<init>(BaseXPath.java:157)
at org.jdom2.xpath.jaxen.JaxenCompiled.<init>(JaxenCompiled.java:150)
... 4 more
I already google the stack trace for about 2 hours, nothing useful founded, I think maybe I made a very stupid mistake, is anyone can figure it out for me? thanks!
I can't reproduce the exceptions you are getting.... I am using JDOM 2.0.5 with Jaxen 1.1.6.
I have created the following:
public static void main(String[] args) {
Element root = new Element ("root");
Element p = new Element("p");
p.addContent(" Return this time: Boo! ");
root.addContent(p);
XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
"./p[contains(.,'time:')]/text()", Filters.textOnly());
XPathDiagnostic<Text> xpd = timeXpath.diagnose(root, true);
System.out.println(xpd);
System.out.println(timeXpath.evaluateFirst(root).getTextTrim());
}
and it produces:
[XPathDiagnostic: './p[contains(.,'time:')]/text()' evaluated (first) against org.jdom2.Element produced raw=1 discarded=0 returned=1]
Return this time: Boo!
I believe you may have out-of-date Jaxen class libraries?
This has completely baffled me on a number of configurations. I keep reading the documentation, and I just don't get it. Here is my registration code:
ForRequestedType<SimpleWorkItemProcessor>().TheDefault.Is.OfConcreteType<SimpleWorkItemProcessor>();
ForRequestedType<WorkItemRetryProcessor>().TheDefault.Is.OfConcreteType<WorkItemRetryProcessor>()
.CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<SimpleWorkItemProcessor>())
.WithCtorArg("busyDelay").EqualTo(TimeSpan.FromMilliseconds(20))
.WithCtorArg("overallTimeout").EqualTo(TimeSpan.FromSeconds(60));
ForRequestedType<WorkItemQueue>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
.CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<WorkItemRetryProcessor>());
As it is, it says there's no default instance for IWorkItemProcessor (which is correct). Switching the last line to this:
ForRequestedType<IWorkItemProcessor>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
.CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<WorkItemRetryProcessor>());
...Makes a stack overflow exception.
How do you chain classes together that both implement an interface, and take in that same interface in their constructor?
This works, but I can't explain why. From what I know, the first version should work just as well.
ForRequestedType<SimpleWorkItemProcessor>().TheDefault.Is.OfConcreteType<SimpleWorkItemProcessor>();
var retryProcessor = ForRequestedType<WorkItemRetryProcessor>().TheDefault.Is.OfConcreteType<WorkItemRetryProcessor>()
.CtorDependency<IWorkItemProcessor>().Is(x => x.OfConcreteType<SimpleWorkItemProcessor>())
.CtorDependency<TimeSpan>("busyDelay").Is(x => x.Object(TimeSpan.FromMilliseconds(20)))
.CtorDependency<TimeSpan>("overallTimeout").Is(x => x.Object(TimeSpan.FromSeconds(60)));
ForRequestedType<IWorkItemProcessor>().TheDefault.Is.OfConcreteType<WorkItemQueue>()
.CtorDependency<IWorkItemProcessor>("workItemProcessor").Is(x => x.Instance(retryProcessor));
I am trying to call cudppSort to sort a set of keys/values. I'm using the following code to set up the sort algorithm:
CUDPPConfiguration config;
config.op = CUDPP_ADD;
config.datatype = CUDPP_UINT;
config.algorithm = CUDPP_SORT_RADIX;
config.options = CUDPP_OPTION_KEY_VALUE_PAIRS | CUDPP_OPTION_FORWARD | CUDPP_OPTION_EXCLUSIVE;
CUDPPHandle planHandle;
CUDPPResult result = cudppPlan(&planHandle, config, number_points, 1, 0);
if (CUDPP_SUCCESS != result) {
printf("ERROR creating CUDPPPlan\n");
exit(-1);
}
The program exits, however on the line:
CUDPPResult result = cudppPlan(&planHandle, config, number_points, 1, 0);
and prints to stdout:
Cuda error: allocScanStorage in file 'c:/the/path/to/release1.1/cudpp/src/app/scan_app.cu' in line 279 : invalid configuration argument.
I looked at the line in scan_app.cu. It is,
CUT_CHECK_ERROR("allocScanStorage");
So apparently my configuration has an error that is causing the allocScanStorage to bomb out. There are only two calls to CUDA_SAFE_CALL in the function and I don't see a reason why either has anything to do with the configuration.
What is wrong with my configuration?
So that this doesn't sit around as an unanswered question (I'm not sure if this is the right SO etiquette but it seems like an answered question shouldn't sit around unanswered...), I'm copying the comment I made above here as an answer since it was the solution:
I figured this out (I'm still learning CUDA at the moment.) Because the error checking is asynchronous errors can show up in strange places if you don't check for them from time to time. My code had caused an error before I called cudppPlan but because I didn't check for errors the cudppPlan reported the error as if it was in cudppPlan.