Skip to content

Commit 2cd5257

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 8201bca commit 2cd5257

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
@@ -69,40 +69,38 @@ impl TimelockInfo {
6969
Self::combine_threshold(1, once(a).chain(once(b)))
7070
}
7171

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

0 commit comments

Comments
 (0)