tustvold commented on code in PR #3258:
URL: https://github.com/apache/arrow-rs/pull/3258#discussion_r1038402887
##########
arrow-array/src/array/boolean_array.rs:
##########
@@ -173,6 +174,92 @@ impl BooleanArray {
) -> impl Iterator<Item = Option<bool>> + 'a {
indexes.map(|opt_index| opt_index.map(|index|
self.value_unchecked(index)))
}
+
+ /// Create a [`BooleanArray`] by evaluating the operation for
+ /// each element of the provided array
+ ///
+ /// ```
+ /// # use arrow_array::{BooleanArray, Int32Array};
+ ///
+ /// let array = Int32Array::from(vec![1, 2, 3, 4, 5]);
+ /// let r = BooleanArray::from_unary(&array, |x| x > 2);
+ /// assert_eq!(&r, &BooleanArray::from(vec![false, false, true, true,
true]));
+ /// ```
+ pub fn from_unary<T: ArrayAccessor, F>(left: T, mut op: F) -> Self
+ where
+ F: FnMut(T::Item) -> bool,
+ {
+ let null_bit_buffer = left
+ .data()
+ .null_buffer()
+ .map(|b| b.bit_slice(left.offset(), left.len()));
+
+ let buffer = MutableBuffer::collect_bool(left.len(), |i| unsafe {
+ // SAFETY: i in range 0..len
+ op(left.value_unchecked(i))
+ });
+
+ let data = unsafe {
+ ArrayData::new_unchecked(
+ DataType::Boolean,
+ left.len(),
+ None,
+ null_bit_buffer,
+ 0,
+ vec![Buffer::from(buffer)],
+ vec![],
+ )
+ };
+ Self::from(data)
+ }
+
+ /// Create a [`BooleanArray`] by evaluating the binary operation for
+ /// each element of the provided arrays
+ ///
+ /// ```
+ /// # use arrow_array::{BooleanArray, Int32Array};
+ ///
+ /// let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
+ /// let b = Int32Array::from(vec![1, 2, 0, 2, 5]);
+ /// let r = BooleanArray::from_binary(&a, &b, |a, b| a == b).unwrap();
+ /// assert_eq!(&r, &BooleanArray::from(vec![true, true, false, false,
true]));
+ /// ```
+ ///
+ /// # Panics
+ ///
+ /// This function panics if left and right are not the same length
Review Comment:
I specifically want this to be infallible, because I want a future
`try_from_binary` to be able to return a user-provided error, instead of
bundling it up in `ArrowError`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]