<div dir="ltr"><div class="gmail_default" style="font-family:monospace">Oh, I will also add a specialized version of this method to java.util.function.UnaryOperator. That is because UnaryOperator is a subinterface of Function. Function goes T -> R, but UnaryOperator just goes T -> T. We do not want UnaryOperator to have the T -> R static function included due to inheritance, so it will get its own version that has the same method name and parameters, but the type parameters will be different.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">Here is another mockup - this time for UnaryOperator2's version of the same method.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">
>
interface UnaryOperator2<T> extends Function2<T,T>, UnaryOperator<T><br>
> {<br>
>
<br>
> public static <T> UnaryOperator2<T> ternaryApply<br>
> (<br>
> Predicate<? super T> test,<br>
> Function<? super T, ? extends T> trueFunction,<br>
> Function<? super T, ? extends T> falseFunction<br>
> )<br>
> {</div><div class="gmail_default" style="font-family:monospace">> </div><div class="gmail_default" style="font-family:monospace">> return<br>
> (T input) -><br>
> test.test(input)<br>
> ? trueFunction.apply(input)<br>
> : falseFunction.apply(input)<br>
> ;<br>
>
<br>
> }<br>
>
<br>> }<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Nov 9, 2023 at 12:12 AM David Alayachew <<a href="mailto:davidalayachew@gmail.com">davidalayachew@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:monospace">It has been a month since I sent this proposal. Since no one has told me that this is a terrible idea, I will submit this as an enhancement to JBS, and once the ticket is made, start work on creating a pull request.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Oct 3, 2023 at 3:13 AM David Alayachew <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:monospace">Whoops, bad import.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">Please replace the following line with the one after it.</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">> import java.util.function.Function;</div><div class="gmail_default" style="font-family:monospace"><br></div><div class="gmail_default" style="font-family:monospace">> import java.util.function.*;<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Oct 3, 2023 at 3:09 AM David Alayachew <<a href="mailto:davidalayachew@gmail.com" target="_blank">davidalayachew@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div style="font-family:monospace" class="gmail_default">Hello all,<br><br>I have an idea that I want to run by you all -- a new method on java.util.function.Function to capture ternary operations.<br><br>Here is a mockup of what I wanted to do.<br><br>> <br>> import java.util.function.Function;<br>> <br>> @FunctionalInterface<br>> public interface Function2<I, O> extends Function<I, O><br>> {<br>> <br>> public static <I, O> Function<I, O> ternary<br>> (<br>> Predicate<I> test,<br>> Function<I, O> trueOutput,<br>> Function<I, O> falseOutput<br>> )<br>> {<br>> <br>> return<br>> (I input) -><br>> test.test(input)<br>> ? trueOutput.apply(input)<br>> : falseOutput.apply(input)<br>> ;<br>> <br>> }<br>> <br>> }<br>> <br><br>I think this is useful for a few reasons.<br><br> * This composes, just like the ternary operator itself.<br> <br> * It pairs well with Function.identity() and method references to clearly (but concisely) communicate intent.<br> <br> * Ternary operations are common, so this will find great use by developers of all sorts.<br><br>There is at least one part I don't quite like about this design - what if one (or both!) of your outputs is not a functional transformation of the input?<br><br>For example, String username = id.isBlank() ? "UNKNOWN" : lookup(id);<br><br>Obviously, this is easy to work around - simply ignore the input of the function. But you lose clarity and simplicity that way. I would put a note in the javadoc that says that this method should only be used in instances where both outputs are a functional transformation of the input. That way, intent is clarified. But if we go that route, maybe this function should get a better name to capture that? testThenApply? ternaryTransform? ternaryApply?<br><br>Thank you for your time and help!<br>David Alayachew<br></div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>