Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: Support for common primitive functions (int, long, double) #34

Open
TobiasRoland opened this issue Nov 10, 2018 · 5 comments
Assignees

Comments

@TobiasRoland
Copy link
Contributor

TobiasRoland commented Nov 10, 2018

For better or worse,java.util.function package has specific classes for primitives. I think it would make sense to have "feature parity" with the java provided functions, but wanted to hear if this would be something you're willing to look at a PR for before I spend a bit of time writing it.

So a sort of simple example of what this change would allow:

IntStream.of(1,2,3) 
  .map(unchecked(this::takesIntThrowsChecked)));

DoubleStream.of(1.0, 2.0, 3.0) 
   .filter(unchecked(d -> {
       if (d > 2.0) throw Exception("Oh no!");
       return true;
   }));

LongStream.of(1,2,3)
.mapToObj(unchecked(x -> {
   if (x > 2) throw Exception("Oh no!")
   return "" + x;
}); 

Specifically I'd be looking to implement Throwing versions of:

  • DoubleBinaryOperator
  • DoubleConsumer
  • DoubleFunction
  • DoublePredicate
  • DoubleSupplier
  • DoubleToIntFunction
  • DoubleToLongFunction
  • DoubleUnaryOperator
  • IntBinaryOperator
  • IntConsumer
  • IntFunction
  • IntPredicate
  • IntSupplier
  • IntToDoubleFunction
  • IntToLongFunction
  • IntUnaryOperator
  • LongBinaryOperator
  • LongConsumer
  • LongFunction
  • LongPredicate
  • LongSupplier
  • LongToDoubleFunction
  • LongToIntFunction
  • LongUnaryOperator

I imagined doing something like this for each of the above:

interface ThrowingIntFunction<T extends Integer, R, E extends Exception> extends ThrowingFunction<T, R, E> {

    R apply(T arg) throws E;

    static <T, R> Function<T, Optional<R>> lifted(final ThrowingFunction<T, R, ?> f) {
        return requireNonNull(f).lift();
    }

    static <T, R> Function<T, R> unchecked(final ThrowingFunction<T, R, ?> f) {
        return requireNonNull(f).uncheck();
    }

    static <T1, R> Function<T1, R> sneaky(ThrowingFunction<? super T1, ? extends R, ?> function) {
        requireNonNull(function);
        return t -> {
            try {
                return function.apply(t);
            } catch (final Exception ex) {
                return SneakyThrowUtil.sneakyThrow(ex);
            }
        };
    }
}
@pivovarit pivovarit self-assigned this Nov 10, 2018
@pivovarit
Copy link
Owner

That's a very good idea - targeting 2.0.0

@pivovarit
Copy link
Owner

...just trying to figure out how to come up with some generator for them - maintaining all of these by hand is not realistic

@rwperrott
Copy link

Indeed, that is plenty more work, which I'm trying for my own fork; not just refactor copy class and some interface renaming, but including subtle stuff like using Optional{primitive-name}, and some functional interfaces for primitives not having a super interface.

@rwperrott
Copy link

For me, https://kevinbirch.github.io/string-template-maven-plugin/ looks promising for Maven driven code generation; the plugin looks old, but it maybe possible to cause it to use a newer https://www.stringtemplate.org/ version, if required.

@rwperrott
Copy link

I implemented all of the primitive using functions in StringTemplates (*.st) files and many pom template reference entries at https://github.com/rwperrott/throwing-function using my fork of string-template-maven-plug at https://github.com/rwperrott/string-template-maven-plugin.

The original string-template-maven-plugin somehow works from NetBeans 12, but fails with an interface incompatibility from IntelliJ IDEA 2020.1, so I enhanced it and upgraded the POM.

@pivovarit pivovarit removed the 2.0.0 label Jan 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants