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

Adding stream operators to list flyweight #16

Open
jitsni opened this issue Mar 22, 2017 · 10 comments
Open

Adding stream operators to list flyweight #16

jitsni opened this issue Mar 22, 2017 · 10 comments

Comments

@jitsni
Copy link
Contributor

jitsni commented Mar 22, 2017

Need streaming operators on list flyweight to filter and reduce. For e.g: To filter an item, one could use the following:

listFW.stream().filter(x -> x.id() == key).findFirst()

Otherwise, forEach() needs to be used and store the item in a temporary holder and it is not convenient.

@jitsni jitsni changed the title Adding operators to list flyweight Adding stream operators to list flyweight Mar 22, 2017
@dpwspoon
Copy link

dpwspoon commented Apr 20, 2017

I couldn't find a way to return a stream without creating a backing of the individual instances. I tried the following ways of generating the stream

  1. static <T> Stream<T> iterate(T seed, UnaryOperator<T> f) and
  2. static <T> Stream<T> generate(Supplier<T> s)

Perhaps we could add the Stream methods directly to the flyweight (like rename the flyweight StreamFlyweight).

Otherwise we could look at stream support

@dpwspoon
Copy link

dpwspoon commented Apr 26, 2017

What about adding:

public boolean anyMatch(Predicate<T> filter) {
    for (int offset = offset(); offset < maxLimit(); offset = itemRO.limit()) {
        itemRO.wrap(buffer(), offset, maxLimit());
        if (filter.test(itemRO)) {
            return true;
        }
    }
    return false;
}

@dpwspoon
Copy link

Added anyMatch here: #22. This or related functionality is very useful as doing it in downstream code via forEach requires effectively final variable to be set (aka, need a reference to containing object field).

@jitsni
Copy link
Contributor Author

jitsni commented Apr 26, 2017

anyMatch would be good addition as I am running into the same. For e.g: I want to check if there is any error in one of the items in the list. I am doing this now:

        boolean[] errors = new boolean[1];
        listFW.forEach(h -> errors[0] = errors[0] || h.error());

Stream has the same method, but signature is slightly different.

    boolean anyMatch(Predicate<? super T> predicate);

can you update the signature in the PR ?

@dpwspoon
Copy link

dpwspoon commented Apr 26, 2017

The PR currently generates

public boolean anyMatch(Predicate<T> predicate)

Are you saying it should be Predicate<? super T> predicate ?

or Predicate<T> predicate ?

I’m not sure which is correct but Predicate<T> predicate matches the current implementation of forEach

@jitsni
Copy link
Contributor Author

jitsni commented Apr 26, 2017

I think it should be Predicate<? super T> which is more allowing. For eg if I have ListFW<Http2DataFrame> then I can use Predicate<Http2Frame>. I think even forEach also. But right now, I am not running into this (though I have inheritance relations among the HTTP2 frame types)

@dpwspoon
Copy link

Ok, I'll change both on the PR. Also, I would like to remove the quotes on the toString as I just spent quite some time debugging an issue where I did toString() instead of asString()... thoughts?

@dpwspoon
Copy link

I hit the above today and needed the <? super T> for the forEach :-). PR is updated

@cmebarrow
Copy link

@jitsni Can we close this given that PR #22 (for anyMatch) has been merged?

@dpwspoon
Copy link

Reopening, I would still like this or something equivalent. Because stream().filter() requires intermediate objects (I believe), perhaps we could implement:

matchFirst(x -> x.id() == key) or firstMatch(x -> x.id() == key)

@dpwspoon dpwspoon reopened this Dec 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants