yangxu998 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Predicates.forFunction(final Function<T, F> function, final F expectedResult) #612

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm curious if anyone has requested the reverse of 
Functions.forPredicate(Predicate<T>):

My colleagues and I often find ourselves creating Functions and Predicates for 
the same fields, and it seems like a method like the following would 
reduce/simply the code necessary to support that kind of functionality:

  public static <F, T> Predicate<T> forFunction(final Function<T, F> function, final F expectedResult) {

    return new Predicate<T>() {
      public boolean apply(@Nullable T input) {
        return Objects.equal(expectedResult, function.apply(input));
      }
    };
  }

Does this seem like a reasonable request?  The name of the method is less than 
desirable -- perhaps something like "predicateFromFunction" is more 
appropriate.  Thoughts?

Original issue reported on code.google.com by stephen....@gmail.com on 25 Apr 2011 at 8:24

GoogleCodeExporter commented 9 years ago
This should be available soon as 
Equivalences.equals().onResultOf(function).equivalentTo(expectedResult) -- 
however unintuitive that incantation might be.

Original comment by kevinb@google.com on 25 Apr 2011 at 8:41

GoogleCodeExporter commented 9 years ago
Wait!  I'm being silly.  This should already be available as 
Predicates.compose(Predicates.equalTo(expectedResult), function)!

Original comment by kevinb@google.com on 25 Apr 2011 at 8:43

GoogleCodeExporter commented 9 years ago
D'oh!  Yeah that's much more elegant.  Thanks!

Original comment by stephen....@gmail.com on 25 Apr 2011 at 9:17

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09