Hacker News new | ask | show | jobs
by Godel_unicode 3416 days ago
One thing that I like about Python implementations of algorithms is less thinking about types. When I implement my sort algorithm, I just need to say > or < and assume the caller has given those terms meaning.

Not a Java expert, can you do something similar with Java? Maybe with generics?

2 comments

I'm no expert but due to the language I think you need to specify types somewhere. It just offloads it to your test code as opposed to the algorithms.

With python, your algorithm will work with the primitives without specifying either in the algorithm or test code. You only need to specify type, i.e maybe a new class and definitely instantiation, if you want to use something more abstract.

This would be true of Ruby and other dynamically typed languages. For beginner algorithms courses at least, a dynamically typed languages makes more sense since the algorithms taught are only clouded by typing. Complexity theory still applies and what not.

Maybe if learning algorithms that do heavy crunching, a lower level language, probably with static types, may be used but by that point you're not going to mind type info.

Yes, generics in Java or templates in C++ is how any algorithms library would handle this. Any type passed into such a function needs to possess the required operators and functions or it will fail to compile/run.