I think python is the same way, the ecosystem is so rich that you can really do anything you want (until you get into low latency).
I think python is the same way, the ecosystem is so rich that you can really do anything you want (until you get into low latency).
You can do better in your own code, but you still have exposure to the code in the library ecosystem.
Worth it, though. It really does seem like there's a Java library for everything.
static void main() {
BiFunction<Integer, Integer, Integer> add = (Integer x, Integer y) -> x + y + 5;
Integer result = addTo(10, add);
Integer result2 = addTo(10, (x, y) -> x + y + 5);
}
static Integer addTo(Integer acc, BiFunction<Integer, Integer, Integer> addFn) {
return addFn.apply(acc, 5);
}
Integer eval(Expression e) {
return switch (e) {
case INT(var value) -> value
case ADD(var left, var right) -> eval(left) + eval(right)
case MULT(var left, var right) -> eval(left) * eval(right)
}
}In Scala, I would write:
enum Expr:
case INT(value: Int)
case ADD(left: Expr, right: Expr)
case MULT(left, Expr, right: Expr)
def eval(e: Expr): Int = e match
case INT(value) => value
case ADD(l, r) => eval(l) + eval(r)
case MULT(l, r) => eval(l) + eval(r)
This "match" syntax is the example given in the Scala docs for Pattern Matching:https://docs.scala-lang.org/tour/pattern-matching.html
The fact that Java happens to use "switch" instead of "match" is one of syntax, not semantics.
JDK 17/18 introduces Sealed Types, which allow you to create ADT's
sealed interface Expr {
record INT(Integer value) implements Expr {}
record ADD(Expr l, Expr r) implements Expr {}
// etc
}
When you "switch" over sealed types, the switch expression is exhaustive if all members have branches and requires no default case + is typesound. f [ [1, _]. [3, _] ] = ...
f [ [], [10, 20] ] = ...(Also, behind the scenes they often compile to static functions and called through the invokedynamic instruction)
Example?
As far as I know, Java has final, which means that particular reference can't be re-assigned, but the object referred to remains mutable. You have to resort to e.g. having separate immutable and mutable interfaces or whatever to restrict a someone from mutating your object.
If you want an immutable data class more than one level deep, I don't know if there's a convenient way to do that like there is with const in C++ (or the default behaviour in Rust).
But I'm not a Java programmer, I haven't really kept up with the language. Happy to be proven wrong.
record User(String name, Integer age, Boolean isActive) {}
https://docs.oracle.com/en/java/javase/18/language/records.h...The only way to stop this is to remove the mutable methods from the interface entirely, which is what I'm complaining about.
Nonetheless, recently more and more standard classes are made deliberately immutable, and there was a proposal for frozen arrays as well (not sure on their status).
Although it's easy to to tell people that mutation is confusing and to avoid it, enforcing that is much easier if the compiler is on your side and will prevent mutation with const.
I've encountered unnecessary mutation (introducing implicit assumptions on the order of calls, and making things more confusing) constantly in both Java and C++, but enforcing const in C++ cuts down on that. Or at least, it forces a const_cast which I won't approve without a really good reason.
You're right about Rust of course, internal mutability is possible and maybe even common with RefCell, but culturally it seems like that's avoided. On the other hand, mutability is extremely common in Java.
Maybe I'm just traumatized from some of the horrific code heavily using mutation I've seen over the years.
I see companies downshifting to unmaintainable toys such as Python even in data engineering circles. It's really odd that mobile developers with Kotlin (and front-end ones with Typescript) are getting ahead of backend ones in adoption of modern languages.
Once Loom and Valhalla get merged to an LTS the remaining vestiges of bad old Java will have been gone. I really hope Graal goes mainstream too. That will hopefully blow out of the water the golangs of the world. But those are platform-level improvements any JVM language will benefit from.
It takes a 2/3-of-your-screen plugin configuration to build a Scala project. And you can simply copy that configuration without even thinking about it to another service.
I believe that making the "<dependency>" declaration a one-liner would fix 80% of what's wrong with maven :)
Every single Gradle project I have worked with has its own structure. Which happens even across repositories owned by the same team. There are DSL flavors (Groovy and Kotlin), both are actually used and differ slightly. The wrapper. Its storage is based on Ivy, not Maven so you double the number of Internet replicas on your HDD. But it's still better than SBT ;)
If the JVM is considered low latency I shudder to think what is high latency.
This is incorrect. Firstly, C4 triggers two types of safepoints: thread-local, and jvm-wide. The latter can easily go into the region of ~200 micros for heaps of ~32GB even when running on fast, overclocked servers. Secondly, the design of C4 incurs performance penalty for accessing objects due to memory barriers. This impacts median latency noticeably.
You might not believe me, but ask Gil and he'll openly admit it. This article was written by someone who:
1) doesn't know how C4 works
2) doesn't analyze relevant metrics from their JVMs
Try sub 5 nanos. I was curious awhile ago at how fast C++ hash set lookup was compared to C#, and it consistently performed a lookup at 1 nanosecond. I tested with up to 6GB of data and then stopped because it was taking longer to generate random data then it was to run the benchmark 10,000 times.
C++ benchmarks here[0]. It's a bit more complicated then just a pure lookup since I was pulling some code out of a larger app, but the benchmark is only measuring the lookup speed. I did the C# benchmarks with BenchmarkDotNet or something like that, I can never remember the exact name.
[0]: https://gist.github.com/ambrosiogabe/66a6e2fdc77e6a600e570f4...
TBH I'm skeptical that you are measuring what you think you are measuring. There are a lot of micro-benchmarking pitfalls, like dead code elimination, loop-invariant code motion, unrolling, and other issues. Unless you actually looked at the machine code coming out of the compiler, you're measuring something you don't understand. E.g. 1 nanosecond is roughly 3-6 instructions. That 100% means the hash lookup has been inlined into the benchmarking loop.
Are your hashtables mostly empty? Really small? Lots of easy hits (or easy misses)? Because the slow cases (actually looking up) are going to be hairier and may not be inlined.
Did you benchmark against Java's HashMap? Because it is also very, very, very fast for simple cases.
Don't take my word for it though, you can take a look at the Robin Hood benchmarks[0]. Robin Hood unordered map is a competitive hash map that's performed much better than the STL for me in many cases. They average a 4 nanosecond lookup speed for a hash map with 2000 elements and an integer key.
> Did you benchmark against Java's HashMap?
I benchmarked against C#, which has a runtime that performs similar if not better than the JVM. The C# code was a ~~few microseconds~~ around 130 nanoseconds. Which is still very fast, but up to 100x slower. (And yes, this was after warming up the code. I used benchmark dot net[1] here.). This is a really easy benchmark to set up. If you doubt me you can write a couple of benchmarks in under an hour and compare yourself.
[0]: https://martin.ankerl.com/2019/04/01/hashmap-benchmarks-04-0...
Edit: I just re-ran the benchmarks because I didn't have the results pasted in the snippet (which I've now done so I don't keep getting this wrong haha). The C# HashSet takes around 130 nanoseconds, not microseconds. So it's not orders of magnitude slower, but it is still more than a 2x slow down and up to a 100x slow down in the case of an integer key.
[0]: https://gist.github.com/ambrosiogabe/ba6bd0fa80588c2fd2ca26d...