mimblewimble / grin

Minimal implementation of the Mimblewimble protocol.
https://grin.mw/
Apache License 2.0
5.04k stars 991 forks source link

folding suggestion #829

Closed tromp closed 6 years ago

tromp commented 6 years ago

pmmr.rs lines 215..221 read

            let mut bagged = None;
            for peak in self.peaks.iter().cloned() {
                bagged = match (bagged, peak) {
                    (None, rhs) => Some(rhs),
                    (Some(lhs), rhs) => Some(lhs.hash_with(rhs)),
                }
            }

This is folding the function hash_with over the elements of self.peaks, which should be a one liner using the rust function fold1

hashmap commented 6 years ago

fold1 is a part itertools which is an external crate used in grin crate but not (yet?) in core. Is it ok to add as an additional dependency to core? With regular fold code is just a bit shorter, smth like

    return self.root == self.peaks.iter().fold(ZERO_HASH, |b, &p| {
                if b == ZERO_HASH {
                    p
                } else {
                    b.hash_with(p)
                }
            });
sesam commented 6 years ago

@hashmap I'm a bit unhappy with adding dependencie in general. For this specific case, maybe the code above might get replaced anyway

tromp commented 6 years ago

code to be obsoleted in https://github.com/mimblewimble/grin/issues/797