[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 {
|
impl FromIterator<PlacedPiece> for Mailbox {
|
||||||
fn from_iter<T: IntoIterator<Item = PlacedPiece>>(iter: T) -> Self {
|
fn from_iter<T: IntoIterator<Item = PlacedPiece>>(iter: T) -> Self {
|
||||||
let mut mailbox = Self::new();
|
iter.into_iter()
|
||||||
for placed_piece in iter {
|
.fold(Self::new(), |mut mailbox, placed_piece| {
|
||||||
mailbox.set(placed_piece.piece(), placed_piece.square());
|
mailbox.set(placed_piece.piece(), placed_piece.square());
|
||||||
}
|
mailbox
|
||||||
|
})
|
||||||
mailbox
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromIterator<(Square, Piece)> for Mailbox {
|
impl FromIterator<(Square, Piece)> for Mailbox {
|
||||||
fn from_iter<T: IntoIterator<Item = (Square, Piece)>>(iter: T) -> Self {
|
fn from_iter<T: IntoIterator<Item = (Square, Piece)>>(iter: T) -> Self {
|
||||||
let mut mailbox = Self::new();
|
iter.into_iter()
|
||||||
for (square, piece) in iter {
|
.fold(Self::new(), |mut mailbox, (square, piece)| {
|
||||||
mailbox.set(piece, square);
|
mailbox.set(piece, square);
|
||||||
}
|
mailbox
|
||||||
|
})
|
||||||
mailbox
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue