@@ -1113,6 +1113,27 @@ impl FSharpArray {
11131113 self . skip ( py, count as isize , cons)
11141114 }
11151115
1116+ #[ pyo3( signature = ( predicate, cons=None ) ) ]
1117+ pub fn take_while (
1118+ & self ,
1119+ py : Python < ' _ > ,
1120+ predicate : & Bound < ' _ , PyAny > ,
1121+ cons : Option < & Bound < ' _ , PyAny > > ,
1122+ ) -> PyResult < FSharpArray > {
1123+ let len = self . storage . len ( ) ;
1124+ let mut count = 0 ;
1125+
1126+ while count < len {
1127+ let item = self . get_item_at_index ( count as isize , py) ?;
1128+ if !predicate. call1 ( ( item, ) ) ?. is_truthy ( ) ? {
1129+ break ;
1130+ }
1131+ count += 1 ;
1132+ }
1133+
1134+ self . take ( py, count as isize , cons)
1135+ }
1136+
11161137 pub fn chunk_by_size ( & self , py : Python < ' _ > , chunk_size : usize ) -> PyResult < Self > {
11171138 if chunk_size < 1 {
11181139 return Err ( PyErr :: new :: < exceptions:: PyValueError , _ > (
@@ -3234,6 +3255,17 @@ pub fn skip_while(
32343255 array. skip_while ( py, predicate, cons)
32353256}
32363257
3258+ #[ pyfunction]
3259+ #[ pyo3( signature = ( predicate, array, cons=None ) ) ]
3260+ pub fn take_while (
3261+ py : Python < ' _ > ,
3262+ predicate : & Bound < ' _ , PyAny > ,
3263+ array : & FSharpArray ,
3264+ cons : Option < & Bound < ' _ , PyAny > > ,
3265+ ) -> PyResult < FSharpArray > {
3266+ array. take_while ( py, predicate, cons)
3267+ }
3268+
32373269#[ pyfunction]
32383270pub fn chunk_by_size (
32393271 py : Python < ' _ > ,
@@ -4551,6 +4583,7 @@ pub fn register_array_module(parent_module: &Bound<'_, PyModule>) -> PyResult<()
45514583 m. add_function ( wrap_pyfunction ! ( sum_by, & m) ?) ?;
45524584 m. add_function ( wrap_pyfunction ! ( tail, & m) ?) ?;
45534585 m. add_function ( wrap_pyfunction ! ( take, & m) ?) ?;
4586+ m. add_function ( wrap_pyfunction ! ( take_while, & m) ?) ?;
45544587 m. add_function ( wrap_pyfunction ! ( transpose, & m) ?) ?;
45554588 m. add_function ( wrap_pyfunction ! ( try_find, & m) ?) ?;
45564589 m. add_function ( wrap_pyfunction ! ( try_find_back, & m) ?) ?;
0 commit comments