Skip to content

Commit 0847dbc

Browse files
committed
[Python] Add support for Array.takeWhile
1 parent 6773e74 commit 0847dbc

File tree

7 files changed

+53
-8
lines changed

7 files changed

+53
-8
lines changed

pyproject.toml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,28 +47,24 @@ reportUnusedVariable = false
4747
reportUnnecessaryIsInstance = true
4848
reportUnnecessaryComparison = true
4949
reportUnnecessaryCast = true
50-
reportPrivateUsage = false # Getters/setters reference private members from outside the class
50+
reportPrivateUsage = true
5151
reportImportCycles = true
5252
reportDuplicateImport = true
5353
reportConstantRedefinition = true
5454
reportOverlappingOverload = true
5555
reportInconsistentConstructor = true
5656
reportImplicitStringConcatenation = true
57-
5857
pythonVersion = "3.12"
5958
typeCheckingMode = "standard"
6059

6160
[tool.ruff]
61+
# Keep in sync with .pre-commit-config.yaml
6262
line-length = 120
63+
lint.ignore = []
64+
lint.select = ["E", "W", "F", "I", "T", "RUF", "TID", "UP"]
6365
target-version = "py312"
6466
include =["*.py"]
6567

66-
[tool.ruff.lint]
67-
select = ["E", "W", "F", "I", "T", "RUF", "TID", "UP"]
68-
ignore = [
69-
"F841" # Ignore unused variable warnings
70-
]
71-
7268
[tool.ruff.lint.flake8-bugbear]
7369
# These Rust extension module types are immutable (frozen)
7470
extend-immutable-calls = [

src/Fable.Cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* [Python] Add `Array.skipWhile` support (by @dbrattli)
13+
* [Python] Add `Array.takeWhile` support (by @dbrattli)
1314
* [Python] Allow `IEnumerator_1` as base class to fix typing issues (by @dbrattli)
1415
* [Python] Add Pythonic import path syntax for relative imports (`.module`, `..parent`, `...grandparent`) (by @dbrattli)
1516
* [Python] Add `[<Py.DecorateTemplate>]` attribute for creating custom decorator attributes (by @dbrattli)

src/Fable.Compiler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
* [Python] Add `Array.skipWhile` support (by @dbrattli)
13+
* [Python] Add `Array.takeWhile` support (by @dbrattli)
1314
* [Python] Allow `IEnumerator_1` as base class to fix typing issues (by @dbrattli)
1415
* [Python] Add Pythonic import path syntax for relative imports (`.module`, `..parent`, `...grandparent`) (by @dbrattli)
1516
* [Python] Add `[<Py.DecorateTemplate>]` attribute for creating custom decorator attributes (by @dbrattli)

src/fable-library-py/fable_library/array_.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
unzip = array.unzip
116116
copy = array.copy
117117
take = array.take
118+
take_while = array.take_while
118119
skip = array.skip
119120
skip_while = array.skip_while
120121
compare_to = array.compare_to
@@ -222,6 +223,7 @@
222223
"sum_by",
223224
"tail",
224225
"take",
226+
"take_while",
225227
"transpose",
226228
"truncate",
227229
"try_find",

src/fable-library-py/fable_library/core/array.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ class FSharpArray[T](MutableSequence[T]):
179179
def sum_by[U](self, projection: Callable[[T], U], adder: IGenericAdder[U]) -> U: ...
180180
def tail(self, cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
181181
def take(self, count: SupportsInt, cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
182+
def take_while(self, predicate: Callable[[T], bool], cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
182183
def transpose(self, cons: FSharpCons[FSharpArray[T]] | None = None) -> FSharpArray[FSharpArray[T]]: ...
183184
def truncate(self, count: SupportsInt) -> FSharpArray[T]: ...
184185
def try_find(self, predicate: Callable[[T], bool]) -> T | None: ...
@@ -353,6 +354,9 @@ def sum[T](array: FSharpArray[T], adder: IGenericAdder[T]) -> T: ...
353354
def sum_by[T, U](projection: Callable[[T], U], array: FSharpArray[T], adder: IGenericAdder[U]) -> U: ...
354355
def tail[T](array: FSharpArray[T], cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
355356
def take[T](count: SupportsInt, array: FSharpArray[T], cons: FSharpCons[T] | None = None) -> FSharpArray[T]: ...
357+
def take_while[T](
358+
predicate: Callable[[T], bool], array: FSharpArray[T], cons: FSharpCons[T] | None = None
359+
) -> FSharpArray[T]: ...
356360
def transpose[T](
357361
array: FSharpArray[FSharpArray[T]] | Iterable[FSharpArray[T]], cons: FSharpCons[FSharpArray[T]] | None = None
358362
) -> FSharpArray[FSharpArray[T]]: ...

src/fable-library-py/src/array.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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]
32383270
pub 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)?)?;

tests/Python/TestArray.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,14 @@ let ``test Array.skipWhile works`` () =
735735
xs |> Array.skipWhile (fun x -> x < 6) |> equal [||]
736736
[||] |> Array.skipWhile (fun x -> x < 3) |> equal [||]
737737

738+
[<Fact>]
739+
let ``test Array.takeWhile works`` () =
740+
let xs = [|1; 2; 3; 4; 5|]
741+
xs |> Array.takeWhile (fun x -> x < 3) |> equal [|1; 2|]
742+
xs |> Array.takeWhile (fun x -> x < 1) |> equal [||]
743+
xs |> Array.takeWhile (fun x -> x < 6) |> equal [|1; 2; 3; 4; 5|]
744+
[||] |> Array.takeWhile (fun x -> x < 3) |> equal [||]
745+
738746
// [<Fact>]
739747
// let ``test Array.sortDescending works`` () =
740748
// let xs = [|3; 4; 1; -3; 2; 10|]

0 commit comments

Comments
 (0)