Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!

Mike Simmons

Ranch Foreman
+ Follow
since Mar 05, 2008
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mike Simmons

Chaucer died in 1400, after which his writing output decreased substantially.

I had considered another example:

Hwæt. We Gardena in geardagum,
þeodcyninga, þrym gefrunon,
hu ða æþelingas ellen fremedon.
Oft Scyld Scefing sceaþena þreatum,
monegum mægþum, meodosetla ofteah,
egsode eorlas. Syððan ærest wearð
feasceaft funden, he þæs frofre gebad,
weox under wolcnum, weorðmyndum þah,
oðþæt him æghwylc þara ymbsittendra
ofer hronrade hyran scolde,
gomban gyldan. þæt wæs god cyning.
ðæm eafera wæs æfter cenned,
geong in geardum, þone god sende
folce to frofre; fyrenðearfe ongeat
þe hie ær drugon aldorlease
lange hwile. Him þæs liffrea,
wuldres wealdend, woroldare forgeaf;
Beowulf wæs breme blæd wide sprang,
Scyldes eafera Scedelandum in.

But Jeff's poem seemed a little easier...
4 days ago

Biniman Idugboe wrote:Is the term stream a mask of something far more complicated going on behind the scene?

Generally, yes.  We don't necessarily have to study and understand all those details, but yes, there are many things going on behind the scene in a stream.

Biniman Idugboe wrote:For example, where did ReferencePipeline.filter ( come from?

Well, a ReferencePipeline is kind of the standard implementation of a Stream, as provided in the standard libraries.  You can look at the source if you like by studying it in and IDE or by expanding the file in your Java installation.  However, note that it's a package-private class ("default" access), meaning no one is expected to care about it in order to use streams.  Stuff like that can be fairly complicated - it may be fun or even useful to know about, but it takes time and study.  And it isn't actually necessary.  
4 days ago
Well, most of our words are words from foreign languages after all.  Some are used frequently; some infrequently; some almost never.  It's the nature of a spelling bee that they're going to go after the obscure words.  One can also argue that spelling bees don't really test anything particularly useful (at least, for the extremely difficult and obscure words at championship level) - but as long as some people are enjoying the competition and paying attention to it, they can do whatever they need to in order to make it suitably challenging.

I wonder if they really exhausted all the obscure difficult words in the language... or if they merely exhausted all the words they had prepared in their lists.  Maybe next year they will just dig deeper, unearthing words not seen in centuries.  Though in that case, the difficulty is getting an authoritative spelling.  But I think we can still find source material to draw from...

Whan that Aprille with his shoures soote,
The droghte of March hath perced to the roote,
And bathed every veyne in swich licóur
Of which vertú engendred is the flour;
Whan Zephirus eek with his swete breeth
Inspired hath in every holt and heeth
The tendre croppes, and the yonge sonne
Hath in the Ram his halfe cours y-ronne,
And smale foweles maken melodye,
That slepen al the nyght with open ye,
So priketh hem Natúre in hir corages,
Thanne longen folk to goon on pilgrimages,
And palmeres for to seken straunge strondes,
To ferne halwes, kowthe in sondry londes;
And specially, from every shires ende
Of Engelond, to Caunterbury they wende,
The hooly blisful martir for to seke,
That hem hath holpen whan that they were seeke.

4 days ago
Think of the stream as a thing that saves up all the instructions you give it, waiting for a terminal command that says "now go do it".  Each time you add a non-terminal command, it adds something internally to its list of things to do.  But it doesn't even try to look at any elements in the stream, until it receives a terminal command.  Because a stream is designed so that each element can only be viewed once, so it needs to know everything that will be done with each element, before it actually tries to process any element.
4 days ago
Arun: OK, now you've changed the post I was replying to. (And that Campbell was replying to.)  The original version of your post had no code, so obviously I was referring to your original code, which was the only code posted so far.  Now you've posted more code, which changes the answer.  

Anyway, I'm heading home now.  Someone else can answer the new version of the question.
1 week ago
No.  For a static variable, there is only one variable, regardless of the number of threads.  However, if it is a mutable variable, then different threads may see different values before or after a change.  In this case, the variable is final, so it's not mutable, and synchronization is unneeded.  All threads will see the same value, the one that points to the instance that was created when this class was first initialized.
1 week ago

Campbell Ritchie wrote:Yes, OP realised he had a correctly‑written singleton. He was asking what would happen if he hadn't written it correctly.

Yes, that's what I had thought - and yet he was asking which object would be referenced, referring back once again to the original code which was written correctly.  It didn't make sense to me, so I thought it worth restating, that that particular code would not have a problem.  It would make much more sense to discuss some other code that does have a problem.

1 week ago
Oh, here's another:
1 week ago
I think that's the best solution - check if bigDecimalA.compareTo(bigDecimalB) == 0, if true that means they're equal.  There are other things you could do, like check if check if bigDecimalA.doubleValue() == bigDecimalB.doubleValue().  That will give you the right answer as long as your numbers don't use too many digits.  But the first one seems more reliable.
1 week ago
In the code shown the the first post in this thread, there will only be one EagerInitializedSingleton created, regardless of how many threads try to create it or access it.
1 week ago
Yeah, I count 30 variations that (a) use all pieces, (b) exclude all mirrored and rotationally symmetric variations, and (c) keep the same topology and curved piece orientation as the solution shown.  Many more are possible if we violate those. Violating 3 makes the counting more complicated; it seemed a good place to stop.
1 week ago

Campbell Ritchie wrote:

Mike Simmons wrote:. . . It just seems an arbitrary restriction that we can manipulate a mutable field, but not a mutable local.

That is of course because the lifetime of the local variable cannot be guaranteed to be longer than the lifetime of the λ. The same applied to anonymous classes.

Right.  And that's why languages like Scala, Groovy, and Kotlin have been unable to implement closures with access to mutable local variables on the JVM.  Too bad.  If only there were some way to allow the variable to live on as long as there was some reference to it... almost as if the closure were like an object,  and it could not be removed from memory until no other active threads had any way to reach a reference to it.  But surely that's not possible...
1 week ago

Piet Souris wrote:


I think those might give you some compilation problems... ;)
1 week ago
Well, the Scanner is a standard library.  There's no standard for record separators, so it's not surprising you'd need to customize the part that identifies that.

I did miss the fact that you're really asking more about how to parse the fields within the record.  Ok.  Honestly, the method you're using is pretty much what I would do.  Well, returning a Map<String,Integer> rather than a raw HashMap.  But same basic idea.  

While it may seem like this is a common pattern that you'd expect in the standard library... I don't think it's quite that standard.  Someone else might use ';' instead of ',', or ':' instead of '=', or allow additional whitespace, or use whitespace as a delimiter, or need values to contain other special characters, or need to specify escape characters, or need a hierarchical data representation like xml, yaml, or json.  There are many options that might need to be customized.  There are probably third partly libraries for this... but I'd be inclined to roll my own, as you have.
1 week ago
You need to identify exactly how to know when one record begins and another ends, and look for that.  In your example, is foo always the first field?  Is berry always the last field?  Are there always five fields in a record?  Is there always a blank line between records?  Are the fields always in the same order?  Will any of those answers change, ever?  Don't rely on a feature that might not always be there... or might occur in a different order.

I don't think the suggestion of \\s* will work very well... that means you could have a delimiter consisting of zero whitespace characters, which can be found anywhere.  You'll just break the input into a bunch of single characters.

I like the idea of using the blank line as a delimiter... I would use:

This way "record" contains all the lines for one record.

The delimiter might just be "\n\n", but adding the repeating pattern with  \\s* in between allows additional whitespace on the "blank" line, and allows multiple blank lines.  May be unnecessary.
1 week ago