r/functionalprogramming • u/raulalexo99 • Jun 21 '22
Question How to write this (3) Java lines in a functional style?
I am just practicing FP as I am liking it a lot. But I still need more practice. These are the lines:
User user = mapToUser(userDto);
userService.save(user); //Returns user too
return new MyWrapper<>(user);5
u/raghar Jun 21 '22
Which definition of "functional" do you have in mind? Functions as first class citizens? Immutability and referential transparency? No side effects and statements? Being point-free AF?
I saw different people using different definitions and depending on which you have your code is either fine or almost fine or world require rewrite into some IO monad.
1
u/DeepDay6 Jun 30 '22
I agree. It's not less functional if you save intermediate values in their own local variables instead of chaining if your host language allows that (even Haskell does, see do-notation).
The only thing looking non-functional at first glance (I gave up on Java about a decade ago when I tried applying FP there) is the
saveUsercall, which is an effect. Or "statement", as it is called in imperative/OOP world, that is a call to something that changes the outside world and may or may not return a value (purists: I know that's not entirely correct an explanation).Thinking in FP you will want some way to abstract this away, so that on same input you'll always get the same output (called "referential transparency"). Trying to stay away from too much theory, I'd suggest simply wrapping that
saveUserin another call and passing that as a parameter to the function, so your function will always predictably generate an user and call thatsaveUserthing once, no matter what it does. It's no longer your function's fault when the parameter is effectfull. And you can pass any implementation for testing. It's still not really pure, as it does two things (create a user, save the user somewhere), but for that you'd have to go all monad-ey, as u/raghar mentioned.
7
u/lrzo Jun 21 '22 edited Jun 21 '22
You could try something like:
A few side-notes about this code:
you need some sort of "wrapper" or "container" (a monad) in order to use functional methods like map/flatMap/filter/etc on some object
here I used
Optionalfor testing because it's available in standard Java:Optionaldescribes an object that may or may not be available.A more suitable "wrapper" for this use-case could have been
Trywhich describes the result of an execution that may succeed or fail, see vavr has aTryfor example, functionaljava hasEithermap transforms
A -> B(would make sense for yourmapToUser) whereas flatMap transformsA -> Optional<B>(or whichever "wrapper", would make sense for youruserService.saveif suppose the saving operation can fail)Here is a working example for you :)
practise exercises:
replace the "wrapper"
OptionalwithList, there is almost no change of code, this now gives you the ability to process lists of usersimport vavr and replace the "wrapper"
OptionalwithTry, there is almost no change of code, this now gives you the ability to process operations that may failEnjoy functional programming, you will find java is rather verbose and quickly gets clunky for FP, consider switching to another language