Skip to content
Advertisement

Rewrite nested ternary operators in functional way

I have the following piece of code:

JavaScript

Could you please help me simplify the above by means of the functional programming?

I would like to use something similar to

JavaScript

Advertisement

Answer

JavaScript

Collectors.joining will only insert a delimiter if there are 2 or more elements. In your case, that’s when both are non-empty.

If both are empty, the result is ""

If first is empty, the result is "second"

If second is empty, the result is "first"

If both are non-empty, the result is "first<br>second"

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement