8 ms·
Great I am looking forward to seeing mode code like this: timeslotRepo.findAll().stream().filter(timeslot->timeslot.getEndDate()==null).first().ifPresentOrEl
by wrnr 4y ago
Great I am looking forward to seeing mode code like this:
timeslotRepo.findAll().stream().filter(timeslot->timeslot.getEndDate()==null).first().ifPresentOrElse(()->{}, ()->{new Timeslot(DateTime.now())})
- tr1ll10nb1ll 4y agoWell, that's an ignorant reduction of functional programming but suit yourself.
- nyanpasu64 4y agoIt's exactly the experiences I've had trying to read functional-style Rust code that uses higher-order function methods as a replacement for control flow primitives.
- sidlls 4y agoYes, it’s far too readable to be actual functional programming.
- ch4s3 4y agoI only write code in an FP language and it never looks like that. Usually I’m passing function results into another function or into a match to break out success and failure states. It doesn’t have to be Haskell wizardry, or even clever.
- tr1ll10nb1ll 4y agoThat's a slap in the face to Erlang/Elixir or maybe you weren't able to comprehend it?
- sidlls 4y agoReadability and the ability to comprehend code with poor readability aren’t really related. I was having a bit of fun at the expense of FP zealots.
- ParetoOptimal 4y agoIf you added line breaks I wouldn't be sure if you were arguing for or against functional programming.
- raiflip 4y agoSo you're filtering specifically for timeslots where the end date is null and therefore guaranteeing that you return a timeslot dated to now?
- wrnr 4y agoif timeslots.stream().matchNone(Timeslot::hasntEnded) { new Timeslot() } Would be an improvement I guess. The way I found this code it was also wrapped in a @Transaction so it creates a new object in the database with spring magic. There was a lot of other things wrong with the code, that was not my point though. Lots of people just think FP better and of course in theory they are right but most people don't think for themselfs. People are quick to act insulted whenever I give some critical remarks about FP. When would you use a forEach function instead of a for each statement, I suggest maybe only use the function if the callback is actually a higher order function you get from somewhere else as I don't see the point of wrapping your logic in a closure. Same thing with the use of observables/reactivity in UI programming, again it is illegal to question this orthodoxy. But then you take a look at how graphics programmers use immediate mode programming and fancy compute shaders to draw the entire frame every frame it makes me smile at how much simpler everything becomes.
- mypalmike 4y agoI'm generally a proponent of OO, and I actually think this kind of filtering chain is one of a number of places where fp code can be more readable than imperative loops.
- Jtsummers 4y agoIt's also, more or less, the "fluent interface" style if laid out differently, other than the lambdas (but even those are in both C# and Java): timeslotRepo.findAll() .stream() .filter(timeslot->timeslot.getEndDate()==null) .first() .ifPresentOrElse(()->{}, ()->{new Timeslot(DateTime.now())}) https://en.wikipedia.org/wiki/Fluent_interface#Java https://en.wikipedia.org/wiki/Fluent_interface#Java
- peanut_worm 4y agoIf you used line breaks I don’t see how thats any less readable than a block of procedural code.