maidh91 / guava-libraries

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

Lazy list, aka a memoized Lists.transform()? #350

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I do not know whether any feature here would have merit. I know that it is 
something that some users have wanted.

The idea is to be like Lists.transform(), but once a transformed value is 
calculated once, remember it and return it henceforth.

The way to accomplish this right now is incredibly involved. Is there some 
valuable feature we should add that would at least streamline this?

    List<Supplier<ExpensiveResult>> suppliers =
        ImmutableList.copyOf(Lists.transform(keys,
            new Function<Key, Supplier<ExpensiveResult>>() {
              public Supplier<ExpensiveResult> apply(Key key) {
                return Suppliers.memoize(Suppliers.compose(
                    MY_EXPENSIVE_FUNCTION,
                    Suppliers.ofInstance(key)));
              }
            }));

    List<ExpensiveResult> results = Lists.transform(suppliers,
        ThisClass.<ExpensiveResult>supplyFunction());

 . . .

  private static <T> Function<Supplier<T>, T> supplyFunction() {
    return new Function<Supplier<T>, T>() {
      public T apply(Supplier<T> supplier) {
        return supplier.get();
      }
    };
  }

Original issue reported on code.google.com by kevinb@google.com on 23 Apr 2010 at 6:29

GoogleCodeExporter commented 9 years ago
The internal Google code base includes a method that caches all values computed 
by a 
function:
  public static <F,T> Function<F,T> memoize(Function<? super F,? extends T> delegate)

With that method, you could simply say
  Lists.transform(list, memoize(function));

Original comment by jared.l....@gmail.com on 13 May 2010 at 1:33

GoogleCodeExporter commented 9 years ago
That's true.  If you're willing to have all the data go into a second data 
structure.. and then you still want to make it look like a List.  And no one 
needs to 
wait for memoize(); all it does is this:

    final Map<F, T> map = mapMaker.makeComputingMap(delegate);
    return new Function<F, T>() {
      public T apply(F from) {
        return map.get(from);
      }
    };

Having two solutions already, I'm closing this.

Original comment by kevinb@google.com on 13 May 2010 at 8:23

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:10