[board] Implement Mailbox::from_iter as a reduce (aka fold) over the incoming iterator
This commit is contained in:
parent
72fd938238
commit
46b19ff616
1 changed files with 10 additions and 12 deletions
|
@ -48,22 +48,20 @@ impl From<[Option<Piece>; Square::NUM]> for Mailbox {
|
|||
|
||||
impl FromIterator<PlacedPiece> for Mailbox {
|
||||
fn from_iter<T: IntoIterator<Item = PlacedPiece>>(iter: T) -> Self {
|
||||
let mut mailbox = Self::new();
|
||||
for placed_piece in iter {
|
||||
iter.into_iter()
|
||||
.fold(Self::new(), |mut mailbox, placed_piece| {
|
||||
mailbox.set(placed_piece.piece(), placed_piece.square());
|
||||
}
|
||||
|
||||
mailbox
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<(Square, Piece)> for Mailbox {
|
||||
fn from_iter<T: IntoIterator<Item = (Square, Piece)>>(iter: T) -> Self {
|
||||
let mut mailbox = Self::new();
|
||||
for (square, piece) in iter {
|
||||
iter.into_iter()
|
||||
.fold(Self::new(), |mut mailbox, (square, piece)| {
|
||||
mailbox.set(piece, square);
|
||||
}
|
||||
|
||||
mailbox
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue