Skip to content

Commit

Permalink
fix a crash in each with 3+ arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Feb 28, 2024
1 parent 2c508f2 commit 77416ae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/algorithm/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,10 @@ fn eachn(f: Function, mut args: Vec<Value>, env: &mut Uiua) -> UiuaResult {
} else {
let mut arg_elems: Vec<_> = args
.into_iter()
.map(|val| -> Box<dyn Iterator<Item = _>> {
if val.rank() == 0 {
Box::new(repeat(val).take(elem_count))
} else {
Box::new(val.into_elements())
}
.map(|val| {
let repetitions = elem_count / val.element_count();
val.into_elements()
.flat_map(move |elem| repeat(elem).take(repetitions))
})
.collect();
for _ in 0..elem_count {
Expand Down
11 changes: 11 additions & 0 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ impl Value {
Self::Box(array) => Box::new(array.into_rows().map(Value::from)),
}
}
/// Get an iterator over the elements of the value
pub fn elements(&self) -> Box<dyn ExactSizeIterator<Item = Self> + '_> {
match self {
Self::Num(array) => Box::new(array.data.iter().copied().map(Value::from)),
#[cfg(feature = "bytes")]
Self::Byte(array) => Box::new(array.data.iter().copied().map(Value::from)),
Self::Complex(array) => Box::new(array.data.iter().copied().map(Value::from)),
Self::Char(array) => Box::new(array.data.iter().copied().map(Value::from)),
Self::Box(array) => Box::new(array.data.iter().cloned().map(Value::from)),
}
}
/// Cosume the value and get an iterator over its elements
pub fn into_elements(self) -> Box<dyn Iterator<Item = Self>> {
match self {
Expand Down
1 change: 1 addition & 0 deletions tests/loops.ua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ A ← ↯2_3_4⇡24
⍤⟜≍: ∵(++) [1] [2 3 4] 5 ++ [1] [2 3 4] 5
⍤⟜≍: ⬚0∵(++) [1] [2 3 4] 5 ⬚0(++) [1] [2 3 4] 5
⍤⟜≍: ⬚0∵(++) [1] [2 3 4] [5 6] ⬚0(++) [1] [2 3 4] [5 6]
⍤⟜≍: [[1_2_8 1_3_8 1_4_8] [1_5_9 1_6_9 1_7_9]] ∵[⊙⊙∘] 1 [2_3_4 5_6_7] 8_9

⍤⟜≍: [1_2 1_3 1_4] ≡⊟ 1 [2 3 4]
⍤⟜≍: [1_2 1_3 1_4] ⬚0≡⊟ 1 [2 3 4]
Expand Down

0 comments on commit 77416ae

Please sign in to comment.