Skip to content

Commit 80f9c8c

Browse files
committed
implement FromIterator for ArrayBase<S,Ix1>
1 parent 95744f8 commit 80f9c8c

File tree

1 file changed

+25
-19
lines changed

1 file changed

+25
-19
lines changed

src/impl_constructors.rs

+25-19
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,6 @@ where
5252
unsafe { Self::from_shape_vec_unchecked(v.len() as Ix, v) }
5353
}
5454

55-
/// Create a one-dimensional array from an iterable.
56-
///
57-
/// **Panics** if the length is greater than `isize::MAX`.
58-
///
59-
/// ```rust
60-
/// use ndarray::{Array, arr1};
61-
///
62-
/// let array = Array::from_iter((0..5).map(|x| x * x));
63-
/// assert!(array == arr1(&[0, 1, 4, 9, 16]))
64-
/// ```
65-
pub fn from_iter<I>(iterable: I) -> Self
66-
where
67-
I: IntoIterator<Item = A>,
68-
{
69-
Self::from_vec(iterable.into_iter().collect())
70-
}
7155

7256
/// Create a one-dimensional array with `n` evenly spaced elements from
7357
/// `start` to `end` (inclusive). `A` must be a floating point type.
@@ -174,6 +158,28 @@ where
174158
}
175159
}
176160

161+
impl<S,A> std::iter::FromIterator<A> for ArrayBase<S,Ix1>
162+
where
163+
S: DataOwned<Elem = A>,
164+
{
165+
/// Create a one-dimensional array from an iterable.
166+
///
167+
/// **Panics** if the length is greater than `isize::MAX`.
168+
///
169+
/// ```rust
170+
/// use ndarray::{Array, arr1};
171+
///
172+
/// let array = Array::from_iter((0..5).map(|x| x * x));
173+
/// assert!(array == arr1(&[0, 1, 4, 9, 16]))
174+
/// ```
175+
fn from_iter<I>(iterable: I) -> Self
176+
where
177+
I: IntoIterator<Item = A>,
178+
{
179+
Self::from_vec(iterable.into_iter().collect())
180+
}
181+
}
182+
177183
/// ## Constructor methods for two-dimensional arrays.
178184
impl<S, A> ArrayBase<S, Ix2>
179185
where
@@ -327,7 +333,7 @@ where
327333
unsafe { Self::from_shape_vec_unchecked(shape, v) }
328334
} else {
329335
let dim = shape.dim.clone();
330-
let v = to_vec_mapped(indexes::indices_iter_f(dim).into_iter(), f);
336+
let v = to_vec_mapped(indexes::indices_iter_f(dim), f);
331337
unsafe { Self::from_shape_vec_unchecked(shape, v) }
332338
}
333339
}
@@ -422,8 +428,8 @@ where
422428
ArrayBase {
423429
ptr: v.as_mut_ptr(),
424430
data: DataOwned::new(v),
425-
strides,
426-
dim,
431+
strides,
432+
dim,
427433
}
428434
}
429435

0 commit comments

Comments
 (0)