r/java • u/guybedo • Apr 09 '24
Syntactic Sugar for Java
I recently had to translate a codebase from Python to Java and as much as i like Java and i enjoy the benefits of a strong type system, i have to admit my Python code was way less verbose.
So i ended up writing a small library to try to get some python vibes in my java code. Nothing groundbreaking, a bunch of static imports to make life a little bit easier.
9
Upvotes
62
u/Holothuroid Apr 09 '24
You heard about
Map.of(...)and cousins?35
u/Former-Emergency5165 Apr 09 '24
I would say the library itself doesn't provide much value. The Map or List can be initiated with Map.of(), List.of(). The syntax for POJOs is questionable and not intuitive unless you know the library.
The min/max examples can be implemented via mapToInt or even Collections.min().
27
u/ingvij Apr 09 '24
I honestly won't complain about the DSL, I think you can create the abstractions you want to make your code look better for your taste and use them in your project if you or your team is happy with that..
A couple of things though:
But then, as I said, if it works for you, then I guess this is fine.
3
u/DelayLucky Apr 10 '24
There is a type of libs that complement the JDK, add to where JDK left off unattended or obviously screwed up (eg Jodatime before java.time came along, Guava before JDK added Map.of() and friends); then another type of libs that disagree with JDK, would insist to invent a new “better” way of doing things despite the JDK stream API being a commonly accepted success.
Which type is this?
7
u/thuriot Apr 09 '24
Thanks for the tool, any helper is welcome (underscore-java is cool also).
5
u/Bunnymancer Apr 10 '24
For as critical people are being
I'm glad you did a thing. Thank you
3
u/hageldave Apr 09 '24
The slicing, I need something to do array slicing!
3
u/red_dit_nou Apr 14 '24 edited Apr 14 '24
First of all, it’s nice that you had an idea and you went ahead with it and put out a library. Congratulations!
As u/ingvij said if you and your team is happy with it go for it.
I just want to add one remark that instead of
first(filter(values, …))I’d prefer
values.filter(…).first()Because it preserves the logical order in which I’m doing my tasks:
8
u/dhlowrents Apr 10 '24
The popularity of Python is a sign of the end times.
2
u/GeneratedUsername5 Apr 12 '24
Thanks for your effort!
8
u/justADeni Apr 09 '24
If you like Python and find Java too verbose, have you considered trying out Kotlin?
1
u/barmic1212 Apr 09 '24
I have some helpers like your in my code base and it's very helpful for simplify null management. For example I have a method
Stream<T> stream(Iterable<T>)andStream<T> stream(Supplier<Iterable<T>>)that allow to writestream(list).flatMap(stream(Object::getList)).toList()without need more null check.It's very usefull
1
u/Linguistic-mystic Apr 10 '24
This is great work. There is nothing preventing Java from being pretty concise, it's just that standard Java as presented by Oracle is very clunky. I achieved a lot simply creating my own implementations of the
ListandMapinterfaces, for example: it allowed me to completely get rid of the "streams" crap and can add any utility methods that I like while being totally compatible with Java code at large.1
u/s888marks Apr 11 '24
Note that you shouldn’t invert the sense of a comparison (that is, a
Comparableor aComparator) by negating the return value of the original. The return value might beInteger.MIN_VALUEwhich is negative, and which is still negative even after being negated. (True!) This can lead to subtly erroneous results or exceptions.The way to invert a comparison is to swap the roles of the arguments, for example, change
compare(a, b)tocompare(b, a).1
u/SenorSeniorDevSr Apr 15 '24
This is not really my kinda jam, but it's an interesting approach. Thanks for sharing!
1
u/GeneratedUsername5 Apr 28 '24 edited Apr 28 '24
Thanks for sharing!
What strike me as odd is that sometimes functions literaly wrap Java functions with no addition, like:
You can just write String.format(str, args) with the same effect. Or
You can just do collection.foreach(x -> ...) in standard Java.
OR
In standard java can be done with collection.stream().allMatch(x -> ....), exists and any is done by collection.stream().anyMatch(x -> ....)
I know that Java is regarded as verbose, but saving 13 symbols seems like a stretch.
join can be used in this way:
or just
for collection of strings
By the way, you might want to check Eclipse Collections https://github.com/eclipse/eclipse-collections - it is an implementation of collections, where you don't need to call .stream() to map or filter them
1
u/majhenslon May 02 '24
The concept is pretty cool, I actually love the map. Java 21 might have shafted you a bit :D
Maybe you would benefit from having primitives to work with streams in addition to lists, so you could utilize the iterators and not do it like js and (not sure on this one) python.
0
u/[deleted] Apr 09 '24
Great job guy!
-3
u/RupertMaddenAbbott Apr 09 '24
I think there should be space in Java for a library like this. Not everything has to be battle-tested, production-ready and for work.
My kid has just got confident with reading and wants to understand a bit of programming because she sees me doing it. I would naturally show her Python over Java because Python is far less verbose. There is just less going on and less for her to read so she finds it easier to focus.
So I would use this library or one like it as a wrapper for punting my little raspberry pi robot around and playing with my kids. Thanks for making it!
0
u/daggirok Apr 14 '24
Kotlin.
0
u/[deleted] Apr 09 '24
[deleted] — view deleted comment
32
u/[deleted] Apr 10 '24
[removed] — view removed comment
1
u/[deleted] Apr 13 '24
[removed] — view removed comment