Win a copy of High Performance Python for Data Analytics this week in the Python forum!

Jesse Silverman

Ranch Foreman
+ Follow
since Oct 25, 2020
Been programming "since forever" but Java was always a second (or third) language.  It's moving to the front seat.  I'm mostly harmless.
New York
Cows and Likes
Cows
Total received
8
In last 30 days
4
Total given
0
Likes
Total received
18
Received in last 30 days
6
Total given
31
Given in last 30 days
11
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jesse Silverman

So working in Python now and reading a lot about it...

Attempts to insert mutable collections into a Hash insta-fail, the way the food processor stops spinning when you remove the top to shove your hand into it...in fact, you can't even get a hash value for them...

I thought of Java as being safety-obsessed and Python being the Wild, Wild West (Present Company Excepted) but there's that one thing at least where the roles are reversed.
I bet I'll find more...

Immutable Sequence Types
The only operation that immutable sequence types generally implement that is not also implemented by mutable sequence types is support for the hash() built-in.

This support allows immutable sequences, such as tuple instances, to be used as dict keys and stored in set and frozenset instances.

Attempting to hash an immutable sequence that contains unhashable values will result in TypeError.



Now, that last part is DEEP!
8 hours ago
It is mildly annoying that Python uses the keyword "is" operator to tell if two references point to the same identical instance, and == tests for content equality.

Well, by itself it is fine, but it gets annoying when you jump back and forth between Python and Java.

I think the real point that Sun and Oracle really meant to drive home the whole time was just "Don't use == to compare strings!" but you would never know that from some of the mock exam questions.

The value returned for id() is way longer on 64-bit Python than on 32-bit.
I don't know if that means that it is an actual address or that the number of possible objects is just much larger, I understand that there is major benefit in never knowing the exact value so that garbage collector can go to town cleaning up the heap without disturbing the application.

They changed a lot in Python 3 since they were willing to break compatibility, but id(my_reference) is still there in 3.x

It is interesting to see the many similarities and handful of differences in how Java and Python treat constant pooling, but less interesting than learning how to use each effectively and safely to create readable, correct programs that run pretty well.

That lesson carries over 100%

I am unimpressed that Python happily allows us to mix boolean, float, integer, str and other objects in a list, but it is less surprising in a dynamic language than someone who arrived late to the party to Java and sees a language with Strong Static Typing letting you do the same thing, Lector Emptor (retrieve elements very carefully).

I still support treating the fact that you can throw all sorts of random junk into a List in either one of them as closer to a bug than a feature.

Everyone should know you can do it, but that is hardly a reason to celebrate it [I have seen tutorial material celebrating this as some kind of cool neat great thing in both languages -- ugh!]

Thanks for the lessons that carry over 100% to the other languages I'll be focusing on for a while.
I'd say the two things that make me look forward to my focus returning to Java at some undetermined point in the future to be secondly, streams and try-with-resources, and firstly, the presence here of you guys.

When I get to the point in Python that I believe common reference material regarding stuff I am doing is confused about or deficient in their explanations in, I will be sure to land in the appropriate forum here on Java--err--CodeRanch.
All of this has fallen out of scope for me, as I have accepted a C++/C#/Python job.

I just wanted to add this tidbit that helps answer the musical question:
"Why do generally fairly competent presenters leave a misunderstanding about the extent of String Pooling in Java?  They don't seem to be generally ignorant."

The ones I saw doing this are bilingual in Java and Python, and I think their Python knowledge cross-contaminated their understanding/recall of Java behavior.

Evidence submitted -- note that completely unlike Java, Python will happily show you the address of objects on the heap.  I am unaware of the full implications of this, probably it is a historical accident that should be avoided, but anyway, see how if you were doing Python all day you might, as people preparing material I had read or watched, imagine String pooling that will never happen in Java without an explicit .intern() call:


You would never see the equivalent of the last two occur in Java, it is probably down to the difference of interpreted versus compiled-interpreted, but anyway, I'll never make this mistake in Java again, I like to believe, but now I see why I was - people presented String Constant Pooling in Java as doing more than it does, and I believe it was due to them using a lot of Python at the same time.
I see traits and properties -- that means....Scala?
2 weeks ago
I  was reviewing my C# notes because I may be taking a job with a lot of C# and just noticed the existence in them of:
IEquatable<T> interface defining Equals(T t)

This (very old) reference on Stack Overflow suggests that when Project Valhalla finally gets here (checks sky for pigs) we might make good use of it depending on exactly what allowing value types looks like (I can't even remember if it is just for primitives or allows custom value types like C#):
https://stackoverflow.com/questions/2476793/when-to-use-iequatablet-and-why#:~:text=The%20IEquatable%20%28T%29%20interface%20is%20used%20by%20generic,such%20methods%20as%20Contains%2C%20IndexOf%2C%20LastIndexOf%2C%20and%20Remove.

Just posting this here because I will forget about it otherwise.
Or forget about this thread, if I am doing enough C# and Python.
2 weeks ago
Like a recent poster, I was in over my head watching a presentation done by someone who is in the Java source code as much as writing Applications, I don't have that link.

I haven't watched tons of his stuff yet, but this guy who teaches programming for a living has several good videos on Spliterators, I think he tries to get things right in his presentations rather than just going for big popularity, as he is a C.S. Professor for his day job (Note, I appreciate everyone trying to educate people for free, but as you know, some people don't bother to learn the material very well before they rush to present it and can accidentally confuse viewers rather badly):

https://www.youtube.com/watch?v=bwGasGTZhkY

My personal notes page on Iterator types looks like:
Enumeration -- Legacy, do not use in new code
Iterator -- Only goes forward, can look and remove only, but valid for all Collection types
ListIterator -- goes forward and back, can add, remove, update, but only for List
Spliterator -- these things are awesome, go learn about them (TBD)
2 weeks ago
In the list of Pre-Requisites that might be necessary to fully understand the example the Original Poster had found and presented I believe I neglected VarArgs, also "new" for Java 5 in 2004.

A one sentence description would be that it allows you to treat zero or more arguments of the same type occurring at the end of a parameter list, or the only elements of a parameter list as an array, or to actually pass in an array, but longer presentations beat it to death for over an hour showing how it interacts with method overloading.

It is useful in many places in Java, and tricky, so researching VarArgs in Java would also yield a number of helpful tutorials.

I note that when diving into Functional Programming with Streams, Jeanne and Scott warn that one had better be on top of all of the topics mentioned before moving forward further to avoid some serious confusion or feeling overwhelmed.  Many otherwise good presentations including at least a couple I linked to presume you have been using these pre-requisites daily in code for quite some time and that you are quite familiar with their nuances.
2 weeks ago
I'm going to agree with you Campbell (big surprise).

I think the real blame lies at the foot of those that present the enhanced for loop (not used here) as a one-stop easy solution, where it really just hides the iterator you would need to do this safely.

At a minimum, they should probably mention that it is  just hiding the iterator for us.

A better presentation would be that the enhanced for is fine for looking at one element at a time and doing something based on that.

The Iterator is fine for moving forward thru things explicitly and perhaps removing some of them (using the Iterator).

If you have a List, the ListIterator let's you move forward or back, and add, remove or update safely as well (thru the ListIterator).

I have seen presentations where the Spliterator was described as doing Iterators right, in ways additionally to parallelizability...better API, but older presentations and those that should perhaps be updated but have not been, never even mention it, leaving it sounding like something that will be used solely with Streams.

Which ties into a discussion I've seen here about how Streams API is often left as an "advanced" topic while they describe techniques available in 2005 as "normal" Java.

Interesting to me this weekend as I am dedicated to Streams for right now, hooping to make them part of my Go-to approach for coding, rather than as an advanced or exotic topic.
2 weeks ago
I had tried to help out the poster of the following topic thread:
https://coderanch.com/t/738150/java/BiPredicate-function-argument

There was a lot of background knowledge that they were missing for their question.
I provided some references that were outside of CodeRanch.

Did I violate a Rule I need to be learning for good right now in my answer?
Did the OP or someone else delete the thread for some other reason?
Did it get moved?

It still shows as "Best This Month" here, (not that it has much competition):
https://coderanch.com/f/143/lambdas-streams

Otherwise it seems to have been deleted or moved.
2 weeks ago
I have just completed the section and agree with your assessment.

While it doesn't make any false statements, it would add very little text to make perfectly clear that Function chaining with Function's .andThen() works similarly to .compose() and differently to Consumer's.
Thank you Stephan!

I tend to go for intermediate variables to make code clearer.
Not just to read, but to debug.

I have spent years of my life (well-paid, but otherwise lost) debugging

result = fun1( fun2( x + y, z) / fun3( w % average( c[0]  - fun4() ) ) );

when that code someone else wrote yielded a crash/stack trace "at line 791".

Not an edifying use of time.
2 weeks ago
On page 181 in Chapter 4, We call back to this saying "In Chapter 3, we used Function with the merge() method."
Here too we should say BiFunction instead.
Less important, but every decrease in opportunity to mis-remember something helps...
The default scope of the Search was more restricted in terms of Forum than I had realized when I hit Search.

My new default is to look and pay attention to which forums I am searching before I hit <Search>
2 weeks ago
Your rationale is not correct, however, many would blame Iterator interface for being confusing.

When you are iterating thru a collection, you may use the iterator itself to remove an element.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Iterator.html
Note that shows you a safe and effective way to remove an element.

If you use the general method to remove an element, it invalidates your iterator, which is unaware that the collection it is iterating thru has changed.

It is pretty confusing to see concurrent modification errors in single-threaded code.
They come about because the Iterator can only deal with removals it has performed itself.  Any other changes to the Collection it is iterating thru pull the rug out from under it.
2 weeks ago
Standard and free, here are the official Oracle tutorials on Collections and Generics.  They cover some of the basics that seemed to be making it more difficult for you to understand the presentation you had found on BiPredicate<> which presumes familiarity with these topics:

https://docs.oracle.com/javase/tutorial/collections/index.html

https://docs.oracle.com/javase/tutorial/extra/generics/index.html
2 weeks ago