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

Add Stream<DomContent> variants of each and with #118

Merged
merged 1 commit into from
May 20, 2018
Merged

Conversation

snago
Copy link
Contributor

@snago snago commented May 20, 2018

The each-methods that takes a Collection or a Map and a Function or BiFunction are nice, but accepting Stream directly would be even more powerful.

each(filter(numbers, n -> n % 2 == 0), n -> li(n.toString()))

would become

each(numbers.stream().filter(n -> n % 2 == 0).map(n -> li(n.toString())))

Sure, that's not shorter but it means other stream functionality such as limit, flatMap, etc. won't have to be duplicated as TagCreator methods. E.g.:

each(numbers.stream().filter(n -> n % 2 == 0).limit(10).map(n -> li(n.toString())))

@tipsy
Copy link
Owner

tipsy commented May 20, 2018

That's a nice approach. Maybe the other methods should be deprecated in favor of this. Could you type of a few examples so I can compare?

@snago
Copy link
Contributor Author

snago commented May 20, 2018

Yes, TagCreator.filter, TagCreator.each(Collection, Function) and TagCreator.each(Map, Function) can probably be deprecated in favor of TagCreator.each(Stream).
I would keep TagCreator.each(Map, BiFunction) though, as that can be easier to use than having to deal with a stream of entries.

Here is the filter example from https://j2html.com/examples.html, rewritten to use the new each-method and with the employees sorted by name:

    body(
        div(attrs("#employees"),
            p("Some sibling element"),
            each(employees.stream()
                .filter(Objects::nonNull)
                .sorted(Comparator.comparing(Employee::getName))
                .map(employee -> div(attrs(".employee"),
                    h2(employee.getName()),
                    img().withSrc(employee.getImgPath()),
                    p(employee.getTitle())
                ))
            )
        )
    )

@tipsy
Copy link
Owner

tipsy commented May 20, 2018

Thanks! I'll merge this now and think about deprecating the other methods.

@tipsy tipsy merged commit cd6e008 into tipsy:master May 20, 2018
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

Successfully merging this pull request may close these issues.

None yet

2 participants