Oops, my bad, in my haste I misread the original code. (Effectively a reduction on OptionalLong where empty dominates, but the code to write that is I think less clear than the imperative loop.)
Paul. > On Dec 7, 2019, at 11:02 AM, Maurizio Cimadamore > <[email protected]> wrote: > > > On 06/12/2019 21:04, Paul Sandoz wrote: >> GroupLayout.java >> — >> >> 80 OptionalLong sizeof(List<MemoryLayout> elems) { >> 81 long size = 0; >> 82 for (MemoryLayout elem : elems) { >> 83 if (AbstractLayout.optSize(elem).isPresent()) { >> 84 size = sizeOp.applyAsLong(size, elem.bitSize()); >> 85 } else { >> 86 return OptionalLong.empty(); >> 87 } >> 88 } >> 89 return OptionalLong.of(size); >> 90 } >> >> FWIW you can do this: >> >> OptionalLong sizeof(List<MemoryLayout> elems) { >> return elems.stream().filter(e -> >> AbstractLayout.optSize(e).isPresent()).mapToLong(MemoryLayout::bitSize) >> .reduce(sizeOp); > > > Looked at this more carefully - the code you suggest has a slightly different > behavior than the one I wrote - note that the original code short-circuit > when the first layout member with no size is encountered. IIUC your code will > just drop unsized member layouts, and compute size on the rest - which is not > what I had in mind. Am I understanding correctly? > > Maurizio >
