9 ms·
I'm more partial to the first one because it keeps a linear flow downwards, and a uniform structure. The second one kind of drifts off, and reshuffling parts of
by syklemil 2y ago
I'm more partial to the first one because it keeps a linear flow downwards, and a uniform structure. The second one kind of drifts off, and reshuffling parts of it is going to be … annoying. IME the dot style lends itself much better to restructuring.
Depending on language you might also have some `.flat_map` option available to drop the `.reduce`.
- deleted 2y ago[deleted]
- itsmeknt 2y agoTrue! Good point on the restructuring, I haven't thought about it in that way. I think I like the second approach because the loop behavior seems clearest, which helps me analyze the time complexity or when I want to skim the code quickly. A syntax like something below would be perfect for me if it existed: var favoriteFoodsOfFurryPetsOfFamousAuthorsOfLongChineseBooksAboutHistory = books[i].author.pets[j].favoriteFood.distinct() where i = pagecount > 100, language == "Chinese", subject == "History", author.mentions > 10_000 where j = is_furry == True
- syklemil 2y agoYou would likely approach it in any style with some helper functions once whatever's in the parentheses or ifs starts feeling big. E.g. in the dot style you could fn bookFilter(book: Book) -> bool { return book.pageCount > 100 and book.language == "Chinese" and book.subject == "History" and book.author.mentions > 10_000 } var favoriteFoodsOfFurryPetsOfFamousAuthorsOfLongChineseBooksAboutHistory = books .filter(bookFilter) .flatMap(book => book.author.pets) .filter(pet => pet.is_furry) .map(pet => pet.favoriteFood) .distinct()
- dahauns 2y agoHm, LINQ query syntax form is kinda going in that direction (from book in books where book.pagecount > 100 && book.language == "Chinese" && book.subject == "History" && book.author.mentions > 10_000 from pet in book.author.pets where pet.is_furry == true select pet.favoriteFood) .Distinct() But it also demonstrates the...erm, chronic "halfassedness" of LINQ's query syntax form with distinct() not available there and having to fall back to method syntax form anyway...