by ajuc · 4y ago · 0 comments
// java equivalent
int numberOfAuthors = arr.stream().map(BlogPost::getAuthor).distinct().collect(Collectors.toList()).size();
I happen to think for loops are usually a better choice in languages like Java, but the option is there.EDIT: actually there's a better way, no need to create a list just to count its elements:
int numberOfAuthors = arr.stream().map(BlogPost::getAuthor).distinct().collect(Collectors.counting()); int numberOfAuthors = arr.stream().map(getAuthor).distinct().collect(counting());
You still save a few parentheses and one method call in Ruby (.compact vs .stream() and .collect()) and the method names are shorter. Mostly it's a matter of static vs dynamic typing and naming conventions, not a consequence of Ruby code blocks. And is it worth shortening at this point?BTW I know uniq is a thing in unix, but I hate this naming decision.