Skip to content

Commit b084010

Browse files
committed
Use terse functional programming terms
When using `fold` the first argument is the accumulator, we can use the typical functional programming identifier `acc` with no loss of clarity. The other argument is a timelock, this is clear because we are iterating `timelocks`, use `t` instead of `sub_timelock` with no loss of clarity (subjective).
1 parent 6f3303d commit b084010

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/miniscript/types/extra_props.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,40 +70,38 @@ impl TimelockInfo {
7070
Self::combine_threshold(1, once(a).chain(once(b)))
7171
}
7272

73-
/// Combines `TimelockInfo` structs, if threshold `k` is greater than one we check for
74-
/// any unspendable path.
75-
pub(crate) fn combine_threshold<I>(k: usize, sub_timelocks: I) -> TimelockInfo
73+
/// Combines timelocks, if threshold `k` is greater than one we check for any unspendable paths.
74+
pub(crate) fn combine_threshold<I>(k: usize, timelocks: I) -> TimelockInfo
7675
where
7776
I: IntoIterator<Item = TimelockInfo>,
7877
{
79-
// timelocks calculation
8078
// Propagate all fields of `TimelockInfo` from each of the node's children to the node
8179
// itself (by taking the logical-or of all of them). In case `k == 1` (this is a disjunction)
8280
// this is all we need to do: the node may behave like any of its children, for purposes
8381
// of timelock accounting.
8482
//
8583
// If `k > 1` we have the additional consideration that if any two children have conflicting
8684
// timelock requirements, this represents an inaccessible spending branch.
87-
sub_timelocks.into_iter().fold(
88-
TimelockInfo::default(),
89-
|mut timelock_info, sub_timelock| {
85+
timelocks
86+
.into_iter()
87+
.fold(TimelockInfo::default(), |mut acc, t| {
9088
// If more than one branch may be taken, and some other branch has a requirement
91-
// that conflicts with this one, set `contains_combination`
89+
// that conflicts with this one, set `contains_combination`.
9290
if k > 1 {
93-
timelock_info.contains_combination |= (timelock_info.csv_with_height
94-
&& sub_timelock.csv_with_time)
95-
|| (timelock_info.csv_with_time && sub_timelock.csv_with_height)
96-
|| (timelock_info.cltv_with_time && sub_timelock.cltv_with_height)
97-
|| (timelock_info.cltv_with_height && sub_timelock.cltv_with_time);
91+
let height_and_time = (acc.csv_with_height && t.csv_with_time)
92+
|| (acc.csv_with_time && t.csv_with_height)
93+
|| (acc.cltv_with_time && t.cltv_with_height)
94+
|| (acc.cltv_with_height && t.cltv_with_time);
95+
96+
acc.contains_combination |= height_and_time;
9897
}
99-
timelock_info.csv_with_height |= sub_timelock.csv_with_height;
100-
timelock_info.csv_with_time |= sub_timelock.csv_with_time;
101-
timelock_info.cltv_with_height |= sub_timelock.cltv_with_height;
102-
timelock_info.cltv_with_time |= sub_timelock.cltv_with_time;
103-
timelock_info.contains_combination |= sub_timelock.contains_combination;
104-
timelock_info
105-
},
106-
)
98+
acc.csv_with_height |= t.csv_with_height;
99+
acc.csv_with_time |= t.csv_with_time;
100+
acc.cltv_with_height |= t.cltv_with_height;
101+
acc.cltv_with_time |= t.cltv_with_time;
102+
acc.contains_combination |= t.contains_combination;
103+
acc
104+
})
107105
}
108106
}
109107

0 commit comments

Comments
 (0)